B4A Class Bluetooth ESC/POS Printer Class

For the last couple of weeks I have been playing with an 80mm Bluetooth ESC/POS thermal printer I bought off eBay, new and delivered, for the ludicrously cheap price of £20. While the print quality, particularly of images with large areas of black (they are gray due to power supply or thermal dissipation limitations probably), is not great it is adequate. The printer itself seems to be a generic Chinese unit (no surprise there then!) which is re-badged by all and sundry and sold at a remarkably wide range of prices.

Here then is a Printer class that will connect by Bluetooth to the printer and enable access to most of the printer functions, including barcodes and image preparation and printing. The included demo shows the capabilities that are available. To run the demo you will need a paired Bluetooth printer that is switched on. The Printer class uses the BitmapCreator, RandomAccessFile and Serial libraries.

On my printer the only thing that doesn't seem to work properly is QR code generation. Small QR codes don't seem to be able to be decoded by scanners and larger ones look obviously wrong with part of the top of the code replicated at the bottom when printed.

The obvious capabilities missing (so far!) are user defined characters and non-volatile bit images. Also not implemented are some codes that are either duplicates of available commands or ones whose functions I don’t understand.

The class module is included as source code in the demo so you can add any missing capabilities you need. If you add a significant one then please post it for others to play with.

EDIT: Version 2 now posted includes support for creating custom characters. See post #5 for details
 

Attachments

  • BluetoothPrinter_v2.0.zip
    298.6 KB · Views: 2,951
Last edited:

vvg

Member
Licensed User
Longtime User
Best solution. I got here what I wanted.
But agraham, if you can will you please
provide USB printing code in the class?
 

Unobtainius

Active Member
Licensed User
Longtime User
Thank you very much for such a great library, a small donation made. I didn't see the suggested amount before making it so there's an extra coffee in it for you
 

Valerio Minnella

Member
Licensed User
Hi Agraham.
I have integrated your code (many thanks) in my application and it works very well, but if I try to print a .png image it does not print anything or a black square.
With .jpg files it's perfect, but I have to use .png and I can't.

Any suggestion?
 

Oswaldo Sanchez

New Member
Licensed User
Longtime User
For the last couple of weeks I have been playing with an 80mm Bluetooth ESC/POS thermal printer I bought off eBay, new and delivered, for the ludicrously cheap price of £20. While the print quality, particularly of images with large areas of black (they are gray due to power supply or thermal dissipation limitations probably), is not great it is adequate. The printer itself seems to be a generic Chinese unit (no surprise there then!) which is re-badged by all and sundry and sold at a remarkably wide range of prices.

Here then is a Printer class that will connect by Bluetooth to the printer and enable access to most of the printer functions, including barcodes and image preparation and printing. The included demo shows the capabilities that are available. To run the demo you will need a paired Bluetooth printer that is switched on. The Printer class uses the BitmapCreator, RandomAccessFile and Serial libraries.

On my printer the only thing that doesn't seem to work properly is QR code generation. Small QR codes don't seem to be able to be decoded by scanners and larger ones look obviously wrong with part of the top of the code replicated at the bottom when printed.

The obvious capabilities missing (so far!) are user defined characters and non-volatile bit images. Also not implemented are some codes that are either duplicates of available commands or ones whose functions I don’t understand.

The class module is included as source code in the demo so you can add any missing capabilities you need. If you add a significant one then please post it for others to play with.

EDIT: Version 2 now posted includes support for creating custom characters. See post #5 for details


Hello i can print QR Barcode but barcode is never decoded, why this happen?
 

agraham

Expert
Licensed User
Longtime User
Hello i can print QR Barcode but barcode is never decoded, why this happen?
Probably because, just like mine, your cheap Chinese printer is crap.
On my printer the only thing that doesn't seem to work properly is QR code generation. Small QR codes don't seem to be able to be decoded by scanners and larger ones look obviously wrong with part of the top of the code replicated at the bottom when printed.
 

scsjc

Well-Known Member
Licensed User
Longtime User
Awesome job it works super well congratulations :)

B4X:
Public Sub FlushClose
    Astream.SendAllAndClose
End Sub

I recommend adding this sub to the class, to be able to close and fire the event "AStream_Terminated" (because it never fires)
Can be called as Printer1.FlushClose
 

Ertan

Active Member
Licensed User
Hello,
It doesn't print all Turkish characters. Only ( ç , ü , ö , Ü , Ö , Ç ) printing these characters.

" ı, ğ, ş, İ, Ğ, Ş, " I want to print these letters. I've been researching for this for a few months but I couldn't figure it out.

Can you help me to solve this event?

" ı, ğ, ş, İ, Ğ, Ş, "

1. İrem
2. Oğlan
3. OĞLAN
4. Şile
5. şile

I couldn't print the underlined letters. How can we solve this?

WhatsApp Image 2020-07-07 at 11.05.58.jpeg
 

Ertan

Active Member
Licensed User

emexes

Expert
Licensed User
I have been dealing with this for 3 months ..
Ouch. But I admire your tenacity, so I'll join you in the quest. I think worst case is that you might have to print text to a bitmap/image, and then print that. Mildly tedious, but quite doable.

nevergiveup.jpg
 

emexes

Expert
Licensed User
This search suggests that code page 857 is the only code page that contains the characters you want to print:

1594115558295.png
 

emexes

Expert
Licensed User
Can this help in printer settings?
Does a bear sh*t in the woods?!?!

Translation: you might have found the solution. Activate that setting, and then try printing Chr(166) & Chr(167) & Chr(168) to the printer.

Might not work first go, because if you are sending as UTF then those characters will be transformed into multi-byte sequences. But worth a go, sometimes things are simpler than expected. :)
 

emexes

Expert
Licensed User
" P61 "Can this help in printer settings?
I found this in a manual for a similar printer.

Also, could you please show me a fragment of your code that sends text to the printer. Eg are you sending strings or bytes? What happens when you send those extended characters Chr(166) & Chr(167) & Chr(168) to the printer? What characters does it print? (from that, we can probably work out what code page it is currently using, which is a useful starting point)

1594116834230.png
 

Ertan

Active Member
Licensed User
I found this in a manual for a similar printer.

Also, could you please show me a fragment of your code that sends text to the printer. Eg are you sending strings or bytes? What happens when you send those extended characters Chr(166) & Chr(167) & Chr(168) to the printer? What characters does it print? (from that, we can probably work out what code page it is currently using, which is a useful starting point)

View attachment 96713
B4X:
Printer1.WriteString(Chr(166) & Chr(167) & Chr(168))
I'm trying to send it this way, right? but it doesn't print.
Or will you guide me how to do otherwise?
 
Last edited:

emexes

Expert
Licensed User
B4X:
Printer1.WriteString(Chr(166) & Chr(167) & Chr(168))
I'm trying to send it this way, right?
Or will you guide me how to do otherwise?
That looks good. The sample in the photograph, presumably you printed that with:
B4X:
Printer1.WriteString("İrem")
Printer1.WriteString("Oğlan")
Printer1.WriteString("OĞLAN")
Printer1.WriteString("Şile")
Printer1.WriteString("şile")
Does WriteString automatically add CRLF to move to the next line, or do you have to add it "manually" eg:
B4X:
Printer1.WriteString("İrem" & CRLF)    'CRLF is actually only LF
Printer1.WriteString("İrem" & Chr(13) & Chr(10))    'CR = ASCII 13, LF = ASCII 10

1594119487157.png
 
Top