How to Print from Android Devices using Thermal Bluetooth Printer Part One

Yusuf Adefolahan Nurudeen
3 min readFeb 27, 2020

--

Do you need to print data either PDF, DOC or custom information gotten from the server or inside your developed android application? If yes then this post will guide you ways to achieve printing from your android device(Smartphone, POS, Tablet) via bluetooth printer.

There are different solutions available to achieve printing data from android device to thermal printer. I will highlight three major ways to print through any thermal bluetooth printer. One way is for non-developers while two and three are meant to be used by developers.

1. For Non-Developer

This can be performed by downloading application from the Google Play Store and pairing the android device with the printer and printer from any of the application listed below:

Bluetooth Print

2. For Developers (Sample Code)

I have created a sample project and can be easily tweaked to print exact details which includes data from server, details stored in the application and edit text or paste text. Clone my project on github, here is the Link to the project. Import the project to your android studio.

Note: Pair your android device to the thermal printer first then you can click connect in the application and proceed to print.

Feel free the modify the cloned project to suit your need.

3. For Developer (Android Library)

You could make use of PrinterThermal-ESCPOS-Android library which is pretty cool to use and you can include logo to the print. To use the library simply create a new project in android studio, follow below installation steps:

Step 1. Add the JitPack repository to your build file. Add it in your root /build.gradle at the end of repositories:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

Step 2. Add the dependency in /app/build.gradle :

dependencies {
...
implementation 'com.github.DantSu:ESCPOS-ThermalPrinter-Android:3.0.1'
}

Step 3. Add bluetooth permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />

Also, you have to check the bluetooth permission in your app like this :

if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, MainActivity.PERMISSION_BLUETOOTH);
} else {
// Your printing code/method HERE
}

Step 4. You can call the printer library in your activity like this:

Printer printer = new Printer(BluetoothPrinters.selectFirstPairedBluetoothPrinter(), 203, 48f, 32);
printer
.printFormattedText(
"[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM))+"</img>\n" +
"[L]\n" +
"[C]<u><font size='big'>ORDER N°045</font></u>\n" +
"[L]\n" +
"[C]================================\n" +
"[L]\n" +
"[L]<b>BEAUTIFUL SHIRT</b>[R]9.99e\n" +
"[L] + Size : S\n" +
"[L]\n" +
"[L]<b>AWESOME HAT</b>[R]24.99e\n" +
"[L] + Size : 57/58\n" +
"[L]\n" +
"[C]--------------------------------\n" +
"[L][R]TOTAL PRICE :[R]34.98e\n" +
"[L][R]TAX :[R]4.23e\n" +
"[L]\n" +
"[C]================================\n" +
"[L]\n" +
"[L]<font size='tall'>Customer :</font>\n" +
"[L]Raymond DUPONT\n" +
"[L]5 rue des girafes\n" +
"[L]31547 PERPETES\n" +
"[L]Tel : +33801201456\n" +
"[L]\n" +
"[C]<barcode type='ean13' height='10'>831254784551</barcode>\n"
)
.disconnectPrinter();

You can check the library for more styling on github.

If you found the post useful, do share with others. Thanks

--

--