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

DonManfred

Expert
Licensed User
Longtime User
I updated the example at post # 1
Thank you. With this demos i can Print Text, Image and Mix.
Nice one. But unfortunately only a-zA-Z0-9. German "Umlauts" (äöüÄÖÜß) does not work with all three Encodings.

I´m pretty sure the problem is this f*** chinese Printer (grrr)...

But really nice library and examples! Good work.
 

Star-Dust

Expert
Licensed User
Longtime User
Thank you. With this demos i can Print Text, Image and Mix.
Nice one. But unfortunately only a-zA-Z0-9. German "Umlauts" (äöüÄÖÜß) does not work with all three Encodings.

I´m pretty sure the problem is this f*** chinese Printer (grrr)...

But really nice library and examples! Good work.
German characters can be set. Unfortunately, I do not have a bluetooth printer at the moment. I made the blind library (so to speak) but it works from 2017.

But next week I will get my first bluetooth printer. I will do some tests and add other functions not in mind including the one you need. And I also hope to make a version for IOS.
 

Star-Dust

Expert
Licensed User
Longtime User
Hi, what's the difference between sample1 and sample2?
No difference, I copied the same example twice.

However remember that for your printer my library is not good, because it is suitable for printed bluetooth, not dot matrix printers with bluetooth adapters.
A bluetooth receiver is not enough to make the bluetooth printer. In your case you need to develop a special library that transmits commands for the needle printer through the bluetooth.
 

DonManfred

Expert
Licensed User
Longtime User
To get german Umlauts working with my printer
B4X:
    ' Set Codepage to 6
    Printer.AddBuffer_ArrayByte(Array As Byte(0x1f,0x1b,0x1f,0xff,0x06))
    Printer.AddBuffer_Writeline("#0x06: äöüÄÖÜß")
    Printer.AddBuffer_Writeline("Umlaute Hähnchen, Brühe, Örtlichkeit, Süße ")
    Printer.AddBuffer_Writeline("äöüÄÖÜß")

WhatsApp Image 2019-03-02 at 19.01.42.jpeg

B4X:
    ' Underline 2px thick
    Printer.AddBuffer_ArrayByte(Array As Byte(0x1b,0x2d,0x02))
    Printer.AddBuffer_Writeline("Underline 2 TEST - Mixed Text and Image")
    ' Underline 1px thick
    Printer.AddBuffer_ArrayByte(Array As Byte(0x1b,0x2d,0x01))
    Printer.AddBuffer_Writeline("Underline 1 TEST - Mixed Text and Image")
    ' Underline 0px thick (off)
    Printer.AddBuffer_ArrayByte(Array As Byte(0x1b,0x2d,0x00))
    Printer.AddBuffer_Writeline("Underline 0 TEST - Mixed Text and Image")
 

Star-Dust

Expert
Licensed User
Longtime User
Good job. Thank you for your suggestion
 

vvg

Member
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.
Sir,
I have b4a Ver.8.50 & serial library Ver.1.31.
I tested your sample but while testing it in debug mode, it is throwing error & application stops. I have attached here the screenshot.
I observed that while executing line 72 i.e. "Printer.SearchNewPrinter", it gives error.
 

Attachments

  • Untitled.png
    Untitled.png
    134.5 KB · Views: 279

Star-Dust

Expert
Licensed User
Longtime User
Download the latest version of the library and try again. The problem should not arise
 

vvg

Member
Licensed User
Longtime User
Download the latest version of the library and try again. The problem should not arise
2
Ok. I tried. Now demo is working ok.
I will test it for my app and if any query will contact u..
Thanx
 

vecino

Well-Known Member
Licensed User
Longtime User
However remember that for your printer my library is not good, because it is suitable for printed bluetooth, not dot matrix printers with bluetooth adapters.
A bluetooth receiver is not enough to make the bluetooth printer. In your case you need to develop a special library that transmits commands for the needle printer through the bluetooth.

Hello, friend, really when putting an adapter, the printer will be bluetooth.
Another thing is to "get along" with the printer. But in this case it is compatible with epson esc/p, so there is no problem either.

Apparently the problem is not your library or other codes that I have tried. The problem is the tablet that I am using.
Finally I could do tests with other devices and all work well. (Always with this matrix printer and the adapter port serial 25 to bluetooth).

In the tablet that does not work, with Android 4.2 the problem is that it stays "locked" several seconds on each line it prints. This happens right in "Printer.flushAllAndClose".
In other codes that I have tried it happens in "Printer.Flush".
Other times, instead of "blocking", it shows the following message:
B4X:
main_imprimirtest (java line: 553)
java.io.IOException: [JSR82] write: write() failed.
   at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:702)
   at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
   at java.io_OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
   at java.io_OutputStreamWriter.flush(OutputStreamWriter.java:158)
   at java.io.BufferedWriter.flush(BufferedWriter.java:124)
   at anywheresoftware.b4a.objects.streams.File$TextWriterWrapper.Flush(File.java:602)
I have only done some tests and they have worked well, but I have not been able to implement it in my software because they have ordered me something else that has more priority, so I had to "park" the theme of the printer again.
Thank you very much for your help.
Regards.
 

vvg

Member
Licensed User
Longtime User
2
Ok. I tried. Now demo is working ok.
I will test it for my app and if any query will contact u..
Thanx
Hi..sir, I want to give regional font printing at point of sale.

using Serial library gives printing in english text but not in regional font as bluetooth printer does not support.

So I thought that printing screenshot or lables (with regional font characters on it) will be better choice.

I also thought that using your library with xui will give desired result.

I tested your library. It is ok.

But I want to print image. I mean I want to use your library for pos receipt printing with regional fonts in which i will print label (or labels) of different sizes and with texts.

So with the support of xui library, I took snapshot of label on which i managed to fetch local font data from database and want that label to be printed as it is.... as an image... so that i could print receipt in regional font.

I tried this with your QR code. I replaced your QR code image with my label of same size. It printed not accurate.. but not bad.... It was OK.. with some distorted characters.

But when tried to print snapshot of different sizes, it printed garbage.

So what to do for printing of snapshot as it is.... or printing labels of different sizes on receipt?

My receipt, either it will be screenshot of activity or

it will be having different fields (lables)

whichever will be easier.....

Will you please guide me for this???
 

Star-Dust

Expert
Licensed User
Longtime User
Hi..sir, I want to give regional font printing at point of sale.

using Serial library gives printing in english text but not in regional font as bluetooth printer does not support.

So I thought that printing screenshot or lables (with regional font characters on it) will be better choice.

I also thought that using your library with xui will give desired result.

I tested your library. It is ok.

But I want to print image. I mean I want to use your library for pos receipt printing with regional fonts in which i will print label (or labels) of different sizes and with texts.

So with the support of xui library, I took snapshot of label on which i managed to fetch local font data from database and want that label to be printed as it is.... as an image... so that i could print receipt in regional font.

I tried this with your QR code. I replaced your QR code image with my label of same size. It printed not accurate.. but not bad.... It was OK.. with some distorted characters.

But when tried to print snapshot of different sizes, it printed garbage.

So what to do for printing of snapshot as it is.... or printing labels of different sizes on receipt?

My receipt, either it will be screenshot of activity or

it will be having different fields (lables)

whichever will be easier.....

Will you please guide me for this???
Hi,
The images must be of a width space that does not exceed that of your printer, otherwise the commands are incorrectly translated and appear written in ASCII characters.

The images also do not have to be too big otherwise the bluetooth will not be able to transmit everything correctly. Then suggest images that represent one line of text at a time.

Unfortunately, you're trying to do something that has not been done by many at the moment, so you'll be the first to experiment.
 

vvg

Member
Licensed User
Longtime User
Hi,
The images must be of a width space that does not exceed that of your printer, otherwise the commands are incorrectly translated and appear written in ASCII characters.
Sir, I want to print indian language (devnagari) characters with blu

The images also do not have to be too big otherwise the bluetooth will not be able to transmit everything correctly. Then suggest images that represent one line of text at a time.

Unfortunately, you're trying to do something that has not been done by many at the moment, so you'll be the first to experiment.
Hi,
The images must be of a width space that does not exceed that of your printer, otherwise the commands are incorrectly translated and appear written in ASCII characters.

The images also do not have to be too big otherwise the bluetooth will not be able to transmit everything correctly. Then suggest images that represent one line of text at a time.

Unfortunately, you're trying to do something that has not been done by many at the moment, so you'll be the first to experiment.
Sir,
My main concern is to print in indian language fonts with bluetooth printer.
Thats is why i am trying all this.
Because so far from what I understood, I can not do this with text printing as it only supporting english and those languages which are supported in printer code pages.
Here in 'stackoverflow' post in which i found the suggestion for what to do...
Here is the sugesstion.....
----
"Printers have predefined character sets built in. These character sets are known as code pages.

Your Bluetooth printer, like most POS printers doesn't seem to support Unicode.

To solve this you could try converting your output to an image and send it to the printer as bytes using an emulation the printer understands.(PCL, ESC/POS, ZPL etc.)"

But i do not know how to do...because i do not know java, bytes arrey concepts etc..

Sir, as I tested, your library is worth to purchase but if you give me this solution, many other indian b4a users will also be benefited.
Kindly include above said functionality in your library.
 

Star-Dust

Expert
Licensed User
Longtime User
Sir,
My main concern is to print in indian language fonts with bluetooth printer.
Thats is why i am trying all this.
Because so far from what I understood, I can not do this with text printing as it only supporting english and those languages which are supported in printer code pages.
Here in 'stackoverflow' post in which i found the suggestion for what to do...
Here is the sugesstion.....
----
"Printers have predefined character sets built in. These character sets are known as code pages.

Your Bluetooth printer, like most POS printers doesn't seem to support Unicode.

To solve this you could try converting your output to an image and send it to the printer as bytes using an emulation the printer understands.(PCL, ESC/POS, ZPL etc.)"

But i do not know how to do...because i do not know java, bytes arrey concepts etc..

Sir, as I tested, your library is worth to purchase but if you give me this solution, many other indian b4a users will also be benefited.
Kindly include above said functionality in your library.
I'm sorry at the moment I can not help you I'm making several libraries. On the other hand I'm creating the iOS version of this library and it's very complex because it uses BLE.

At the moment only you have this need, although it is achievable, it would take too much time and I have no request (apart from you), it is not advantageous for me to neglect other projects for this.
 

DonManfred

Expert
Licensed User
Longtime User
Sir, as I tested, your library is worth to purchase but if you give me this solution, many other indian b4a users will also be benefited.
Kindly include above said functionality in your library.
There is no need to add anything to the Library.
Use an Canvas to draw your text on and create a snapshot from the Canvas. Print this snapshotimage then.
You don´t need java knowledge for this. Just B4A.
If you are not able to do it then you should create a new thread in the Questionsforum about it.
 

Star-Dust

Expert
Licensed User
Longtime User
There is no need to add anything to the Library.
Use an Canvas to draw your text on and create a snapshot from the Canvas. Print this snapshotimage then.
You don´t need java knowledge for this. Just B4A.
If you are not able to do it then you should create a new thread in the Questionsforum about it.
Already Erel has suggested him to use SnapShot views (Label) but fails. o_O

So I suppose the problem can be more complex, also because for several weeks fighting on it ... maybe it needs something ready that I can not do :p
 

vvg

Member
Licensed User
Longtime User
Already Erel has suggested him to use SnapShot views (Label) but fails. o_O

So I suppose the problem can be more complex, also because for several weeks fighting on it ... maybe it needs something ready that I can not do :p
 

vvg

Member
Licensed User
Longtime User
As suggested by many of you, don manfred, erel I successfully took Snapshot & image created.
But problem is about printing same image/lable on receipt.
I can not understand how to code to scale it so that it get printed well on my 58 mm Bluetooth printer?
 

vvg

Member
Licensed User
Longtime User
There is no need to add anything to the Library.
Use an Canvas to draw your text on and create a snapshot from the Canvas. Print this snapshotimage then.
You don´t need java knowledge for this. Just B4A.
If you are not able to do it then you should create a new thread in the Questionsforum about it.
I drew text. Took snapshot.
Then i created png file of the snapshot and tried to print using bt printer library by star dust. But it printed garbage characters or dark strips..
Upto snapshot, i hv done already. But printing part i can not understand. I am trying till now also. But also searching for wellworking ready code/library ...ready to donate...because it is taking too much time and efforts as I am unclear about some basic printing concepts. So far I had developed desktop applications with vb6 and easy reporting tool like crystal reports. From crystal reports printing part was always been much easier.
So here I m facing problems.
If you have time to do this job, please tell me how much I hv to donate because stardust is saying he is busy in another project.
 
Top