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: 603
Last edited:

MR Kai

Member
Licensed User
Longtime User
hi star
when print image get this
plz check attached images
 

Attachments

  • 1.png
    1.png
    234.9 KB · Views: 273
  • 2.png
    2.png
    242.8 KB · Views: 263

Star-Dust

Expert
Licensed User
Longtime User
hi star
when print image get this
plz check attached images
Image looks too big. You have to shrink it to 330 dots wide and it has to be B/W, shades of gray are not good
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Update rel. 1.05
  • Added conversions of color images to black and white for printing
 

MarcoRj

New Member
Hi, I need to print a webview on a thermal printer.. can I print a webview with this library?...Have any example pls..
 

Star-Dust

Expert
Licensed User
Longtime User
Hi, I need to print a webview on a thermal printer.. can I print a webview with this library?...Have any example pls..
as @aeric correctly said you need to get a picture. webView has a function to export page to image. You must bear in mind that the image must be reduced in size to be printed on a thermal printer
 

Star-Dust

Expert
Licensed User
Longtime User
in the first post it is explained how to do it
 

Star-Dust

Expert
Licensed User
Longtime User
WISH to be added if possible:
B4X:
PrintPDF(dir as string,filename as string)
Use this
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Yes, I'm using that method, but I am wishing to be integrated inside your great lib in your next update.
 

Star-Dust

Expert
Licensed User
Longtime User
HaH thank you for your clarification, I understood what your wish was.

This function in version b4a and b4j require external libraries that I do not own and cannot integrate into my own library.

Studying another system of mine to integrate the PDF inside would take a lot of time and increase costs. I prefer to suggest an external method and keep the cost low
 

RonkyOne

Member
Licensed User
Longtime User
Hi ,
when using the network printer,
and for some reason the connection with it is lost, any call returns this error and closes the app.
what do you recommend as a solution, like a call before writing?

or a way to trap the error...
Thank you



error:

java.lang.RuntimeException: java.net.SocketException: Socket is not connected
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:258)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.net.SocketException: Socket is not connected
at java.net.Socket.checkOpenAndCreate(Socket.java:671)
at java.net.Socket.getInputStream(Socket.java:361)
at anywheresoftware.b4a.objects.SocketWrapper.getInputStream(SocketWrapper.java:239)
at b4a.example.ble.lan_printer._lanconnection_connected(lan_printer.java:149)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
... 8 more
 

Star-Dust

Expert
Licensed User
Longtime User
Hi ,
when using the network printer,
and for some reason the connection with it is lost, any call returns this error and closes the app.
what do you recommend as a solution, like a call before writing?

or a way to trap the error...
Thank you



error:

java.lang.RuntimeException: java.net.SocketException: Socket is not connected
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:258)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.net.SocketException: Socket is not connected
at java.net.Socket.checkOpenAndCreate(Socket.java:671)
at java.net.Socket.getInputStream(Socket.java:361)
at anywheresoftware.b4a.objects.SocketWrapper.getInputStream(SocketWrapper.java:239)
at b4a.example.ble.lan_printer._lanconnection_connected(lan_printer.java:149)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
... 8 more
I imagine you lose the connection after sending and then hang around exceeding the timeout.

If so, every time you send something to the printer, if the next send takes longer than 10 seconds (in some cases even less) you should open the connection [eg. Printer.Connect(Host,Port) ] again and perhaps close it after printing [eg. Printer.Disconnect]
 
Top