Android Tutorial BlueTooth Printing via SPP

I finally have reliable, stable printing to a blue tooth printer (Citizen CMP-10 and CMP-20) from my Samsung 5570L Galaxy Mini Android Device running v2.3.6

Attached is a skeleton project (based on the Serial example posted here) in Basic4Android v1.8 that requires the following libraries

Core v1.77
Serial v1.20
ToggleLibrary 1.00

It may work with other versions but this is what works for me in my specific situation


To print you must first pair the phone/device to the printer

In the program you must place what you wish to print in PrintBuffer (string) variable and call StartPrinter (sub)

If the printer is not turned on it returns an error message and allows you to reprint

Hope this helps someone :)

Tim
 

Attachments

  • BTPrint.zip
    8.7 KB · Views: 6,375
Last edited:

timwil

Active Member
Licensed User
Longtime User
I have not tried it - have not needed to - text only

But follow the ESC/POS specs - it will get you there
 

KMatle

Expert
Licensed User
Longtime User
Hope this helps someone

Thank's for that. Your example code is missing the connect-statement. I've added some lines to select the printer by it's mac address. Maybe you want to update your example.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        btAdmin.Initialize("BlueTeeth")
        cmp20.Initialize("Printer")
        Dim PairedDevices As Map
        PairedDevices = cmp20.GetPairedDevices
        Dim l As List
        l.Initialize
        For i = 0 To PairedDevices.Size - 1
            l.Add(PairedDevices.GetKeyAt(i))
        Next
        Dim res As Int
        res = InputList(l, "Choose device", -1) 'show list with paired devices
        If res <> DialogResponse.CANCEL Then
            cmp20.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address and connect
        End If
    End If
End Sub
 

timwil

Active Member
Licensed User
Longtime User
I don't understand

The StartPrinter Sub contains the connect code....
 

Pahlevi

New Member
Licensed User
Hi Timwil i'm newbie in b4A, hope u can solve my issue, i add some code for test the print

Sub Activity_Resume
Toggla.TurnBluetoothOn
PrintBuffer = "test Print" <---- here
StartPrinter
End Sub

Bluetooth On succes
Printer already paired
but always say "Printer Error" and reprint or cancel


Here printer spec "Branch Kozure BP-806
  • Thermal
  • speed : 80 mm/s
  • width : 48 mm
  • char/line : 24/48, 36/72, 12/24, 8/16
  • support icon from 8178 and char china
  • Control Command : General ESC/POS print instruction set
  • Supporting Characters : Reverse, left and right rotation, highlight, underline on the left and right, dash play, line spacing
  • Type papper : Thermal, 58 mm, diameter 40 mm
  • connection : USB/bluetooth/serial 232 port
  • Support handphone Android via bluetooth
  • Support Windows : XP/Win8/Win 10 32 bits and 64 bits
  • Power : 9 V 1.5A
 

Pahlevi

New Member
Licensed User
Hi Timwil i'm newbie in b4A, hope u can solve my issue, i add some code for test the print

Sub Activity_Resume
Toggla.TurnBluetoothOn
PrintBuffer = "test Print" <---- here
StartPrinter
End Sub

Bluetooth On succes
Printer already paired
but always say "Printer Error" and reprint or cancel


Here printer spec "Branch Kozure BP-806
  • Thermal
  • speed : 80 mm/s
  • width : 48 mm
  • char/line : 24/48, 36/72, 12/24, 8/16
  • support icon from 8178 and char china
  • Control Command : General ESC/POS print instruction set
  • Supporting Characters : Reverse, left and right rotation, highlight, underline on the left and right, dash play, line spacing
  • Type papper : Thermal, 58 mm, diameter 40 mm
  • connection : USB/bluetooth/serial 232 port
  • Support handphone Android via bluetooth
  • Support Windows : XP/Win8/Win 10 32 bits and 64 bits
  • Power : 9 V 1.5A


I change my phone from samsung N915 to n5100 and its work .. :)
 

timwil

Active Member
Licensed User
Longtime User
I have found that there is a difference with the connect parameters under some conditions so the difference might be the actual phone bluetooth implementation but happy you got it to work
 

Pahlevi

New Member
Licensed User
hi All how to initialized after print ?..
here my code


Sub btn2_Click
PrintReceipt
StartPrinter
cmp20.Connect(PairedDevicesX.Get(lX.Get(resX)))
ToastMessageShow("Print Success",True)

'init
PrintBuffer = " " <=== without this success
edttext1.Text = " " <=== without this success
End Sub


before add code "init" print success without issue, but i need to initialize for the next print, after that they only print blank screen..
 

luke2012

Well-Known Member
Licensed User
Longtime User
I'm approaching a ESC/POS commands porting from 58mm printer to 80mm printer. Any suggestion about this ? :)
 

timwil

Active Member
Licensed User
Longtime User
hi All how to initialized after print ?..
here my code


Sub btn2_Click
PrintReceipt
StartPrinter
cmp20.Connect(PairedDevicesX.Get(lX.Get(resX)))
ToastMessageShow("Print Success",True)

'init
PrintBuffer = " " <=== without this success
edttext1.Text = " " <=== without this success
End Sub


before add code "init" print success without issue, but i need to initialize for the next print, after that they only print blank screen..


I don't understand what you are asking
 

luke2012

Well-Known Member
Licensed User
Longtime User
The same commands work on a 58mm Printer AND 80mm printer. I have used both successfully.

Yes. I agree with you.
My users says that the font is too small on the 80mm paper. This is because I use the same code that print on a 58mm printer.

So my target is to make the font bigger and more readable on a 80mm paper. So probably I have to find a command that set the print mode / font type on my 80mm printer. Within the printer manual I found this command:


ESC M n
[Name] Select character font
[Format] ASCII ESC M n
Hex 1B 4D n
Decimal 27 77 n
[Range] n = 0, 1, 48, 49
[Description] Selects character fonts.

n
0,48 Character Font A (12x24) selected
1,49 Character Font B (9x17) selected

I guess that I have to set the Character Font A to make the Character bigger and more readable on a 80mm paper...
 
B4X:
Sub PrintBarCode(PrintData As String, ShowNumbers As Boolean)
   PrintBuffer.Append(Chr(29) & "h" & Chr(50) & CRLF)
   PrintBuffer.Append(Chr(29) & "w" & Chr(2) & CRLF)

   If ShowNumbers Then
      PrintBuffer.Append(Chr(29) & "H" & "2" & CRLF)
      PrintBuffer.Append(Chr(29) & "f" & "0" & CRLF)
   Else
      PrintBuffer.Append(Chr(29) & "H" & "0" & CRLF)
   End If

   PrintBuffer.Append(Chr(29) & "k" & Chr(4) & PrintData & Chr(0) & CRLF)
End Sub

unknown member: append
How to fix this?
 

luke2012

Well-Known Member
Licensed User
Longtime User
Yes the printers usually have two built in fonts - you have to use the larger one

Strange :) I'm trying to set the command (ESC M n) to set the character font within the (working) print buffer string but nothing change. The printed character type remain the same (same font and same size).

Probably I have check better the printer manual...
 

timwil

Active Member
Licensed User
Longtime User
Strange :) I'm trying to set the command (ESC M n) to set the character font within the (working) print buffer string but nothing change. The printed character type remain the same (same font and same size).

Probably I have check better the printer manual...

Let me re-phrase - the printer "USUALLY" has two built in fonts
 
Top