Print using Zebra QLn420 printer with andorid application in kotlin.

cover_image

In this post, we will be going to build an android application which will include the following :
1. Scanning for available Bluetooth devices.
2. Connecting to Zebra QLn420 printer.
3. Printing the provided template.

i. Scanning for available Bluetooth devices

We have to define certain variables which we will be going to use later in the program.

ListView -> To provide a list of names & address of nearby Bluetooth devices.
mDeviceList -> To store the names of visible Bluetooth devices.
mBluetoothAdapter -> This will let you perform fundamental Bluetooth tasks.
bluetoothDeviceAddress -> To store the addresses of visible Bluetooth devices.

In activity_main.xml file,

In MainActivity — onCreate, we have to initialize the variables defined above

In this, I have used setOnItemClickListener because I know I’ll only click on the item showing my Zebra printer. You can add as many checks you want according to your preferences.

Then we have to have a broadcast receiver which will receive near by Bluetooth devices and will be defined as:

The device list is submitted to list view which results as

Bluetooth devices listing

We also have to unregister our receiver when our activity is destroyed.
In case you forget to unregister the receiver then it can result in more consumption of resources as the receiver is active in the background where you don’t want it to work.

We have initialised printAction() on list view item click where have to pass the bluetooth address of the printer and the string of the template you want to print.

This string is no ordinary string, it is known as ZPL String and looks like :

This is actually a template with details in it. You can have you own template which you can pass and print.

In this function, we are setting up a bluetooth connection with the printer and we executing this code in try-catch block as there may be error while setting up the connection.

The instance of the printer and the ZPL string are then passed to another function named as ‘sendToPrint()’ to execute further steps.

In sendToPrint, we have created a file with extension .LBL in our local phone storage. The createFile() is used to create that file and then the path of the saved file is passed to the printer which will result in performing print operation.

The whole work is executing on list view setOnItemClickListener(). You can modify the workflow according to your usage.

Full code:

A document shared by Nuri Şahin

link -https://www.zebra.com/content/dam/zebra_new_ia/en-us/solutions-verticals/product/Software/Printer%20Software/Link-OS/print-connect/PC-UserGuide-P1082444-001.pdf

I hope this article would have helped you. In case of any suggestions or queries please do comment.

Android Developer @BluSmart

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store