In my last project I used Zebra mobile printers to print data sent from Android devices.
In general there isn’t nothing hard to do, only one thing that I don’t like is that I have to install a big application to get the Zebra SDK ( you can find the SDK here ).
There are few simple steps that we have to do to get Android and Zebra printer connected:
- Add permissions to your AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- In our Activity we have to get the BluetoothAdapter and BondedDevices
BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = bAdapter.getBondedDevices(); final ArrayList<BluetoothDevice> btList = new ArrayList<>(); btList.addAll(pairedDevices);
- Here is the code that can send data to printer
private ZebraPrinter print(BluetoothDevice bDevice,String content) { ZebraPrinter printer = null; Connection printerConnection= new BluetoothConnection(bDevice.getAddress()); try { printerConnection.open(); printer = DataHolder.getInstance().getPrinter(); if(printer == null) { printer = ZebraPrinterFactory.getInstance(PrinterLanguage.CPCL, printerConnection); DataHolder.getInstance().setPrinter(printer); } sendToPrint(printer,content); printerConnection.close(); } catch (ConnectionException e) { Log.d(TAG,e.getMessage()); } finally { } return printer; } private void sendToPrint(ZebraPrinter printer,String content) { try { File filepath = getFileStreamPath("TEMP.LBL"); createFile("TEMP.LBL", content); printer.sendFileContents(filepath.getAbsolutePath()); } catch (ConnectionException e1) { Log.d(TAG,"Error sending file to printer"); } catch (IOException e) { Log.d(TAG,"Error creating file"); } } private void createFile(String fileName, String content) throws IOException { FileOutputStream os = this.openFileOutput(fileName, Context.MODE_PRIVATE); os.write(content.getBytes()); os.flush(); os.close(); }
- Let’s suppose our printer is first in btList array. We can print some data by calling the print() method like this:
print(btList.get(0), content);
Here you can see how this works in real life:
Thanks to MemoExpress for printer.
[:en]In my last project I used Zebra mobile printers to print data sent from Android devices.
In general there isn’t nothing hard to do, only one thing that I don’t like is that I have to install a big application to get the Zebra SDK ( you can find the SDK here ).
There are few simple steps that we have to do to get Android and Zebra printer connected:
- Add permissions to your AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- In our Activity we have to get the BluetoothAdapter and BondedDevices
BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = bAdapter.getBondedDevices(); final ArrayList<BluetoothDevice> btList = new ArrayList<>(); btList.addAll(pairedDevices);
- Here is the code that can send data to printer
private ZebraPrinter print(BluetoothDevice bDevice,String content) { ZebraPrinter printer = null; Connection printerConnection= new BluetoothConnection(bDevice.getAddress()); try { printerConnection.open(); printer = DataHolder.getInstance().getPrinter(); if(printer == null) { printer = ZebraPrinterFactory.getInstance(PrinterLanguage.CPCL, printerConnection); DataHolder.getInstance().setPrinter(printer); } sendToPrint(printer,content); printerConnection.close(); } catch (ConnectionException e) { Log.d(TAG,e.getMessage()); } finally { } return printer; } private void sendToPrint(ZebraPrinter printer,String content) { try { File filepath = getFileStreamPath("TEMP.LBL"); createFile("TEMP.LBL", content); printer.sendFileContents(filepath.getAbsolutePath()); } catch (ConnectionException e1) { Log.d(TAG,"Error sending file to printer"); } catch (IOException e) { Log.d(TAG,"Error creating file"); } } private void createFile(String fileName, String content) throws IOException { FileOutputStream os = this.openFileOutput(fileName, Context.MODE_PRIVATE); os.write(content.getBytes()); os.flush(); os.close(); }
- Let’s suppose our printer is first in btList array. We can print some data by calling the print() method like this:
print(btList.get(0), content);
Here you can see how this works in real life:
Thanks to MemoExpress for printer.
no hay sdk
Dear sir,
I want to print from my Android app to Zebra Qln420 printer , model like in your video.
Where i can find link for sdk, because this one doesn’t work, and how to implement in my project ?
One more question ,
What is DataHolder in your project ?
Thanks/
Sorry dude,
but i found all i ask u, but i only didint know now how u create content ?
In which format or language ?
Dear Sir,
I want to connect bluetooth connection .
‘Connection printerConnection= new BluetoothConnection(bDevice.getAddress());’
but it doesn’t use. why?
Dear Sir,
You can give me sample source code because i have the little difficult to connect zebra mobile printer iMZ320 and android device.
I downloaded Zebra SDK but it doesn’t use BluetoothConnection.
I don’t have this jar(‘com.zebra.sdk.comm.BluetoothConnection;’ ).
Please, Can you give me share zebra_sdk including BluetoothConnection jar?
Thanks.
I will publish in few day on github.com/daniftodi source code.
Thanks, Sir.
HI is there any update on this ?
Did you published this source code ?? if not
Please tell me how to make file .lbl and map variables to it ??
Hi any news for the github code?
I am really interesting in this!
Thanks
Hi, at this moment I developed a new version that can print from PDF files ( most CRM sytems are generating pdf files ). If you are interested in this solution contact me on my email: webulxpert [at] gmail [dot] com