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,974
Last edited:

emexes

Expert
Licensed User
The problem might not be as obvious to others as it is to you.

I should have added that all of that looks easy to fix. ☮️

The hardest is probably handling reporting for shifts / trading periods that straddle midnight, eg a restaurant open from 5pm until 2am.
 

MR Kai

Member
Licensed User
Longtime User
The problem might not be as obvious to others as it is to you.

Is it that the Print Time is a date?
Is it that the Sales Date is blank?
Is it that restaurant trading days often straddle midnight, ie not just a single date?
Is it that monetary amounts have between 1 and 5 decimal places?
Is it the incorrect total amount of sales by payment method? or
Is it that 0.36 was rounded to 0.3 ?
Is it the adjacent repetition of Revenue and Tips totals?
Is it that Gross Sales is an unexplained 22 higher than Total Sales?
Is it that Take Away QTY is 6.0 ? Could it ever be eg 6.3 ?
Is it the 0 guests and their associated average of 494.06 ?
Is it the mismatched sales totals by group vs profit center?
Is it the 0 void items totalling 2964.36 ?
 
Last edited:

MR Kai

Member
Licensed User
Longtime User
I should have added that all of that looks easy to fix. ☮️

The hardest is probably handling reporting for shifts / trading periods that straddle midnight, eg a restaurant open from 5pm until 2am.
all of data in this report its garbage data and for test
my question about when print attached first image second one what i get from printer
 

emexes

Expert
Licensed User
🍻 all of data in this report its garbage data and for test

I figured it was just sample data, but the calculation and formatting issues remain. Happily, all look minor and easy. ☮️


my question about when print attached first image second one what i get from printer

It is nifty that you have that intermediate image to help diagnose what's happening. 🍻

I do not have one of those printers, but my first general thoughts are:

- has it previously worked when printing other reports by the same method? (ie via image)
- do older/other apps that used to print ok, still print ok?
- is the problem repeatable, eg after turning the printer off and on and restarting your app?
- what happens for shorter reports? eg receipts
 

MR Kai

Member
Licensed User
Longtime User
thank you for your replay
when print receipt its good and contain image with perfect resolution
this report drawing on clv and panel converted to pdf and from pdf to image and print image


PanelToPdf:
public Sub PanelToPdf(Panel As B4XView, FileName As String)
    Try
        Dim pdf As PdfDocument
        pdf.Initialize
        pdf.StartPage(Panel.Width, Panel.Height)
'        pdf.StartPage(595, 842) 'A4 size
        Dim bmp As B4XBitmap = Panel.Snapshot
        Dim dest As Rect
        dest.Initialize(10, 10, 0, 0)
        dest.Width = bmp.Width / bmp.Scale
        dest.Height = bmp.Height / bmp.Scale      
        pdf.Canvas.DrawBitmap(bmp, Null, dest)
        pdf.FinishPage
        Dim out As OutputStream = File.OpenOutput(provider.SharedFolder, FileName, True)
        pdf.WriteToStream(out)
        out.Close
        pdf.Close
    Catch
        Log(LastException)
    End Try
End Sub




and then call this method


print:
Sub SendToPrinter(Fnamed As String)

    Try  
           
        If CashierPrinter.IsConnected=False Then          
            CashierPrinter.Connect("192.168.1.201",9100,0)
            Sleep(2000)
        End If
                   
        CashierPrinter.Reset              
        Dim IMAGEPanel As Bitmap
        IMAGEPanel.InitializeResize(Mod2.provider.SharedFolder, Fnamed,576, 512, True) 'ignore              
        Dim myimage As AnImage = CashierPrinter.ImageToBWIMage(IMAGEPanel)
        myimage = CashierPrinter.ThresholdImage(myimage, 128)
        myimage= CashierPrinter.PackImage(myimage)
        CashierPrinter.WriteString(CRLF)
        CashierPrinter.PrintImage(myimage)
        CashierPrinter.Feed
        CashierPrinter.FullCut
       
    Catch
        Log(LastException)
    End Try
End Sub
 
Last edited:

emexes

Expert
Licensed User
when print receipt its good and contain image with perfect resolution
this report drawing on clv and panel converted to pdf and from pdf to image and print image

clv → panel capture → pdf → image → print 🏆🤣 not much opportunity for anything to go awry there 🤔

Printing a receipt and printing a report, are they the same process?

What if you trim the length (height?) of the report?

Either by cropping* the image, or by deleting some rows from the top and bottom of the clv.

Maybe there is some limit to the height of images that can be printed. Or to the size of the image, *eg* no more than 65535 or 524287 pixels.

That's another idea - try printing simple boxes or grids, say 3 inches wide, first 1 inch high, then 2 inches, then 3 inches, etc... then see if it falls over when it gets to as long as your report.


* or even resizing, and ignore the squashed look for the time being, we're just trying to make the image and printout match.
 

emexes

Expert
Licensed User
Maybe there is some limit to the height of images that can be printed. Or to the size of the image, *eg* no more than 65535 or 524287 pixels.

I am just a nightshift member of this forum, so yes: you and me here are a classic case of the blind leading the blind. 🤣

If we can't nut it out beforehand then, with luck, in ~six hours time @agraham will point out the obvious that we've missed. 🍻
 
Last edited:

emexes

Expert
Licensed User
when print receipt its good and contain image with perfect resolution

Another thought is: try printing a receipt that is as long as your report 🤔

If big receipt prints ok, then compare the receipt-printing and report-printing process/code side-by-side.

If big receipt doesn't print ok, then we are probably looking for a size - or maybe memory - limit.
 

MR Kai

Member
Licensed User
Longtime User
receipt is ok

but this receipt created line by line
 

Attachments

  • yyyy.jpg
    yyyy.jpg
    64.7 KB · Views: 147

aeric

Expert
Licensed User
Longtime User
hi
i have problem with print this report
attached image explain what mean
Seems like a hardware issue. Is it same on another printer that nothing is printed?
Edit: ah, now I get it. You are trying to print this as image and the dimension is very high.
Since all the output are text, why need to convert to image? Alright, maybe you want to store a snapshot copy. There may be memory or buffer limitation in printing images. My idea is to break the whole image into multiple shorter ones and print 1 by 1.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
You should not ask why if you know the forum rules or guideline.

You should post your question in a new thread instead of old thread.
It is benefit for others and you can easily find back your threads. Don't make the thread long or clutter and difficult to read. This is a library thread. This will make future readers having problem to find answer.
 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, PrintImage or PrintImage2?
What is the difference between PrintImage and PrintImage2?
When is it convenient to use one or the other?
Thank you.
 
Top