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,392
Last edited:

timwil

Active Member
Licensed User
Longtime User
Hi

This program is meant to print the string of text contained in the PrintBuffer string variable to your blue tooth connected printer - it will then display a message box.

What is it that you are trying to do?

tim
 

jflaplante

Member
Licensed User
Longtime User
Hi,

I've been a web programmer since the mid 90' (when the definition didn't exist) and before that I was more into Dbase, Foxbase+ and Paradox programming. Strangely those are my first steps in basic !!!

I've come to B4A to develop a mobile invoicing/signature capture application for one of my clients on his delivery trucks. I haven't found any examples or tutorials that can help me so far so I'm patching the pieces together and decided to begin with printing.

First, let me thank you for all the information that you shared.

Second, While I still need to investigate/undertand a few things, I can confirm that this setup works with a Nexus 7 tablet and a Star Micronics WSP-i350 3 inches thermal printer.

Third, I have a question... Using your current solution, how would someone proceed to print a captured signature either in the form of a graphic file or a dataBLOB in the sqlite database?

Thanks in advance if you have any info to help me.

JF.
 

timwil

Active Member
Licensed User
Longtime User
Hi

I know what you mean about Dbase. I developed in dBase then Clipper for many years and only got into Basic with Visual Basic v6 when I was dragged kicking and screaming into Windows programming.

I have no problem assisting - that is what we are here for. The easiest way is to contact me via [email protected].

You said that you had the tablet printing to the printer. What are you looking to tackle next? SQL? Sockets? HTTP?

I have not printed any graphics yet but you should look up the printer documentation on how to print graphics. You are sending data directly to the printer so you would have total control over what is printed.

tim
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
it is also working on Bixolon SPP-R200 (samsung) printer
and from many phones with no problem
 

metzeller_2013

Member
Licensed User
Longtime User
Got it printing on a Datecs Dpp 250, Just a small text...

I am wondering if this is right, I will just add some ECS/POS codes to my print buffer with the data i am printing and the printer would apply the codes to the printout?? Is this correct? :sign0104:
 

metzeller_2013

Member
Licensed User
Longtime User
@ Sir Timwil
Can you pls post a sample code on how to send ECS/POS codes to the printer?
I have a hard time trying to figure this out. Everytime i send a code the printer just prints the code along with the text, no formatting of so whatever is happening. TIA
 

timwil

Active Member
Licensed User
Longtime User
Here is a PART of my POS program that shows how I use the control codes in printing my receipts

B4X:
Sub Process_Globals
   Dim aNormal As String
   Dim aBold As String
   Dim aDoubleWide As String
   Dim aDoubleHigh As String
   Dim aDoubleWideHigh As String
   Dim aUnderLine As String

   Dim bNormal As String
   Dim bBold As String
   Dim bDoubleWide As String
   Dim bDoubleHigh As String
   Dim bDoubleWideHigh As String
   Dim bUnderLine As String
End Sub


Sub Activity_Create(FirstTime As Boolean)
   aNormal = Chr(27) & "!" & Chr(0)
   aBold = Chr(27) & "!" & Chr(8)
   aDoubleHigh = Chr(27) & "!" & Chr(16)
   aDoubleWide = Chr(27) & "!" & Chr(32)
   aDoubleWideHigh = Chr(27) & "!" & Chr(48)

   bNormal = Chr(27) & "!" & Chr(1)
   bBold = Chr(27) & "!" & Chr(9)
   bDoubleHigh = Chr(27) & "!" & Chr(17)
   bDoubleWide = Chr(27) & "!" & Chr(33)
   bDoubleWideHigh = Chr(27) & "!" & Chr(49)
End Sub


Sub PrintReceipt
   DateTime.DateFormat = "dd-MMM-yyyy"
   DateTime.TimeFormat = "HH:mm:ss"

   If salPrint.Text = "PRINT" Then
      PrintBuffer = ""

      rs = db.ExecQuery("select * from System")
      If rs.RowCount > 0 Then
         rs.Position = 0

         PrintBuffer = PrintBuffer & Chr(27) & "a" & Chr(1) & CRLF
         PrintBuffer = PrintBuffer & aDoubleWideHigh & rs.GetString("Name").Trim & aNormal & CRLF
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("H1"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("H2"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("H3"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("H4"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("H5"))
         PrintBuffer = PrintBuffer & Chr(27) & "a" & Chr(0) & CRLF

         PrintBuffer = PrintBuffer & "********************************" & CRLF
         PrintBuffer = PrintBuffer & "Receipt: " & SysUserID & "-" & SysReceiptNo & CRLF
         PrintBuffer = PrintBuffer & "   Date: " & DateTime.Date(DateTime.Now) & CRLF
         PrintBuffer = PrintBuffer & "   Time: " & DateTime.Time(DateTime.Now) & CRLF
         PrintBuffer = PrintBuffer & " Device: " & SysDevice & CRLF
         PrintBuffer = PrintBuffer & "********************************" & CRLF

         For xc = 0 To CurrentSale.Size -1
            PrintBuffer = PrintBuffer & "  " & CurrentSale.Get(xc) & CRLF
         Next

         PrintBuffer = PrintBuffer & "********************************" & CRLF
         PrintBuffer = PrintBuffer & aDoubleWideHigh & " Total $" & udf.sNumber(mTotal,8) & aNormal & CRLF
         PrintBuffer = PrintBuffer & "********************************" & CRLF

         PrintBuffer = PrintBuffer & Chr(27) & "a" & Chr(1) & CRLF
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("F1"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("F2"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("F3"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("F4"))
         PrintBuffer = PrintBuffer & udf.OptionalString(rs.GetString("F5"))
         PrintBuffer = PrintBuffer & Chr(27) & "a" & Chr(0) & CRLF & CRLF & CRLF
      End If
      rs.Close

      StartPrinter

      SalInit
   Else
      salPrint.Text = "PRINT"
   End If
End Sub
 

aviario

Active Member
Licensed User
Longtime User
Hello timwil, you will not have an example to print a logo with esc codes

thanks
 

timwil

Active Member
Licensed User
Longtime User
I have never printed any graphics

I was supposed to get to it but I just have not yet

If you read thru the ESC POS codes you will see that it is there though
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi Timwil,
Please explain to me about these code belows. Where are they from? Thank you in advance.
B4X:
  aNormal = Chr(27) & "!" & Chr(0)
  aBold = Chr(27) & "!" & Chr(8)
  aDoubleHigh = Chr(27) & "!" & Chr(16)
  aDoubleWide = Chr(27) & "!" & Chr(32)
  aDoubleWideHigh = Chr(27) & "!" & Chr(48)

  bNormal = Chr(27) & "!" & Chr(1)
  bBold = Chr(27) & "!" & Chr(9)
  bDoubleHigh = Chr(27) & "!" & Chr(17)
  bDoubleWide = Chr(27) & "!" & Chr(33)
  bDoubleWideHigh = Chr(27) & "!" & Chr(49)
 

timwil

Active Member
Licensed User
Longtime User
In ESC/POS there are two fonts built into the printer (at least the printers I have been using). Font A and Font B.

<code>aNormal = Chr(27) & "!" & Chr(0)</code>

I am setting up a string that will set the printer to use Font A in the regular style - that is Not Bold and Not Italic and not Double Width and not Double Height

So if I send a ESC ! and chracter 0 the printer will be in Normal/Regular Font A type

<code>aBold = Chr(27) & "!" & Chr(8)</code>

To get Font A in Bold I send ESC ! character 8

To get Double High .... Double Wide ..... Double Wide and High

Then the next set of strings hold the control codes to select Font B in various styles

Any clearer now?
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi againTimwil,
I 've never seen about underline encoding. All of printer used as same as your encoding? or custom each ones?
 
Top