Rubriky
Development

React Native – bluetooth printer control

Yes, iOS does not work with Blueooth thermal printers out of the box (share – bluetooth – printer – send). And the point of this article is to share a few experience which I’ve gained during the development of an application, which uses this kind of device.

iOS and bluetooth printers

As written – iOS and thermal printers (ESC/POS) do not work together out of the box. And even worse — the pairing itself does not work the usual way either.

Already written library? Bad luck.

The first thing that came up to my mind was to take a look at the existing libraries. And yes, there are like 2 of them:
https://github.com/leesiongchan/react-native-esc-pos
and
https://github.com/januslo/react-native-bluetooth-escpos-printer

but…

The first one (react-native-esc-pos) declares no support for iOS, so that one was a no-go.
The second one almost drove me crazy. It should support iOS and it also should cover all the necessities (bluetooth pairing, encoding ESC/POS instructions etc…), but…(again)

After taking care of all the React Native obstacles (navigation dependencies issues, non-working bluetooth in iOS emulator etc.) I’ve successfully listed all the bluetooth devices and also connected to the desired one (yay!).

All that remained was to print something. So I’ve read the documentation and sent a first request. And… nothing happened.

Simply nothing was happening. No beeps, not LED blinking, no logs.

Hex-byte-what?

After hours spent trying to make the library work, I’ve decided to do the the dirty work myself and use only the low level react-native-ble-plx library for the bluetooth communication and programme the ESC POS communication myself.

Since it required encoding the text correctly, translate the commands (and the text) into hexa instructions etc., I’ve spent some time googling and found another handy library – esc-pos-encoder.

Using that one, I was able to only chain all the print setup and print commands and send them to the printer itself. Easy as that.

So after all what has to be done is connect to the device, get the instructions, translate them using the encoder and send them to the device. Done.

One more thing…

Does the printer process only a part of the instructions? Beware of the requestMTU options. After a few tries, I’ve managed to fix the issue setting it to value 512, which seems to work just fine.

And one more problem…

And the problem is called the Android operating system and it’s version of the printer. The iOS printer supports the Bluetooth LE (Low energy) version, which (unfortunately) is not supported for bluetooth. And since the library I used for the iOS only works with the bluetooth LE, I had to start (almost) allover again for Android.

After the pain I’ve been through, I’ve decided to not to complicate the things even more and I used separate libraries for the iOS and for Android. For iOS it was the mentioned react-native-ble-plx and for Android, I’ve used the react-native-bluetooth-serial.

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *