Android Code Snippet Print to a Bluetooth POS-receipt printer with matching codepage

Original thread: https://www.b4x.com/android/forum/threads/bluetooth-printing-via-spp.17692/page-12

I simlified the above's example to print on a BT POS printer like this

Unbenannt.JPG


with the correct codepage to print special chars like "äöüßéè", etc. (codepage 1252).

Steps:

- switch on the printer
- switch on BT on your device
- pair your device with the printer (pw on my printer is 1234, take a look at the manual)
- wait 'till it's paired and see the printer's name
- insert the first unique chars in the code (the MAC address is retrieved via the printers name)
- run the app and print

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Bluetooth POS Printing
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim PrintBuffer As String

    Dim BtAdmin As BluetoothAdmin
    Dim BTConnection As Serial
    Dim Printer As TextWriter
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        BtAdmin.Initialize("BlueTooth")
        BTConnection.Initialize("Printer")
        Dim PairedDevices As Map
        PairedDevices = BTConnection.GetPairedDevices
        Dim l As List
        Dim DeviceName, MacAddress As String
        l.Initialize
        For i = 0 To PairedDevices.Size - 1 'Check all devices
            l.Add(PairedDevices.GetKeyAt(i))
            DeviceName=PairedDevices.Getkeyat(i)
            MacAddress=PairedDevices.GetValueAt(i)
            Log(DeviceName & " -> " & MacAddress)
            If DeviceName.Contains("Thermal") Then 'Insert the BT-Name of the printer or use the MAC address
                Exit
            End If
        Next
        BTConnection.Connect(MacAddress)
    End If
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause
   
End Sub

Sub Printer_Connected (Success As Boolean)
    If Success Then
        Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
        PrintBuffer=Chr(27)&"t"&Chr(16)&"Hello öäüßéèê" 'Set codepage 1252 
        Printer.WriteLine(PrintBuffer)
        Printer.Flush

        Msgbox("Printed!","")
        Printer.Close
        BTConnection.Disconnect 'disable this if you like
    Else
        Msgbox("No printer found...","Print error")
    End If
End Sub
 

KMatle

Expert
Licensed User
Longtime User
Nice is helpful, and have your a a solution for Image printing?

See here: https://www.b4x.com/android/forum/threads/send-bitmap-data-to-esc-pos-printer.73599/#post-467662 but I did not try so far.

What you can do is search Google for a VB(.net) example and port the code. I think there are tons evailable. Maybe I do a convert.

I don't know. It looks like a pretty good printer to me. o_O
(Hint: Maybe you should clarify what "POS Printer" stands for).

I added "receipt" to the title :) And yes, the printer (about 50€) has a good quality and it pairs very fast and reliable.
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Great for English ..

Arabic windows-1256 not working!
Also how to print right to left direction?
I want using your printing approach not another library.
 

scsjc

Well-Known Member
Licensed User
Longtime User
See here: https://www.b4x.com/android/forum/threads/send-bitmap-data-to-esc-pos-printer.73599/#post-467662 but I did not try so far.

What you can do is search Google for a VB(.net) example and port the code. I think there are tons evailable. Maybe I do a convert.



I added "receipt" to the title :) And yes, the printer (about 50€) has a good quality and it pairs very fast and reliable.

Hi,
i try to convert a code vb to print image... but can't have a solution... my sample code is https://www.b4x.com/android/forum/threads/print-image-on-bluetooth-printers.85345/ and only cant a print pixels on a bucle 0-255, i don't know if that can you help to find a print image ...
thanks
 

Eugene Puah

New Member
Licensed User
Please do answer with the default font. It is not needed to change the font. You can use bold for single words to mark them. But writing everything bold and italic only make the post more difficult to read...
Don,
Sorry about the bold fonts. I will pay attention to this next time.
Eugene Puah
 

joaomoraes83

Member
Licensed User
friends, how to change the font of a particular word in print?
I tried Dim cs As CSBuilder
Button1.Text = cs.Initialize.Size (50) .Append (Button1.Text) .PopAll
Did not work.
Any tips?
 

joaomoraes83

Member
Licensed User
Updating for those who need it:
B4X:
'active double pass (increase source)
Printer.WriteLine (Chr (27) & Chr (33) & Chr (32) & "")

'deactivates double pass
Printer.WriteLine (Chr (27) & Chr (33) & Chr (0) & "")
 
Last edited:

kilat kosasih

Member
Licensed User
Longtime User
Original thread: https://www.b4x.com/android/forum/threads/bluetooth-printing-via-spp.17692/page-12

I simlified the above's example to print on a BT POS printer like this

View attachment 53390

with the correct codepage to print special chars like "äöüßéè", etc. (codepage 1252).

Steps:

- switch on the printer
- switch on BT on your device
- pair your device with the printer (pw on my printer is 1234, take a look at the manual)
- wait 'till it's paired and see the printer's name
- insert the first unique chars in the code (the MAC address is retrieved via the printers name)
- run the app and print

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Bluetooth POS Printing
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim PrintBuffer As String

    Dim BtAdmin As BluetoothAdmin
    Dim BTConnection As Serial
    Dim Printer As TextWriter
End Sub

Sub Globals
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        BtAdmin.Initialize("BlueTooth")
        BTConnection.Initialize("Printer")
        Dim PairedDevices As Map
        PairedDevices = BTConnection.GetPairedDevices
        Dim l As List
        Dim DeviceName, MacAddress As String
        l.Initialize
        For i = 0 To PairedDevices.Size - 1 'Check all devices
            l.Add(PairedDevices.GetKeyAt(i))
            DeviceName=PairedDevices.Getkeyat(i)
            MacAddress=PairedDevices.GetValueAt(i)
            Log(DeviceName & " -> " & MacAddress)
            If DeviceName.Contains("Thermal") Then 'Insert the BT-Name of the printer or use the MAC address
                Exit
            End If
        Next
        BTConnection.Connect(MacAddress)
    End If
End Sub

Sub Activity_Resume
  
End Sub

Sub Activity_Pause
  
End Sub

Sub Printer_Connected (Success As Boolean)
    If Success Then
        Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
        PrintBuffer=Chr(27)&"t"&Chr(16)&"Hello öäüßéèê" 'Set codepage 1252
        Printer.WriteLine(PrintBuffer)
        Printer.Flush

        Msgbox("Printed!","")
        Printer.Close
        BTConnection.Disconnect 'disable this if you like
    Else
        Msgbox("No printer found...","Print error")
    End If
End Sub
Dear Sir

Thank you for the coding example In the program I created, when I ran the command "PairedDevices = BTConnection.GetPairedDevices"
the program immediately exited, there was no message.
Are there certain settings that must be installed?
Here is the beginning of my program:

#Region Module Attributes
#FullScreen: True
#IncludeTitle: True
#ApplicationLabel: RP Penjualan
#VersionCode: 1
#VersionName: satu
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#BridgeLogger: true
#End Region

Sub Process_Globals
Dim PrintBuffer As String
Dim BtAdmin As BluetoothAdmin
Dim BTConnection As Serial
Dim Printer As TextWriter
Dim DeviceName, MacAddress As String
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
BtAdmin.Initialize("BlueTooth")
BTConnection.Initialize("Printer")
Dim PairedDevices As Map
PairedDevices = BTConnection.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevices.Size - 1 'Check all devices
l.Add(PairedDevices.GetKeyAt(i))
DeviceName=PairedDevices.Getkeyat(i)
MacAddress=PairedDevices.GetValueAt(i)
Log(DeviceName & " -> " & MacAddress)
If DeviceName.Contains("RPP210A") Then
Exit
End If
Next
End If
End Sub

Thankyou
regards
kilat kosasih
 
Top