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:

luke2012

Well-Known Member
Licensed User
Longtime User
Thanks @timwil :)
I bought a RPP-02N for dev & test purpose.
The RPP-02N has a wireless range of 10 mt (Bluetooth 2.0)

There is also the RPP-200 with Bluetooth 4.0. This mean that the wireless range of RPP-200 could be greater than the RPP-02N wireless range?

@timwil Jeff told me that the wireless range of RPP-200 with Bluetooth 4.0 is 10 mt.
So if I need a greater range I have to switch on a WiFi model. Your sample works on bluetooth only right ?
 

mokrokuce

Member
Licensed User
Longtime User
Hello Timwill, first of all: thanks for the example project, I can confim it works with Xperia Arc and noname bluetooth printer i got from eBay (I can post link, but you can see the model from the pictures attached).

I have a problem printing Latin2 extended characters. I got from wikipedia (https://en.wikipedia.org/wiki/ISO/IEC_8859-2) decimal codes for these extended characters and I try sending them to the printer, but I get some unwanted characters.

I have modified your code like this:
B4X:
Sub Globals
    'Dim TogglA As Toggle
    Dim txt1 As EditText
    Dim btn1 As Button
    Dim lbl1 As Label
    Dim lst1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)

txt1.Initialize("txt1")
btn1.Initialize("btn1")
lbl1.Initialize("")
lst1.Initialize("lst1")

For i=30 To 900
    lst1.AddTwoLines2(Chr(i),i,i)
Next
lbl1.TextSize=22
btn1.Text="print"
txt1.InputType=txt1.INPUT_TYPE_PHONE
Activity.AddView(lbl1,0,0,100%x,60dip)
Activity.AddView(txt1,0,60dip,100%x,60dip)
Activity.AddView(btn1,0,120dip,100%x,60dip)
Activity.AddView(lst1,0,180dip,100%x,100%y-240dip)

    If FirstTime Then
     
        btAdmin.Initialize("BlueTeeth")
        cmp20.Initialize("Printer")
    End If
    'StartPrinter
End Sub

...and added some new code:
B4X:
Sub txt1_TextChanged (Old As String, New As String)
If New<>"" Then
    lbl1.Text=Chr(New)
End If
End Sub
Sub btn1_click
    PrintBuffer="ASCII (" & txt1.Text & "):" & Chr(txt1.Text) & CRLF
    'PrintBuffer=PrintBuffer & "StringGetBytes (šđčćž):" & "šđčćž".GetBytes("UTF8")
    StartPrinter
End Sub
Sub lst1_ItemClick (Position As Int, Value As Object)
    txt1.Text=Value
End Sub

This way I see all ASCII codes from 30 to 900 and when I select code 208 (see image "screenshot") I see the character displaying as I want it in the app. This character I can see in the test page printed by the printer (see image "testpage") and I am sure I have set the printer to the correct codepage (see image "settings").
When I click "Print" button, the printer prints out what is in image "print".

Can you or someone point me in the right direction here?

I don't know how to send these extended characters to the printer, and I am sure the printer can print these characters (as seen in image "testpage").
 

Attachments

  • screenshot.png
    screenshot.png
    23.5 KB · Views: 605
  • testpage.png
    testpage.png
    396.6 KB · Views: 806
  • settings.png
    settings.png
    465.5 KB · Views: 756
  • print.png
    print.png
    430.5 KB · Views: 740

luke2012

Well-Known Member
Licensed User
Longtime User
Hello Timwill, first of all: thanks for the example project, I can confim it works with Xperia Arc and noname bluetooth printer i got from eBay (I can post link, but you can see the model from the pictures attached).

I have a problem printing Latin2 extended characters. I got from wikipedia (https://en.wikipedia.org/wiki/ISO/IEC_8859-2) decimal codes for these extended characters and I try sending them to the printer, but I get some unwanted characters.

I have modified your code like this:
B4X:
Sub Globals
    'Dim TogglA As Toggle
    Dim txt1 As EditText
    Dim btn1 As Button
    Dim lbl1 As Label
    Dim lst1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)

txt1.Initialize("txt1")
btn1.Initialize("btn1")
lbl1.Initialize("")
lst1.Initialize("lst1")

For i=30 To 900
    lst1.AddTwoLines2(Chr(i),i,i)
Next
lbl1.TextSize=22
btn1.Text="print"
txt1.InputType=txt1.INPUT_TYPE_PHONE
Activity.AddView(lbl1,0,0,100%x,60dip)
Activity.AddView(txt1,0,60dip,100%x,60dip)
Activity.AddView(btn1,0,120dip,100%x,60dip)
Activity.AddView(lst1,0,180dip,100%x,100%y-240dip)

    If FirstTime Then
    
        btAdmin.Initialize("BlueTeeth")
        cmp20.Initialize("Printer")
    End If
    'StartPrinter
End Sub

...and added some new code:
B4X:
Sub txt1_TextChanged (Old As String, New As String)
If New<>"" Then
    lbl1.Text=Chr(New)
End If
End Sub
Sub btn1_click
    PrintBuffer="ASCII (" & txt1.Text & "):" & Chr(txt1.Text) & CRLF
    'PrintBuffer=PrintBuffer & "StringGetBytes (šđčćž):" & "šđčćž".GetBytes("UTF8")
    StartPrinter
End Sub
Sub lst1_ItemClick (Position As Int, Value As Object)
    txt1.Text=Value
End Sub

This way I see all ASCII codes from 30 to 900 and when I select code 208 (see image "screenshot") I see the character displaying as I want it in the app. This character I can see in the test page printed by the printer (see image "testpage") and I am sure I have set the printer to the correct codepage (see image "settings").
When I click "Print" button, the printer prints out what is in image "print".

Can you or someone point me in the right direction here?

I don't know how to send these extended characters to the printer, and I am sure the printer can print these characters (as seen in image "testpage").

I have the same problem scending italian characters
 

luke2012

Well-Known Member
Licensed User
Longtime User
Someone known how to send a Latin-1 (e grave accent) char ("è") to a ESC / POS printer using a ISO-8859-1 code page?
 

luke2012

Well-Known Member
Licensed User
Longtime User
send hex E7 char

First of all thanks for your reply @tigrot :)

The "è" char is filled by the user and it's could be within the PrintBuffer string.
So I have to do something like this:

B4X:
PrintBuffer.Replace("è", chr(<code>))

The chr function accept a unicode value (as int) as input parameter.

I found this conversion table for "è" char: https://en.wikipedia.org/wiki/È
If I understood well the table, I have to send chr(232)

P.S.
Is there a B4A function that accept Hex code ?
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
No I didn't.
The only thing that was set by the supplier is the code page ISO-8859-1.

In my case the question is: "How to set the language on the RPP02N printer?"

I try to check the manual, any suggestion will be apreciated :)
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
Within my printer commands manual I found a table that allow to select the international character set (Default character set is 0 ---> U.S.A.)

The command to select the character set for Italy is: ESC R 6
The decimal command format is: 27 82 6

So the piece of code that send the command to the printer should be:

B4X:
Dim printer As TextWriter
Dim LangIT As String
LangIT =  Chr(27) & "!" & Chr(82) & "!" & 6

printer.WriteLine(LangIT)

Is it right?
 
Last edited:

timwil

Active Member
Licensed User
Longtime User
Did you pair the printer with the Android device?

Do you have any other devices paired with your Android device?

I does not say that it supports Epson ESC/POS - it just says "Escape Command compatibility" - I am not sure what that means

Did you try the sample app for Android that came with the printer? To see if it prints from that?
 

timwil

Active Member
Licensed User
Longtime User
Within my printer commands manual I found a table that allow to select the international character set (Default character set is 0 ---> U.S.A.)

The command to select the character set for Italy is: ESC R 6
The decimal command format is: 27 82 6

So the piece of code that send the command to the printer should be:

B4X:
Dim printer As TextWriter
Dim LangIT As String
LangIT =  Chr(27) & "!" & Chr(82) & "!" & 6

printer.WriteLine(LangIT)

Is it right?


I guess - I have never used that....try it see if it works
 

luke2012

Well-Known Member
Licensed User
Longtime User
I guess - I have never used that....try it see if it works

@timwil thanks for your suggestion.
What about sending commands to a wifi thermal printer (ESC / POS) ? If I understood well the object to use is AsyncStreamsObject to send commands string to the printer...
 

Juanll2003uy

Member
Licensed User
Longtime User
Hi, im printing in a APEX3 BT with the code of this post and it works fine.

I need to print a QR code, in the manual says that i need to send this code for example:

<ESC> ‘Z’ ‘7’ ‘2’ ‘M’ ‘A’ <NUL> <FS> ‘2’ ‘h’ ‘t’ ‘t’ ‘p’ ‘:’ ‘/’ ‘/’ ‘w’ ‘w’ ‘w’ ‘.’ ‘d’ ‘a’ ‘t’ ‘a’ ‘m’ ‘a’ ‘x’ ‘–‘ ‘o’ ‘n’ ‘e’ ‘i’ ‘l’ ‘.’ ‘c’ ‘o’ ‘m’

Can you help with this in B4a code?

thanks!
 

Juanll2003uy

Member
Licensed User
Longtime User
I need to export that esc/pos code to b4a code, with this it works!

The problem is that i dont know write it in b4a code :|
 

timwil

Active Member
Licensed User
Longtime User
from my example I use a string to hold the data to be sent to the printer

so PrintBuffer = PrintBuffer & chr(27) will add <ESC> to the string


so from your example: <ESC> ‘Z’ ‘7’ ‘2’ ‘M’ ‘A’ <NUL>

you should:

PrintBuffer = PrintBuffer & chr(27) & "Z72MA" & chr(0)
 
Top