Printing using Bluetooth printers in Android
Printing using Bluetooth printers is usually not desired for android developers, every printer has it’s own SDK, And if you use a large verity of printers you need to include many lib’s and thus deal with them, this will affect the apk size and might cause issues.
How printers work?
Android devices talk with printers via Bluetooth socket by sending a set of commands, these commands are defined in the printer’s command manual, and each command has a specific job.
command sample: 0x1B, 0x33 (to initialize printer)
The commands are usually similar in most printers, And every printer provides you with an Android SDK interface for using these commands and to deliver them to the printer.
Well, We can deal with all printers without using their SDK’s by sending the set of commands via Bluetooth directly.
Printooth
Printooth is an android library, provides us a simple interface to deal with all kinds and types of Bluetooth printers. one library for all printers.
You can make a simple demo by following a few steps :
Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add dependency
dependencies {
implementation 'com.github.mazenrashed:Printooth:${LAST_VERSION}'
}
Add permissions to manifest
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Initialize Printooth
Should be initialized once in Application.onCreate()
:
Printooth.init(context);
Scan and pair printer
Printooth provides a scanning activity to make pairing process easy. Just start ScanningActivity
and you will skip the process of pairing and saving the printer.
startActivityForResult(Intent(this, ScanningActivity::class.java), ScanningActivity.SCANNING_FOR_PRINTER)
When the printer is ready:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK)
//Printer is ready now
}
If you want to create your own user interface, you can pass your paired printer to Printooth like this:
Printooth.setPrinter(printerName, printerAddress)
Printing
Printooth provide a simple builder to design your paper. To print Hello World
simply write this code:
var printables = ArrayList<Printable>()
var printable = Printable.PrintableBuilder()
.setText("Hello World")
printables.add(printable)
BluetoothPrinter.printer().print(printables)
Use all builder functionalities:
var printables = ArrayList<Printable>()
var printable = Printable.PrintableBuilder()
.setText("Hello World") //The text you want to print
.setAlignment(DefaultPrinter.ALLIGMENT_CENTER)
.setEmphasizedMode(DefaultPrinter.EMPHASISED_MODE_BOLD) //Bold or normal
.setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
.setUnderlined(DefaultPrinter.UNDELINED_MODE_ON) // Underline on/off
.setCharacterCode(DefaultPrinter.CHARACTER_CODE_USA_CP437) // Character code to support languages
.setLineSpacing(DefaultPrinter.LINE_SPACING_60)
.setNewLinesAfter(1) // To provide n lines after sentence
.build()
printables.add(printable)
BluetoothPrinter.printer().print(printables)
Finally
Printooth provides many solutions to a large verity of cases and options to ease up the process of dealing and tweaking the printers you want.
That was everything for today, If you have any questions please reach out on Linkedin or leave a comment.