B4J Library jSD: Bluetooth Printer

BT_Printer library allows you to print text and image to a bluetooth thermal printers
It is similar to the BT_Printer library for Android (BLE_Printer for iOS), and implementations, events and methods are almost identical.
We are working to have the same library for all platforms. On iOS it will be different because it uses BLE, but we will try to standardize the use.

This version requires an activation code. If you do not have a code then it will work in DEMO mode and you will only be able to print every 4 minutes.

Dependence: jBluetooth

jSD_BT_Printer

Author:
Star-Dust
Version: 0.08
  • BT_Printer
    • Events:
      • BluetoothIsDisabled
      • ConnectedToPrint (Success As Boolean)
      • DataReceived (Buffer() As Byte)
      • DiscoveryComplete (Printers As Map)
      • DiscoveryFinished
      • DiscoveryNewPrinter (Name As String, MacAdress As String)
      • DiscoveryNoDeviceFound
      • ErrorDiscovery
      • SendingError (Mac As String)
    • Functions:
      • AddBuffer_ArrayByte (B As Byte()) As String
      • AddBuffer_BarCode (Code As String) As String
      • AddBuffer_Bitmap (Bmp As Image, ShiftLeft As Int) 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
      • CenterJustify As String
        after this command calls SendBufferToPrinter
      • Class_Globals As String
      • ClearBuffer As String
      • Close As String
      • Connected As Boolean
      • flushAllAndClose As String
      • Initialize (CallBack As Object, EventName As String, EncodingType As String, CodeActivation As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Verifica se l'oggetto sia stato inizializzato.
      • LeftJustify As String
        after this command calls SendBufferToPrinter
      • RightJustify As String
        after this command calls SendBufferToPrinter
      • SearchNewPrinter As String
      • SelectFromMac (Mac As String) As String
      • SendBufferToPrinter
    • Properties:
      • Spacing
        after setSpacing command calls SendBufferToPrinter
  • ESC_POS
    • 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
    • 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



Stampa.png
upload_2019-3-12_14-44-41.png
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Update rel 0.02

  • Now you can print images.

To get the activation code (to have the complete functions) contact me in private
 

vvg

Member
Licensed User
Longtime User
Update rel 0.02

  • Now you can print images.

To get the activation code (to have the complete functions) contact me in private

Is this the update of your library what i m trying for?
Can i use it for b4a?
If so, as you said how contact you privately?
Private message means private conversation in b4a community site or your email? Pl provide email if necessary to contact u on email.
 

Star-Dust

Expert
Licensed User
Longtime User
This section of the forum is dedicated to B4J
 

Star-Dust

Expert
Licensed User
Longtime User
Update rel 0.03

Inserted an example to print characters with another font using XUI.
B4X:
Printer.AddBuffer_Writeline("TEST PRINTER")
Printer.AddBuffer_Writeline("_____________")
Printer.SendBufferToPrinter
Printer.ClearBuffer
Printer.AddBuffer_Bitmap(CreateImage(Chr(0xF209),xui.CreateFontAwesome(60)),0)
Printer.AddBuffer_Bitmap(CreateImage(Chr(0xF17B),xui.CreateFontAwesome(60)),0)
Printer.SendBufferToPrinter


B4X:
Sub CreateImage(Text As String,Font As B4XFont) As Image
    Dim Can As B4XCanvas
    Dim V As B4XView = xui.CreatePanel("")
    V.SetLayoutAnimated(0,0,0,120,120)
 
    Can.Initialize(V)
    Can.DrawRect(Can.TargetRect,xui.Color_White,True,1)
    Can.DrawText(Text,Can.TargetRect.CenterX,Can.TargetRect.CenterY,Font,xui.Color_Black,"CENTER")
    Can.Invalidate
    Return Can.CreateBitmap
End Sub

RESULT
OtherFont.png
 

Star-Dust

Expert
Licensed User
Longtime User
Update rel 0.04
 

Bob Spielen

Active Member
Licensed User
Helo SD, I have a BlueBamboo Model P25-M printer. Executing the example it crashes.
When I comment the lines where the example Adds a BitMap it works fine. What could it be that
the printer does not print the images?
Thanks in advance for you advice....
 

Star-Dust

Expert
Licensed User
Longtime User
It would be helpful for me to see the error message
 

Bob Spielen

Active Member
Licensed User
To speed up processing I just placed the command: Printer.SelectFromMac("00:08:1B:95:42:BD") in B4A and the flow goes to Printer_ConnectedToPrint (Success As Boolean) as true.
The question here is related to B4J, where (Printer.SelectFromMac("00081B9542BD")) it doesn't work, I have to search first. Is it possible to connect directly without have to search the device?
 

Star-Dust

Expert
Licensed User
Longtime User
To speed up processing I just placed the command: Printer.SelectFromMac("00:08:1B:95:42:BD") in B4A and the flow goes to Printer_ConnectedToPrint (Success As Boolean) as true.
The question here is related to B4J, where (Printer.SelectFromMac("00081B9542BD")) it doesn't work, I have to search first. Is it possible to connect directly without have to search the device?
Make sure the string that the Mac contains is correct and formatted correctly. I see you didn't put a colon in B4J.

However I will run tests to verify.
 

Star-Dust

Expert
Licensed User
Longtime User
Update 0.06
  • Added DiscoveryComplete (Printers As Map)
  • Fix bug
As in the Android version, DiscoveryComplete has been added which will make DiscoveryNewPrinter, DiscoveryFinished and DiscoveryNoDeviceFound unnecessary.

After the search, you can only wait for DiscoveryComplete and it will return a map with the printer name as keys and MacAddr as the value. If the Map is empty it will not have found devices
B4X:
Printer.SearchNewPrinter
 
Wait For Printer_DiscoveryComplete(Printers As Map)
For Each Name As String In Printers.Keys
        Log($"${Name}: ${Printers.Get(Name)}"$)
Next
 

Star-Dust

Expert
Licensed User
Longtime User
To speed up processing I just placed the command: Printer.SelectFromMac("00:08:1B:95:42:BD") in B4A and the flow goes to Printer_ConnectedToPrint (Success As Boolean) as true.
The question here is related to B4J, where (Printer.SelectFromMac("00081B9542BD")) it doesn't work, I have to search first. Is it possible to connect directly without have to search the device?
It appears that a scan needs to be done before connecting to the printer.

I solved the problem by keeping the MAC address of the printer.

Whenever I need to print I start scanning silently and as soon as it finds (with DiscoveryNewPrinter ) the printer that I have stored on the MAC, I start printing.

The whole is invisible to the end user
 

Star-Dust

Expert
Licensed User
Longtime User
Update ver. 0.07
  • fix bugs
  • Added justification functions
 

Star-Dust

Expert
Licensed User
Longtime User
ANNOUNCEMENT
I have completed the tests for USB printing and it seems to work.
So we would have available as well as Bluetooth SPP also the USB connection (for printers installed with their own driver)
 

walterf25

Expert
Licensed User
Longtime User
How can I get the full library, I have the need to use it with a thermal printer, I tried your demo library and it seems to connect fine but when I try to print I get the following...
Waiting for debugger to connect...
Program started.
BlueCove version 2.1.1-SNAPSHOT on winsock
Searching for devices...
Trying to connect to: (B85044044EBA)
Demo library - Sorry not available

Please let me know, Thanks,
Walter
 

Star-Dust

Expert
Licensed User
Longtime User
it will work in DEMO mode and you will only be able to print every 4 minutes
 

walterf25

Expert
Licensed User
Longtime User
it will work in DEMO mode and you will only be able to print every 4 minutes
Strange, I am trying to print a barcode using a NETUM G5 thermal printer, it supports ESC/POS protocol, but for some reason is not working, do you have any examples on how to print a 1D barcode?

Walter
 

Star-Dust

Expert
Licensed User
Longtime User
For the barcode each printer may have a different command. Have you read the manual? what commands does it provide?

Some printers do not print the barcode
 

walterf25

Expert
Licensed User
Longtime User
For the barcode each printer may have a different command. Have you read the manual? what commands does it provide?

Some printers do not print the barcode
I'm sure this one prints barcodes, I bought specifically for this, and yes I have read the manual, but the manual does not provide a list of commands, all I know is that it supports ESC/POS commands, it comes with an app which I have used and am able to print, but I need to integrate this into a ABMaterial app that I have developed, so far I am able to connect to the printer via bluetooth just fine, but not able to print anything yet. By the way just came across this class written by @Graham, and it seems to work however the barcode printing doesn't seem to print anything, it does feed the paper but does not print anyting.
https://www.b4x.com/android/forum/threads/bluetooth-esc-pos-printer-class.106553/#content

Fyi, this is the printer

Walter
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
For the barcode each printer may have a different command. Have you read the manual? what commands does it provide?

Some printers do not print the barcode
An ESC/POS printer usually also supports printing an image. Perhaps it is more convenient to convert your 1D label into an image. Then you have full control over the dimensions and centering of your 1D label and creating and printing of an image is much simpler using a library.
 
Top