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:

Ertan

Active Member
Licensed User
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

View attachment 96717
B4X:
    Printer1.WriteString("İrem" & CRLF)    'CRLF is actually only LF
    Printer1.WriteString("İrem" & Chr(13) & Chr(10))    'CR = ASCII 13, LF = ASCII 10
rem writes as. The letter "İ"does not appear.
 

Ertan

Active Member
Licensed User
Can you show me your code that initializes/opens Printer1?
Screenshot_4.png
 

Ertan

Active Member
Licensed User
What do you get if you do:
B4X:
Printer1.WriteString("Hello One")
Printer1.WriteString("Hello Two" & Chr(10))
Printer1.WriteString("Hello Three" & Chr(13))
Printer1.WriteString("Hello Four" & Chr(13) & Chr(10))
Printer1.WriteString("Hello Five")
WhatsApp Image 2020-07-07 at 14.11.59.jpeg

I get results this way.

I noticed something.
1.jpg

When selecting PC857 from the printer's settings and printing all the letters, Turkish letters are printed.

------------------------------------------------------------------------------------------------------------
2.jpg

However, there are no Turkish letters when the Charset is printed on the B4A Bluetooth Printer project.
There is no letter here.
 

Ertan

Active Member
Licensed User
Righto, so that tells us:
- CR/LF is not automatically added
- LF will move to start of next line
- CR does nothing

which aligns with that the class code seems to do.

Yikes, you have good eyes!
SOLVED!

B4X:
Printer1.WriteString2("Türkçe Yazı" & CRLF, "IBM857")
You can print Turkish characters with this code.
IBM857 It is important.
 

Tico.13

Member
Hi @agraham, how can i establish the barcode width, i have try taking out the comment of 'Printer1.BarCodeWidth = 4 and setting a number but i havent get it to work, thanks in advance an best regards
 

agraham

Expert
Licensed User
Longtime User
It's already implemented in the ESCPOSPrinter class
B4X:
' Set the line content justification, must be the first item on a new line
' 0 left, 1 centre, 2 right
Public Sub setJustify(justify As Int)
    WriteString(ESC & "a" & Chr(justify + 48))
End S
 

TILogistic

Expert
Licensed User
Longtime User
ESC a

[Name]
Select justification
[Format]
ASCII ESC a n
Hex1B61n
Decimal2797n
[Range]
n = 0 – 2, 48 – 50
[Default]
n = 0
[Description]
In Standard mode, aligns all the data in one line to the selected layout, using n as follows:
nJustification
0, 48Left justification
1, 49Centered
2, 50Right justification
 
[QUOTE = "agraham, pos: 785087, anggota: 448"]
Ini sudah diterapkan di kelas ESCPOSPrinter
[CODE = b4x] 'Setel justifikasi konten baris, harus menjadi item pertama di baris baru
'0 kiri, 1 tengah, 2 kanan
Public Sub setJustify (justify As Int)
WriteString (ESC & "a" & Chr (rata + 48))
Akhiri S [/ CODE]
[/QUOTE]
terima kasih @agraham. ini sangat membantu saya
 

Tico.13

Member
Hi @agraham ! hope you are fine! How does the printbuffer works? im trying to print in labels paper but after sending to print 3 labels(paper) my printer starts to jumps spaces and print outside the paper or there is a way to set paper size or the gap between labels?
 

Joel R

New Member
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
I had only 1 problem about this printer module regarding on barcode 39 extra character on last digit..
anyone can solve about my problem?
 

agraham

Expert
Licensed User
Longtime User
my printer starts to jumps spaces and print outside the pape
on barcode 39 extra character on last digit..
All these cheap Chinese thermal printers seem buggy in one way or another so you will just have to play with it yourself and see if you can find something that works for you.
 

Urishev

Member
Licensed User
Longtime User
Thank you Mr. Agraham for the wonderful Bluetooth Printer Class.
There are no lines in your example. Does your class support line drawing?
 
Top