B4A Library SD EscPos Printer (Lan,USB, Bluetooth SPP, BLE)

This library allows you to print with thermal printers compatible with the ESC POS language in 4 different communication channels (USB, Bluetooth SPP, BLE, LAN)
you can download the ble3 library from here

The DEMO version prints one minute every 2. The full version of the library will be issued to those making a donation (Contact me in private before making a donation)

WARNING: Make sure the library meets all your needs, is compatible with the printer you need to use.

For ESC/POS B4J version see Here
For ESC/POS B4i version see Here
For ZEBRA printer see Here


Those who want a different version, such as LAN, USB, NFC please contact me privately.
OSBluetooth SPPBLE (Beacon)USB LAN / WIFI RS 232 NFC
Androidxxxx
iOsxx (on request)
Windowxx (on request)x (on request)

To print a PDF with ESC/POS see



SD_EscPosPrinter

Author:
Star-Dust
Version: 1.08
  • BLE_Printer
    • Events:
      • BLEoff
      • Connected (services As List)
      • Disconnected
      • ImageWriteComplete
      • PrinterFound (Name As String, ID As String)
      • StateChanged (msg As String)
      • WriteComplete (Characteristic As String, Success As Boolean)
    • Fields:
      • CharatteristicName As String
      • Charset As String
      • currentState As Int
      • ServiceName As String
    • Functions:
      • AddTab (ArrayTab As Int()) As String
        Add Tab
        eg. PrinterBLE.AddTab(Array As Byte(100,150,121))
      • Beep (Times As Byte, SecondsDurate As Byte) As String
        .Printer.Beep(1..9,1..9)
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • Connect (ID As String) As String
      • Disconnect As String
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • InitializePrinter As String
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • LeftJustify As String
        after this command calls SendBufferToPrinter
      • PrintBitmap (bmp As Bitmap)
      • RightJustify As String
        after this command calls SendBufferToPrinter
      • ScanPrinter As String
      • SetCodePage (Code As Byte) As String
        es. Printer.SetCodePage(06) for umlauts char
      • Write (Text As String) As String
        Write (Print) a Text
      • Write_ArrayByte (B As Byte()) As String
        Write (Send to Printer) a Array of Byte
        eg. PrinterBLE.Write_ArrayByte(Array As Byte(0x1D,150,23))
        eg. PrinterBLE.Write_ArrayByte("Text".GetBytes("UTF8"))
      • WriteBarCode (Code As String) As String
      • WriteBarCode2D (Code As String) As String
      • WriteLine (Text As String) As String
        Write (Print) a Text + CHLF
      • WriteList (list As List) As String
        Write (Print) a List
    • Properties:
      • CodeTable
        es. printer.setCodeTable(Encoding.Code_WPC1252)
        aftet this command calls SendBufferToPrinter
      • isConnect As Boolean [read only]
      • isScanning As Boolean [read only]
      • Spacing
        after setSpacing command calls SendBufferToPrinter
  • BT_Printer
    Note: AddPermission(android.permission.ACCESS_COARSE_LOCATION)
    • Events:
      • BluetoothIsDisabled
      • ConnectedToPrint (Success As Boolean)
      • DataReceived (Buffer() As Byte)
      • DisconnectToPrint (Mac As String)
      • DiscoveryComplete (Printers As Map)
      • DiscoveryFinished
      • DiscoveryNewPrinter (PrintedFound As Map, DeviceClass As Int)
      • DiscoveryNoDeviceFound
      • ErrorDiscovery
      • ListPrinterAssociated (ListNameAndMac As Map)
      • SendingError (Mac As String)
      • SendTerminated
    • Fields:
      • Charset As String
      • Const_AUDIO_VIDEO As Int
      • Const_COMPUTER As Int
      • Const_HEALTH As Int
      • Const_IMAGING As Int
      • Const_MISC As Int
      • Const_NETWORKING As Int
      • Const_PERIPHERAL As Int
      • Const_PHONE As Int
      • Const_TOY As Int
      • Const_UNCATEGORIZED As Int
      • Const_WEARABLE As Int
    • Functions:
      • AddBuffer_ArrayByte (B As Byte()) As String
      • AddBuffer_BarCode (Code As String) As String
      • AddBuffer_BarCode2D (Code As String) As String
      • AddBuffer_Bitmap (Bmp As Bitmap, ShiftLeft As Int) As String
      • AddBuffer_BitmapAlternativeCoding (Bmp As Bitmap) As String
      • AddBuffer_List_Write (list As List) As String
      • AddBuffer_List_WriteLine (list As List) As String
      • AddBuffer_Tab (ArrayTab As Int()) As String
      • AddBuffer_Write (Text As String) As String
      • AddBuffer_WriteLine (Text As String) As String
      • AddPrintDefineImage As String
      • Beep (Times As Byte, SecondsDurate As Byte) As String
        .Printer.Beep(1..9,1..9)
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • ClearBuffer As String
      • Connected As Boolean
      • ConnectFromMac (Mac As String) As String
      • DefineImage (Image As Bitmap)
      • Disconnect As String
      • flushAllAndClose
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • InitializePrinter As String
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • LeftJustify As String
        after this command calls SendBufferToPrinter
      • Preview As Bitmap
      • ResetPreViewWidth (NewWidth As Int) As String
      • RightJustify As String
        after this command calls SendBufferToPrinter
      • SearchFromAssociatedPrinter As String
      • SearchNewPrinter As String
        select --------------------------------------------
      • SendBufferToPrinter
      • SetCodePage (Code As Byte) As String
        es. Printer.SetCodePage(06) for umlauts char
      • WriteBarCode (Code As String) As String
        send to print immediately, do not preview - Not available in the demo version
      • WriteBarCode2D (Code As String) As String
        send to print immediately, do not preview - Not available in the demo version
      • WriteBitmap (Bmp As Bitmap) As String
        send to print immediately, do not preview - Not available in the demo version
      • WriteByte (Data As Byte()) As String
        send to print immediately, do not preview - Not available in the demo version
      • WriteText (Text As String) As String
        send to print immediately, do not preview - Not available in the demo version
    • Properties:
      • CodeTable
        es. printer.setCodeTable(Encoding.Code_WPC1252)
        aftet this command calls SendBufferToPrinter
      • Spacing
        after setSpacing command calls SendBufferToPrinter
  • ESC_POS
    Code module
    Subs in this code module will be accessible from all modules.
    • Fields:
      • BoldOff As String
      • BoldOn As String
      • cn_PDF417 As String
      • cn_QRcode As String
      • CutPaper As String
      • CutPartialPaper As String
      • CutTotalPaper As String
      • Demo As Boolean
      • DoubleOff As String
      • DoubleOn As String
      • FontA_Bold As String
      • FontA_DoubleHight As String
      • FontA_DoubleWide As String
      • FontA_DoubleWideHeight As String
      • FontA_Normal As String
      • FontB_Bold As String
      • FontB_DoubleHeight As String
      • FontB_DoubleWide As String
      • FontB_DoubleWideHeight As String
      • FontB_Normal As String
      • Horizzontal As String
      • InitializePrinter As String
      • ItalicFontOff As String
      • ItalicFontOn As String
      • NoUnderline As String
      • QueryErrorCauses As String
      • QueryOfflineCauses As String
      • QueryPaperStatus As String
      • QueryPrinterStatus As String
      • Underline1 As String
      • Underline2 As String
      • Vertical As String
    • Functions:
      • feed (lines As Int) As String
      • Process_Globals As String
  • Encoding
    Code module
    Subs in this code module will be accessible from all modules.
    • Fields:
      • CharSet_Chinese As String
      • CharSet_ChineseS As String
      • CharSet_DOS_Latin_1 As String
      • CharSet_IBM_PC As String
      • CharSet_ISO8859 As String
      • CharSet_UTF8 As String
      • CharSet_Windows1252 As String
      • Code_PC437 As Int
      • Code_PC850 As Int
      • Code_PC857 As Int
      • Code_PC858 As Int
      • Code_PC860 As Int
      • Code_PC863 As Int
      • Code_PC865 As Int
      • Code_PC866 As Int
      • Code_WPC1252 As Int
    • Functions:
      • Process_Globals As String
  • LAN_Printer
    • Events:
      • Connected (Success As Boolean)
      • DataReceived (Data() As Byte)
      • Diconnected
      • ErrorTrasmission
    • Fields:
      • Charset As String
    • Functions:
      • AddTab (ArrayTab As Int()) As String
        Add Tab
        eg. PrinterBLE.AddTab(Array As Byte(100,150,121))
      • Beep (Times As Byte, SecondsDurate As Byte) As String
        .Printer.Beep(1..9,1..9)
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • Connect (Host As String, Port As Int) As String
      • Disconnect As String
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • InitializePrinter As String
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • LanConnection_Connected (Successful As Boolean) As String
      • LeftJustify As String
        after this command calls SendBufferToPrinter
      • PrintBitmap (bmp As Bitmap) As String
      • RightJustify As String
        after this command calls SendBufferToPrinter
      • SetCodePage (Code As Byte) As String
        es. Printer.SetCodePage(06) for umlauts char
      • Write (Text As String) As String
        Write (Print) a Text
      • Write_ArrayByte (B As Byte()) As String
        Write (Send to Printer) a Array of Byte
        eg. PrinterBLE.Write_ArrayByte(Array As Byte(0x1D,150,23))
        eg. PrinterBLE.Write_ArrayByte("Text".GetBytes("UTF8"))
      • WriteBarCode (Code As String) As String
      • WriteBarCode2D (Code As String) As String
      • WriteLine (Text As String) As String
        Write (Print) a Text + CHLF
      • WriteList (list As List) As String
        Write (Print) a List
    • Properties:
      • CodeTable
        es. printer.setCodeTable(Encoding.Code_WPC1252)
        aftet this command calls SendBufferToPrinter
      • Spacing
        after setSpacing command calls SendBufferToPrinter
  • RS_Printer
    • Fields:
      • Charset As String
    • Functions:
      • AddTab (ArrayTab As Int()) As String
        Add Tab
        eg. PrinterBLE.AddTab(Array As Byte(100,150,121))
      • Beep (Times As Byte, SecondsDurate As Byte) As String
        .Printer.Beep(1..9,1..9)
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • Connect (SerialDevOutput As String) As String
        es. Connect("/dev/ttyAMA1")
        es. Connect("/dev/ttyS0")
      • Disconnect As String
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • InitializePrinter As String
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • LeftJustify As String
        after this command calls SendBufferToPrinter
      • PrintBitmap (bmp As Bitmap) As String
      • RightJustify As String
        after this command calls SendBufferToPrinter
      • SetCodePage (Code As Byte) As String
        es. Printer.SetCodePage(06) for umlauts char
      • Write (Text As String) As String
        Write (Print) a Text
      • Write_ArrayByte (B As Byte()) As String
        Write (Send to Printer) a Array of Byte
        eg. PrinterBLE.Write_ArrayByte(Array As Byte(0x1D,150,23))
        eg. PrinterBLE.Write_ArrayByte("Text".GetBytes("UTF8"))
      • WriteBarCode (Code As String) As String
      • WriteBarCode2D (Code As String) As String
      • WriteLine (Text As String) As String
        Write (Print) a Text + CHLF
      • WriteList (list As List) As String
        Write (Print) a List
    • Properties:
      • CodeTable
        es. printer.setCodeTable(Encoding.Code_WPC1252)
        aftet this command calls SendBufferToPrinter
      • Spacing
        after setSpacing command calls SendBufferToPrinter
  • USB_Printer
    • Events:
      • WriteComplete
    • Fields:
      • Charset As String
    • Functions:
      • AddTab (ArrayTab As Int()) As String
        Add Tab
        eg. PrinterBLE.AddTab(Array As Byte(100,150,121))
      • Beep (Times As Byte, SecondsDurate As Byte) As String
        .Printer.Beep(1..9,1..9)
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • CloseConnection As String
      • FindAdbDevice As Boolean
      • HasPermission As Boolean
      • Initialize (CallBack As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • InitializePrinter As String
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • LeftJustify As String
        after this command calls SendBufferToPrinter
      • OpenConnection As String
      • PrintBitmap (bmp As Bitmap) As String
      • RequestPermission As String
      • RightJustify As String
        after this command calls SendBufferToPrinter
      • SetCodePage (Code As Byte) As String
        es. Printer.SetCodePage(06) for umlauts char
      • Write (Text As String) As String
        Write (Print) a Text
      • Write_ArrayByte (D As Byte()) As String
        Write (Send to Printer) a Array of Byte
        eg. PrinterBLE.Write_ArrayByte(Array As Byte(0x1D,150,23))
        eg. PrinterBLE.Write_ArrayByte("Text".GetBytes("UTF8"))
      • WriteBarCode (Code As String) As String
      • WriteBarCode2D (Code As String) As String
      • WriteLine (Text As String) As String
        Write (Print) a Text + CHLF
      • WriteList (list As List) As String
        Write (Print) a List
    • Properties:
      • CodeTable
        es. printer.setCodeTable(Enoding.Code_WPC1252)
        aftet this command calls SendBufferToPrinter
      • Spacing
        after setSpacing command calls SendBufferToPrinter


log release
  • 1.04
    • Added CutPartialPaper, CutTotalPaper and feed(lines As Int) in esc_pos.
    • Added WriteBitmap WriteByte WriteText - send to print immediately
  • 1.05
    • Added conversions of color images to black and white for printing
  • 1.06
    • fixed LAN classbug
  • 1.07
    • Added Beep method
  • 1.08
    • Added WriteBarCode, WriteBarCode2D and for Printer_BT also added AddBarCode2d. Other commands to print 2D barcodes compatible with some POS ESCs
 

Attachments

  • SD_EscPosPrinter 1.08.zip
    47.3 KB · Views: 232
Last edited:

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
B4X:
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg & Chr(13) & Chr(10)
    SendMsg = SendMsg &  Chr(27) & "i" & CRLF

This is the code I use to send empty line feed to the printer and cut the paper. Reduce according to your needs.
Sorry, I was guessing cut behaviour, But I tested the code now, it worked fine for long receipt as well, thank you
 

Star-Dust

Expert
Licensed User
Longtime User
Only for the month of August 2022
  • ESCPOS Printer library (Android) (USB, LAN, BLE, Bluetooth)
  • iSD_BLE_Printer (ios) (BLE)
  • jSD BT_Printer (Windows) (Bluetooth SPP)

Promotion Price : 30€
Contact me with a private message before making the donation
 
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
For whatever reason, the search for a Bluetooth printer using the sample project doesn't return anything in my case. However, I can detect the printer if I run a different project (not using this library) on the same tablet.

Any ideas?
 

Star-Dust

Expert
Licensed User
Longtime User
For whatever reason, the search for a Bluetooth printer using the sample project doesn't return anything in my case. However, I can detect the printer if I run a different project (not using this library) on the same tablet.

Any ideas?
what tablet is it and what version of Android it uses? Bluetooth or ble?

Try setting SDK target to 19 to verify that it's not a question of permissions or manifest
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
NEWS
The DEMO version will allow you to print just one minute every 2. Now you can also print images and barcodes
 

iCAB

Well-Known Member
Licensed User
Longtime User
what tablet is it and what version of Android it uses? Bluetooth or ble?

Try setting SDK target to 19 to verify that it's not a question of permissions or manifest
Using Target SDK 19 I can see the printer and it does connect (according to the toast message). But If I try to click the "Text" button, it says "Demo Library -sorry not available"
 

Star-Dust

Expert
Licensed User
Longtime User
Using Target SDK 19 I can see the printer and it does connect (according to the toast message). But If I try to click the "Text" button, it says "Demo Library -sorry not available"
Well, that means it connects and works. If you use target 19 and function and with others it doesn't work then you have to fix the Manifest and the runtime permission request.

As for the DEMO version, if you download the one that is now available (I updated a couple of hours ago) you can print one minute every 2. So if it gives you the message just wait 60 seconds and restart the print
 

iCAB

Well-Known Member
Licensed User
Longtime User
I updated to version 1.01 and still getting locked out.
I wish you can provide a bit more flexibility, perhaps lock it based on date & time or something like that (it will be difficult to break in production)
 

Star-Dust

Expert
Licensed User
Longtime User
I updated to version 1.01 and still getting locked out.
I wish you can provide a bit more flexibility, perhaps lock it based on date & time or something like that (it will be difficult to break in production)
It freezes in odd minutes, you just have to wait 30/60 seconds
 

Star-Dust

Expert
Licensed User
Longtime User
Printer:
Printer.Write(ESC_POS.FontA_Normal)
Printer.Write(ESC_POS.FontB_Bold)
Printer.Write(ESC_POS.FontA_DoubleWideHeight)
' etc....
See also the example attached to the second post.
Or the examples attached in the above BT_Printer library
 
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
@Star-Dust thanks for your reply.
I am getting an exception with the code below


B4X:
            If BTConnected Then
                B_Printer.ClearBuffer
                B_Printer.AddBuffer_Writeline(Chr(27) & Chr(97) & Chr(50))
                B_Printer.RightJustify
                B_Printer.AddBuffer_Writeline("TEST -RIGHT")
                B_Printer.LeftJustify
                B_Printer.AddBuffer_Writeline("TEST - LEFT")
                B_Printer.CenterJustify
                B_Printer.AddBuffer_Writeline("TEST - CENTER")
                B_Printer.AddBuffer_Writeline("PRINT Euros char on chinese printer")
                
                
                    'The line below causes an exception. if I comment it out, all is good
                    ' java.lang.RuntimeException: No device connected
                    '    at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:413)
                    '    at anywheresoftware.b4a.objects.BleManager2.WriteData(BleManager2.java:371)
                    
                BL_Printer.Write(ESC_POS.FontA_Normal)
            End if
 

Star-Dust

Expert
Licensed User
Longtime User
change:
BL_Printer.Write(ESC_POS.FontA_Normal)
into
B_Printer.Write(ESC_POS.FontA_Normal)

(BL_Printer is for BLE / B_Printer for Bluetooth SPP)
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi @Star-Dust

How do I verify that the image and BarCode printing actually work with my printer? and in which format is the BarCode being generated?

Thanks again!
iCAB
 

Star-Dust

Expert
Licensed User
Longtime User
Hi @Star-Dust

How do I verify that the image and BarCode printing actually work with my printer? and in which format is the BarCode being generated?

Thanks again!
iCAB
For the printing of the images see the example in post # 2. For BarCode use the Printer.WriteBarCode("1234567890").

As always, the DEMO version prints in even minutes
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Update rel. 1.04
  • Added WriteBitmap WriteByte WriteText - send to print immediately
  • Added CutPartialPaper, CutTotalPaper and feed(lines As Int) in esc_pos.
On some printers the CutPaper command does not work, so I added CutTotalPaper and CutPartialPaper for printers that use other ESC / POS codes.
Also I added the feed function to scroll through the lines
B4X:
Printer.Write(ESC_POS.feed(5)) ' feed 5 lines'
 
Last edited:

menneaduplo

Member
Licensed User
Longtime User
Only for the month of August 2022
  • ESCPOS Printer library (Android) (USB, LAN, BLE, Bluetooth)
  • iSD_BLE_Printer (ios) (BLE)
  • jSD BT_Printer (Windows) (Bluetooth SPP)

Promotion Price : 30€
Contact me with a private message before making the donation
Hi Star-Dust,
I would like to buy the library for B4A. I need to print images on 4 inch BIXOLON. I tried the demo with the text and I trust that the full version works well with the images as well.

How can I contact you privately?
 

Star-Dust

Expert
Licensed User
Longtime User
Hi Star-Dust,
I would like to buy the library for B4A. I need to print images on 4 inch BIXOLON. I tried the demo with the text and I trust that the full version works well with the images as well.

How can I contact you privately?
I don't know this printer. You should try the demo version first. if you can print the images then it is compatible. Proof that the library has all the functions you need.
 
Top