B4A Class Bluetooth ESC/POS Printer Class

Discussion in 'Additional libraries, classes and official updates' started by agraham, Jun 8, 2019.

  1. agraham

    agraham Expert Licensed 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

    Attached Files:

    Last edited: Jun 12, 2019
    rraswisak, M Zuhri, Inman and 15 others like this.
  2. agraham

    agraham Expert Licensed User

    While preparing the above Printer class I have spent hours downloading, trying and discarding printer apps from the store. There are two that I have kept, both ad-free as I discard most anything with ads immediately.

    Pointillist by Aption
    This is a simple app that can print any image on the device. It has some simple image editing capabilities and has much more sophisticated dithering algorithms than I have used so the image quality is slightly better, but still limited by the crudity of the printer.

    RawBT by 402d
    As well as being able to print text, PDF and image files this app can function as an Android print service so that content can be printed from any app that implements the Android printing functions. To print images on my printer it needs to use the "ESC * 33" Setting. There is a fee to remove the banner text notification on printouts including an annual option which I have paid.

    It you have found something better then please post details.
    Sandman likes this.
  3. Sandman

    Sandman Well-Known Member Licensed User

    If it's still available on eBay, perhaps you could link to it?
  4. agraham

    agraham Expert Licensed User

    Last edited: Jun 9, 2019
    Sandman likes this.
  5. agraham

    agraham Expert Licensed User

    Version 2 now posted includes support for creating custom characters. Although the documentation for my printer says that it should be able to create custom characters both for font A and font B it only works for font A on my printer. Selecting UseCustomCharacters=True when CharacterFont=1 causes the printer to turn itself off :(

    However the custom character generation and use works fine for font A and as creating them can be a chore I have included some methods for drawing graphics primitives on a custom character so you can more easily create your mini-masterpieces of art. :)
    Last edited: Jun 12, 2019
  6. djmainero

    djmainero Member Licensed User

    Hi there!
    First of all, thanks for sharing the code!
    I have a question, I was trying to center the printed image with the function "PrintImage ()" without success. I tried "setJustify (1)" but just center the text.

    From already thank you very much!
  7. agraham

    agraham Expert Licensed User

    Images can't be centered unless you manipulate the actual image data. This seems to be a limitation of the ESC/POS command set.
  8. djmainero

    djmainero Member Licensed User

    Thanks for the reply!
    I'm going to fill in white the img to "center it".
    Again thanks!
  9. joaomoraes83

    joaomoraes83 Member Licensed User

    When printing image, sometimes I did not print correctly, any suggestions?
  10. vvg

    vvg Member Licensed User

  11. vvg

    vvg Member Licensed User

    Many thanks agraham, that you have made available this class. I really appreciate because the printer library so far i was using was lacking something (looking tedious job for expected output and tedious indeed also for a hardcore non-java vb6 programmer like me) and even after paid libraries i used also didn't made my work that easy what this class has made....
    Thanx!! Thanx again...
  12. bl4ck4nt

    bl4ck4nt Member Licensed User

    Thanx for your library agraham, i try this library if i disconnect always show "Error: java.io.IOException: bt socket closed, read return: -1", i used MINI Thermal Printer (POS-5802DD)
  13. agraham

    agraham Expert Licensed User

    I guess that would be expected. Just swallow it within a Try - Catch block.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice