B4A Library SD: BT Printer (Bluetooth)

Star-Dust

Expert
Licensed User
(No WRAP, No Java Only B4A)

BT_Printer
library allows you to print text and image to a bluetooth thermal printers
(Herefor B4J, Here for B4i)

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)

Required: B4A 8.8+ and Serial Library 1.30+

To try print with the demo version 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.16
  • BT_Printer
    Note: Android 6+ add on manifest
    AddPermission(android.permission.ACCESS_COARSE_LOCATION)
    • Events:
      • BluetoothIsDisabled
      • ConnectedToPrint (Success As Boolean)
      • DisconnectToPrint (Mac As String)
      • 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
      • ChangeEncoding (EncodingType 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.
      • Preview As Bitmap
      • ResetPreViewWidth (NewWidth As Int) As String
      • SearchNewPrinter As String
        select --------------------------------------------
      • SelectFromAssociatedPrinter As String
      • SelectFromMac (Mac As String) As String
      • 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_DoubleWideHight As String
      • FontA_Normal As String
      • FontB_Bold As String
      • FontB_DoubleHight As String
      • FontB_DoubleWide As String
      • FontB_DoubleWideHight As String
      • FontB_Normal As String
      • InitializePrinter As String
    • Functions:
      • Process_Globals As String
  • Encoding
    Code module
    Subs in this code module will be accessible from all modules.
    • Fields:
      • ISO8859 As String
      • UTF8 As String
      • Windows1252 As String
      • Chinese As String
      • ChineseS As String
    • Functions:
      • Process_Globals As String
 

Attachments

Last edited:

Star-Dust

Expert
Licensed User
Sample:

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

Star-Dust

Expert
Licensed User

Star-Dust

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

Star-Dust

Expert
Licensed 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
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
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
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
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
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
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
HOW TO CHANGE THIS libary to USB PRINTER ?
 

Star-Dust

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