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,089
  • sample_bt_ble.zip
    11.3 KB · Views: 632
  • SD_BT_Printer 0.21.zip
    21.2 KB · Views: 710
Last edited:

kurniahenki

Member
Licensed User
Dear Sir

after i change the target sdk from 26 to 19 , it show the first time install
asking permission for Approximage location (network-based) and granted.

and my version android 6

it still same "Unfortunatelly, Bluetooth Printer Sample has stopped"
 

Star-Dust

Expert
Licensed User
Longtime User
You must not ask for any position.
By tomorrow I am attaching a simple example to try so we understand what happens in your devices.

Have you updated the serial library?

Star-Dust said:
Required: B4A 8+ and Serial Library 1.30+ (here)
 

kurniahenki

Member
Licensed User
i already download serial library from your link , and alread put in my basic4android libaries that used by b4a
and already serial version 1.31

and neither i select it or not select it still when compile to .apk and install and running the apps still same error

"Unfortunatelly, Bluetooth Printer Sample has stopped"

many thanks for your passion sir.

i really hope can start donate and using this library sir.


many thanks.
 

Star-Dust

Expert
Licensed User
Longtime User
i already download serial library from your link , and alread put in my basic4android libaries that used by b4a
and already serial version 1.31

and neither i select it or not select it still when compile to .apk and install and running the apps still same error

"Unfortunatelly, Bluetooth Printer Sample has stopped"

many thanks for your passion sir.

i really hope can start donate and using this library sir.


many thanks.
You do not have to select the Serial library, and it is important that it is up-to-date
I will give you another example that allows you to understand the error
 

kurniahenki

Member
Licensed User
sorry sir.

but when i showing error message on running release, the error happen when printer.initialize

the weird is when i running on debug mode it's normal no error.


thx
 

Star-Dust

Expert
Licensed User
Longtime User
try to intercept the error with this method
B4X:
    Try
        Printer.Initialize(Me,"Printer",Encoding.Windows1252)
    Catch
        Log("error: " & LastException.Message)
    End Try
 

kurniahenki

Member
Licensed User
try to intercept the error with this method
B4X:
    Try
        Printer.Initialize(Me,"Printer",Encoding.Windows1252)
    Catch
        Log("error: " & LastException.Message)
    End Try

Dear Sir

nothing happen and not showing error when release just suddenly message
""Unfortunatelly, Bluetooth Printer Sample has stopped""

for when debuging on logs :

Please update to B4A-Bridge v2.30+
Logger connected to: samsung SM-P355-359896061311831
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.DynamicBuilder.build(DynamicBuilder.java:21)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:349)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:454)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at b4a.example.main._activity_create(main.java:353)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.RuntimeException: Font asset not found b4x_fontawesome.otf
at android.graphics.Typeface.createFromAsset(Typeface.java:301)
at anywheresoftware.b4a.objects.TextViewWrapper.getTypeface(TextViewWrapper.java:155)
at anywheresoftware.b4a.objects.TextViewWrapper.build(TextViewWrapper.java:183)
at anywheresoftware.b4a.objects.ButtonWrapper.build(ButtonWrapper.java:98)
... 19 more
main_activity_create (java line: 353)
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:170)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at b4a.example.main._activity_create(main.java:353)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.ViewWrapper.innerInitialize(ViewWrapper.java:73)
at anywheresoftware.b4a.objects.ButtonWrapper.innerInitialize(ButtonWrapper.java:39)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:435)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:454)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
... 14 more
--------- beginning of crash
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.DynamicBuilder.build(DynamicBuilder.java:21)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:349)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:454)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at b4a.example.main._activity_create(main.java:353)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.RuntimeException: Font asset not found b4x_fontawesome.otf
at android.graphics.Typeface.createFromAsset(Typeface.java:301)
at anywheresoftware.b4a.objects.TextViewWrapper.getTypeface(TextViewWrapper.java:155)
at anywheresoftware.b4a.objects.TextViewWrapper.build(TextViewWrapper.java:183)
at anywheresoftware.b4a.objects.ButtonWrapper.build(ButtonWrapper.java:98)
... 19 more
main_activity_create (java line: 353)
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:170)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at b4a.example.main._activity_create(main.java:353)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.ViewWrapper.innerInitialize(ViewWrapper.java:73)
at anywheresoftware.b4a.objects.ButtonWrapper.innerInitialize(ButtonWrapper.java:39)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:435)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:454)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
... 14 more
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **


thx
 

Star-Dust

Expert
Licensed User
Longtime User
So the error is not in BT_Printer.Initialize.But rather it would appear in something that is in the Layout.

Try like this:
B4X:
Try
    Activity.LoadLayout("MAIN")
    ListView1.SingleLineLayout.Label.TextSize=14
    ListView1.SingleLineLayout.ItemHeight=30dip
  
    Printer.Initialize(Me,"Printer",Encoding.Windows1252)
    Preview.Color=Colors.LightGray
Catch
        Log("error: " & LastException.Message)
End Try
 

kurniahenki

Member
Licensed User
So the error is not in BT_Printer.Initialize.But rather it would appear in something that is in the Layout.

Try like this:
B4X:
Try
    Activity.LoadLayout("MAIN")
    ListView1.SingleLineLayout.Label.TextSize=14
    ListView1.SingleLineLayout.ItemHeight=30dip
 
    Printer.Initialize(Me,"Printer",Encoding.Windows1252)
    Preview.Color=Colors.LightGray
Catch
        Log("error: " & LastException.Message)
End Try

Dear Sir

Now the layout can show when running on release, but the problem is wherever i click any button or radiobutton it showing error application stopped and suddenly close the apps.

thx
 

Star-Dust

Expert
Licensed User
Longtime User
It is clear that you have misunderstood the problem.
You wrote that the release was interrupted, and you indicated Printer.Initialize.

Now you're saying that compiles and starts, but stops when you click a view in the Layout.

Actually the error that appears in the registers is not of my library but of the Layout.

Are you using an old version of B4A?

The strange thing that debugs you work ... so there is something that escapes.

The only problem, a could be access to File.DirInernal that my library uses in the DEMO version.

But tomorrow I can produce an example that can identify this problem. For today I can not give you any more help
 
Last edited:

kurniahenki

Member
Licensed User
Many Thanks Sir

my version B4A is 8.50 and registered version.

with my other coding is never happen problem with Activity.LoadLayout (when not using SD_BT_Printer), but i really like you library, it can help me much.

when i try to set like this :

Sub Activity_Create(FirstTime As Boolean)
Try
Activity.LoadLayout("MAIN")
Catch
Log("error on layout : " & LastException.Message)
End Try

Try
ListView1.SingleLineLayout.Label.TextSize=14
ListView1.SingleLineLayout.ItemHeight=30dip
Catch
Log("error on listiew : " & LastException.Message)
End Try

Try
Printer.Initialize(Me,"Printer",Encoding.Windows1252)
Preview.Color=Colors.LightGray
Catch
Log("error on printer : " & LastException.Message)
End Try
End Sub

everything work fine even with release, no error and all function can work fine.

I'm just curios why should be have to using try on Activity.LoadLayout.

I'm looking forward for your another example so can identify the problem.

One thing : each time i'm trying print barcode using your example for the first time success with barcode not completely print, and trying print barcode again it prints not barcode but weird code , and trying to print again back to first result barcode image not completely print.

for the text print is success with no error.

You Are The Best.

Many Thanks
 

kurniahenki

Member
Licensed User
Dear Sir

I'm really sorry , it's all my mistake, just found the cause of problem with activity.loadlayout,
it happens that buttonprint is using image and the file of image doesn't exists.

after i change btnprint without using image everything going normal.

the problem now is :
each time i'm trying print barcode using your example for the first time success with barcode not completely print, and trying print barcode again it prints not barcode but weird code , and trying to print again back to first result barcode image not completely print.

for the text print is success (perfect) with no error.

thx
 

Star-Dust

Expert
Licensed User
Longtime User
Dear Sir

I'm really sorry , it's all my mistake, just found the cause of problem with activity.loadlayout,
it happens that buttonprint is using image and the file of image doesn't exists.

after i change btnprint without using image everything going normal.
I asked you if I changed anything and you said no.

the problem now is :
each time i'm trying print barcode using your example for the first time success with barcode not completely print, and trying print barcode again it prints not barcode but weird code , and trying to print again back to first result barcode image not completely print.

for the text print is success (perfect) with no error.

thx
It could be too big and not be contained. Or if the transmission is interrupted too much.

Before printing an image, try to reset with ESC_POS.InitializePrinter
 

vecino

Well-Known Member
Licensed User
Longtime User
Hi, how can I try the demo?
I press the button on the printer and it shows a toastmessage saying that it is demo.
Thank you.
 

Star-Dust

Expert
Licensed User
Longtime User
Hi, how can I try the demo?
I press the button on the printer and it shows a toastmessage saying that it is demo.
Thank you.
The demo can only be tested for 7 days. You used it for too long.

Hi, I am testing your library and I see that there is no example Send_Buffer_to_Print_flush.
It's a mistake?
Regards.
In your message of June 19, 2018 tell me that you have tried the library.

So what other tests do you want to do now o_O ?
 

vecino

Well-Known Member
Licensed User
Longtime User
I did not get it to work in those days. I left it parked. Today I tried to try it again.
Thank you.
 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, really? Is that the control?
I tried to try it, it did not work and that's why I asked.
Now I said to myself: "I'm going to try again."
I downloaded the demo version and I have not let it prove it.
Anyway, thanks.
 

Star-Dust

Expert
Licensed User
Longtime User
I'm happy to present the update to version 0.15. I have also updated the examples to post#1

  1. Now the list of printers and MacAddr in a Map.
  2. The new version (FlushAndClose) will give you 14 days of trial from the installation.
  3. It is possible to store an image in the printer and recall it without having to transfer it every time.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
The new version will give you 14 days of trial from the installation.
How do i Print something with the connected Printer?

If i click on the Print Button i get a Toast saying "Demo Library - Sorry not available".

I downloaded the Library and the Demos a few minutes ago for the first time.

I am trying to maybe get my chinese Xprinter Printer to work with this lib.

Hard to test if i can not print in the Demo.
 

Star-Dust

Expert
Licensed User
Longtime User
How do i Print something with the connected Printer?

If i click on the Print Button i get a Toast saying "Demo Library - Sorry not available".

I downloaded the Library and the Demos a few minutes ago for the first time.

I am trying to maybe get my chinese Xprinter Printer to work with this lib.

Hard to test if i can not print in the Demo.
Sorry, there is an mistake in the example.
The DEMO version can only use the FlushAndClose command and not the SendBufferToPrinter command.

I updated the example at post # 1
 
Last edited:
Top