B4A Library SD: BT Printer Bluetooth

ANNOUNCEMENT
Starting today, this library will be abandoned and will no longer be distributed.
A new, more comprehensive Android library for ESC/POS thermal printers will be released. (BT, BLE, LAN, USB) (here
)



(No WRAP, No Java Only B4A)
BT_Printer
library allows you to print text and image to a
thermal printers (Bluetooth and BLE) with ESC/POS language
(For B4J versione see Here, for B4i versione see Here, for ZEBRA printer Here)

To print the QR CODES with your BT thermal printer you can create an image containing the QR code with the help of the @Erel library (here) or can use library of @Johan Schoeman (here)

WARNING: Make sure the library meets all your needs, is compatible with the printer you need to use.
To try print with the demo version (rel. 0.21) you can only use the FlushAndClose command which lasts 14 days and then disables.
The full version of the library will be issued to those making a donation (Contact me in private before making a donation). In the donation indicate the library you want and your NickName. Those requesting the full version will receive updates for 3 months.



Video1.gif
upload_2018-9-2_17-24-41.png



SD_BT_Printer

Author:
Star-Dust
Version: 0.21
  • BLE_Printer
    • Events:
      • 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
      • currentState As Int
      • ServiceName As String
    • Functions:
      • AddTab (ArrayTab As Int()) As String
        Add Tab
        eg. PrinterBLE.AddTab(Array As Byte(100,150,121))
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • Connect (ID As String) As String
      • Initialize (CallBack As Object, EventName As String, EncodingType 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
      • 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(Ecoding.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:
      • 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_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
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • ChangeEncoding (EncodingType As String) As String
      • Class_Globals As String
      • ClearBuffer As String
      • Close As String
      • Connected As Boolean
      • DefineImage (Image As Bitmap)
      • flushAllAndClose
      • Initialize (CallBack As Object, EventName As String, EncodingType 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
      • SearchNewPrinter As String
        select --------------------------------------------
      • SelectFromAssociatedPrinter As String
      • SelectFromMac (Mac As String) As String
      • SendBufferToPrinter
      • SetCodePage (Code As Byte) As String
        es. Printer.SetCodePage(06) for umlauts char
    • Properties:
      • CodeTable
        es. printer.setCodeTable(Ecoding.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
      • 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:
      • Process_Globals As String
  • Encoding
    Code module
    Subs in this code module will be accessible from all modules.
    • Fields:
      • Chinese As String
      • ChineseS 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_WPC1252 As Int
      • DOS_Latin_1 As String
      • IBM_PC As String
      • ISO8859 As String
      • UTF8 As String
      • Windows1252 As String
    • Functions:
      • Process_Globals As String
 

Attachments

  • Sample1.zip
    10.2 KB · Views: 2,061
  • sample_bt_ble.zip
    11.3 KB · Views: 596
  • SD_BT_Printer 0.21.zip
    21.2 KB · Views: 671
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Examples and documentation

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Stampa As BT_Printer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private Label1 As Label
    Private ListView1 As ListView
    Private RadioButton1 As RadioButton
    Private RadioButton2 As RadioButton
    Private RadioButton3 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("MAIN")
    ListView1.SingleLineLayout.Label.TextSize=14
    ListView1.SingleLineLayout.ItemHeight=30dip

    Stampa.Initialize(Me,"Stampa",Encoding.Windows1252)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    ListView1.Clear
    Stampa.SelectFromAssociatedPrinter
End Sub

Sub Button2_Click
    ListView1.Clear
    Stampa.SearchNewPrinter
End Sub

Sub Stampa_ListPrinterAssociated (ListNameAndMac As List)
    ListView1.Clear
    For Each BT As NameAndMac In ListNameAndMac
        ListView1.AddSingleLine2(BT.Name,BT.Mac)
    Next
End Sub

Sub Stampa_DiscoveryNewPrinter (Print As NameAndMac, DeviceClass As Int)
     Log("DevCl: " & DeviceClass)
     ListView1.AddSingleLine2(Print.Name,Print.Mac)
End Sub

Sub Stampa_DiscoveryFinished
    Log("Fine ricerca")
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    Stampa.SelectFromMac(Value)
End Sub

Sub Stampa_ConnectedToPrint
    Printer
End Sub

Sub Stampa_Send_Terminated
    Log("Finish!!")
    Stampa.Close
End Sub

Sub Printer
        Stampa.AddBuffer_Writeline(ESC_POS.BoldOn & "Hallo dear" & ESC_POS.DoubleOff)
        Stampa.AddBuffer_Writeline("How are you?")
        Stampa.AddBuffer_Writeline("Fine thank's")
        Stampa.AddBuffer_Bitmap(File.DirAssets,"qr.jpg",0)

        ' if AddBuffer_Bitmap don't work
        'Stampa.AddBuffer_Bitmap2(File.DirAssets,"qr.jpg")

        ' If you wont add a BarCode Image
        'Stampa.AddBuffer_BarCode("123456/A")
    End If

    Stampa.SendBufferToPrint
    'Stampa.flushAllAndClose
    Label1.Text = Stampa.Preview
End Sub

The library is equipped with a series of ESC commands, you can use them by listening to ESC_POS.

For example, if you would like to reset the printer:
B4X:
Printer.AddBuffer_ArrayByte(ESC_POS.InitializePrinter.GetBytes("UTF8"))
'or
Printer.AddBuffer_Write(ESC_POS.InitializePrinter)


Those who have a Chinese printer sometimes have difficulty printing the Euro (€) character. By setting the Code Page PC858 and Chinese (or simplified Chinese) Encoding you will be able to print it
B4X:
Printer.CodeTable=Encoding.Code_PC858
Printer.ChangeEncoding(Encoding.Chinese)
Printer.AddBuffer_WriteLine("10 €.")

How to get text alignment:
B4X:
'Right:
Printer.AddBuffer_Writeline(Chr(27) & Chr(97) & Chr(50))

'Center:
Printer.AddBuffer_Writeline(Chr(27) & Chr(97) & Chr(49))

'Left:
Printer.AddBuffer_Writeline(Chr(27) & Chr(97) & Chr(48))
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User

Star-Dust

Expert
Licensed User
Longtime User
For the moment I added a few commands. In the future I will put other commands if I can, and if I can also print BITMAP.

For now, you have to be satisfied with this :p
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

Star-Dust

Expert
Licensed User
Longtime User
In fact, I found this problem and I did not understand the reason. I followed other examples using TextWriter and TextReader.

Thanks for the tip. I update the source code.
 
Last edited:

MAGAREY

Member
Licensed User
Longtime User
When I try to run the example I get the following error
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
java.lang.NoSuchMethodError: No static method ToastMessageShow(Ljava/lang/String;Z)V in class Lanywheresoftware/b4a/keywords/Common; or its super classes (declaration of 'anywheresoftware.b4a.keywords.Common' appears in /data/app/b4a.example-1/base.apk)
    at b4a.example.bt_printer._initialize(bt_printer.java:424)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at b4a.example.main.afterFirstLayout(main.java:102)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6186)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
** Activity (main) Resume **
 

Star-Dust

Expert
Licensed User
Longtime User
The library is for b4A version 6.50 (or at most 6.80) because I use this version

Someone did me kindness to compile a version for B4A 7+
This version modifies a method: Send_Buffer_to_Print becomes Send_Buffer_to_Print_flush

you can also find an Library for 7+ and example in the attachment


Update for B4A 8 see post#1
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
You still have to thoroughly test the 2 methods for bitmap printing. They may not work properly.
Eventually let me know if it works for you
 

aviario

Active Member
Licensed User
Longtime User
Hi. first of all, sorry for my english.
We were testing the example in B4A 7.3 and it prints rare characters instead of the image.
 

MAGAREY

Member
Licensed User
Longtime User
Hi. first of all, sorry for my english.
We were testing the example in B4A 7.3 and it prints rare characters instead of the image.
this also happened to me, check the type of language your printer uses, and change it to esc/pos, my printer was a zebra and has by default the zpl lenguage
 

Star-Dust

Expert
Licensed User
Longtime User
At this time, the method for printing images was not finished. It is not included in the description. We will have to wait for an update.
For the moment the method does not work on all printers.
 

hears

Active Member
Licensed User
Longtime User
HOW TO CHANGE THIS libary to USB PRINTER ?
 

Star-Dust

Expert
Licensed User
Longtime User
Probably at the next update will be included the printing of images, and the QR and USB connection. For the moment the project is stopped because I'm working on other libraries and apps with higher priority

But if you have good will on this forum find the necessary to develop your own library,
There are some B4A developers who have already add print/pos with USB to App
 

Star-Dust

Expert
Licensed User
Longtime User
Update to version 0.05 at post#1 The AddBuffer_Bitmap function has been tested and is working on most printers. Alternatively you can use AddBuffer_Bitmap2
 
Top