Mobile printing with Android and Zebra printer

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:

  1. Add permissions to your AndroidManifest.xml
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  2. 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);
  3. 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();
    }
  4. 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:

  1. Add permissions to your AndroidManifest.xml
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  2. 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);
  3. 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();
    }
  4. 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.

12 thoughts on “Mobile printing with Android and Zebra printer

  1. 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 ?

  2. Dear Sir,

    I want to connect bluetooth connection .

    ‘Connection printerConnection= new BluetoothConnection(bDevice.getAddress());’

    but it doesn’t use. why?

  3. 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.

    • 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

Dă-i un răspuns lui Thu Anulează răspunsul

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.