Android Question how to Auto-cut thermal receipt in a printer Bluetooth? (Solved)

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi, my new printer Bluetooth just arrived this morning
it works great using the codes here and here

question: Does anyone know how to setup the autocut (autocut the paper)?
this is the printer, I thought it should be automatic

thanks
 
Solution
B4X:
...
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)
        PrintBuffer=Chr(29) & Chr(86) & Chr(48)
        Printer.WriteLine(PrintBuffer) '<-------- YOU FORGET THIS
        Printer.Flush 'limpia la impresora
        Msgbox("Printed!","")
        Printer.Close
        BTConnection.Disconnect 'disable this if you like
    Else
.....

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The class DataForSendToPrinterTSC inside the jar has a Method "cut" which returns the cutcommand ....
B4X:
public static byte[] cut() {
        return strTobytes("CUT\n");
    }
So; if you already have it running somehow you can send the cut command by yourself... The command (as bytearray) must be "CUT\n"
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
ah ah it looks to advanced for me :(
how add your code if I'm using the demo test:
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
   
    Private NativeMe As JavaObject
End Sub

Sub Globals
  
End Sub


Sub Activity_Create(FirstTime As Boolean)
   
'    If FirstTime Then
'        NativeMe.InitializeContext
'    End If
'    Dim s As String = NativeMe.RunMethod("cut", Null)
'    Log(s)
   
   
    If FirstTime Then
        BtAdmin.Initialize("BlueTooth")
        BTConnection.Initialize("Printer")
        NativeMe.InitializeContext
       
        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("Printer001") 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
        NativeMe.InitializeContext
        Dim s As String = NativeMe.RunMethod("cut", Null)
        Log(s)
       

'        Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
'        PrintBuffer=Chr(27)&"t"&Chr(16)&"Hello world" '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

#If JAVA
public static byte[] cut() {
        return strTobytes("CUT\n");
    }                                   
#End If
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Is it not something like this?

B4X:
PrintBuffer="CUT\n"
Printer.WriteLine(PrintBuffer)
Printer.Flush
it is just printing "CUT\n"

from the website of the printer (in the .rar file android SDK),
any idea how to use the commands below
Note: the printer is 80mm autocut paper
  • selectCutPagerModerAndCutPager
    public static byte[] selectCutPagerModerAndCutPager(int m)
    select the model of cutpage and cut the pager GS V m
    Parameters:
    m:0,48,all - cut;1,49,half cut
  • selectCutPagerModerAndCutPager
    public static byte[] selectCutPagerModerAndCutPager(int m,
    int n)
    select cut paper model and cut paper GS V m n
    Parameters:
    m:66 -
    n:feed - n,half cut paper
 
Last edited:
Upvote 0

eps

Expert
Licensed User
Longtime User
Doh!

I guess there isn't a Printer.Cut command?

As DonManfred mentioned you need to do this "The command (as bytearray) must be "CUT\n""

So you need to send that as a byte array not string (sorry)
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
ok, I think..I'm not understanding how to do it :(
this is the code
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
  
    Private NativeMe As JavaObject
End Sub

Sub Globals
 
End Sub


Sub Activity_Create(FirstTime As Boolean)
  
'    If FirstTime Then
'        NativeMe.InitializeContext
'    End If
'    Dim s As String = NativeMe.RunMethod("cut", Null)
'    Log(s)
  
  
    If FirstTime Then
        BtAdmin.Initialize("BlueTooth")
        BTConnection.Initialize("Printer")
        NativeMe.InitializeContext
      
        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("Printer001") 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
        NativeMe.InitializeContext
        Dim s As String = NativeMe.RunMethod("cut", Null)
        'Dim s As strTobytes = NativeMe.RunMethod("cut", Null)
        'Dim s As Byte = NativeMe.RunMethod("cut", Null)
        Log(s)
      

        Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
        PrintBuffer=Chr(27)&"t"&Chr(16)&"Hello world" '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

#If JAVA
public static byte[] cut() {
        return strTobytes("CUT\n");
    }                                  
#End If

I have this error
B4X:
src\b4a\example\main.java:569: error: cannot find symbol
        return strTobytes("CUT\n");
               ^
  symbol:   method strTobytes(String)
  location: class main
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Try sending either

B4X:
Chr(29) & Chr(86) & Chr(0)

or

B4X:
Chr(27) & Chr(109)

directly to the printer, the second is a partial cut. Depending on the emulation of the ESC/POS, these codes may or may not work for you.
yes, doesn't work..also tried (66) and (65)
Where do I have to look for the code for my printer? email to the brand of the printer?
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
devices like these have 2 modes of operation. command/control mode, and then data mode.

Data mode is what your getting, everything you send to it is printed. You need to figure out how to get it into command mode to be able to do what you want it to do. the SDK/API should tell you this. I know with LCD/VFD displays, you have to send the ESC code and then the command.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You can try this library. As i dont have such a printer i cannot test it.
I spent >6 hours to do it.

- Check the manifest-changes (there is a reference to a service inside the 3rd party jar)
- It is a time-limited Library
- It is a complete wrap for all Methods of the 3rd Party jar DataForSendToPrinterTSC-Class
- You need to find out how to connect. The Service should be connect correctly using the Example prject. Check the available Methods. Especially the Methods beginning with BD.
Once you get a successfull connect you should be able to write data to it...

All other methods are just there to create a bytearray for this specific command to print it later using the writeDataByYourself method.

PosPrintSDK

Author:
Version:
0.1
  • POSPrinter
    • Events:
      • onBind()
      • onFailed (tag As Object)
      • onSuccess (tag As Object)
      • onUnBind()
    • Fields:
      • binder As net.posprinter.posprinterface.IMyBinder
      • ISCONNECT As Boolean
    • Functions:
      • autoDetect (x As Int, y As Int) As Byte()
      • backFeed (n As Int) As Byte()
      • bar (x As Int, y As Int, width As Int, heigth As Int) As Byte()
      • barCode (x As Int, y As Int, codeType As String, heigth As Int, human As Int, rotation As Int, narrow As Int, wide As Int, content As String) As Byte()
      • BDacceptdatafromprinter
      • BDcheckLinkedState (tag As Object)
      • BDclearBuffer
      • BDconnectBtPort (arg0 As String, tag As Object)
      • BDconnectNetPort (arg0 As String, arg1 As Int, tag As Object)
      • BDconnectUsbPort (arg1 As String, tag As Object)
      • BDdisconnectCurrentPort (tag As Object)
      • bitmap (x As Int, y As Int, mode As Int, bitmap As android.graphics.Bitmap, bmpType As net.posprinter.utils.BitmapToByteData.BmpType) As Byte()
      • blineBydot (m As Int, n As Int) As Byte()
      • blineByinch (m As Double, n As Double) As Byte()
      • blineBymm (m As Double, n As Double) As Byte()
      • blineDetect (x As Int, y As Int) As Byte()
      • block (x As Int, y As Int, width As Int, height As Int, font As String, rotation As Int, x_multiplication As Int, y_multiplication As Int, space As Int, alignment As Int, content As String) As Byte()
      • box (x As Int, y As Int, x_end As Int, y_end As Int, thickness As Int) As Byte()
      • checkPrinterStateByPort4000 As Byte()
      • checkPrinterStateByPort9100 As Byte()
      • cls As Byte()
      • codeBlockFMode (x As Int, y As Int, rotation As Int, row_height As Int, module_width As Int, content As String) As Byte()
      • codePage (n As String) As Byte()
      • country (n As String) As Byte()
      • cut As Byte()
      • delay (ms As Int) As Byte()
      • density (n As Int) As Byte()
      • direction (n As Int) As Byte()
      • disPlay (s As String) As Byte()
      • dmatrix (x As Int, y As Int, width As Int, height As Int, expression As String, content As String) As Byte()
      • dmatrix3 (x As Int, y As Int, width As Int, height As Int, xm As Int, row As Int, col As Int, expression As String, content As String) As Byte()
      • downLoad (filename As String) As Byte()
      • downLoad2 (filename As String, bitmap As String) As Byte()
      • downLoad3 (filename As String, filepath As String) As Byte()
      • downLoad4 (filename As String, size As Int, content As String) As Byte()
      • ellipse (x As Int, y As Int, width As Int, height As Int, thickness As Int) As Byte()
      • eop As Byte()
      • erase (x As Int, y As Int, width As Int, height As Int) As Byte()
      • feed (n As Int) As Byte()
      • files As Byte()
      • formFeed As Byte()
      • gapBydot (m As Int, n As Int) As Byte()
      • gapByinch (m As Double, n As Double) As Byte()
      • gapBymm (m As Double, n As Double) As Byte()
      • gapDetect (x As Int, y As Int) As Byte()
      • home As Byte()
      • Initialize (EventName As String)
      • initialPrinter As Byte()
      • kill (filename As String) As Byte()
      • limitFeedBydot (n As Int) As Byte()
      • limitFeedByinch (n As Double) As Byte()
      • limitFeedBymm (n As Double) As Byte()
      • move As Byte()
      • offSetBydot (m As Int) As Byte()
      • offSetByinch (m As Double) As Byte()
      • offSetBymm (m As Double) As Byte()
      • pdf417 (x As Int, y As Int, width As Int, height As Int, rotate As Int, option As String, content As String) As Byte()
      • print (m As Int, n As Int) As Byte()
      • putBmp (x As Int, y As Int, filename As String, bpp As Int, contrast As Int) As Byte()
      • qrCode (x As Int, y As Int, eccLevel As String, cellWidth As Int, mode As String, rotation As Int, model As String, mask As String, content As String) As Byte()
      • readBuffer As net.posprinter.utils.RoundQueue
      • reference (x As Int, y As Int) As Byte()
      • reverse (x As Int, y As Int, width As Int, height As Int) As Byte()
      • run (filename As String) As Byte()
      • selfTest As Byte()
      • selfTest2 (page As String) As Byte()
      • shift (n As Int) As Byte()
      • sizeBydot (m As Int, n As Int) As Byte()
      • sizeByinch (m As Double, n As Double) As Byte()
      • sizeBymm (m As Double, n As Double) As Byte()
      • sound (level As Int, interval As Int) As Byte()
      • speed (n As Double) As Byte()
      • text (x As Int, y As Int, font As String, rotation As Int, x_multiplication As Int, y_multiplication As Int, content As String) As Byte()
      • write (data As Byte(), tag As Object)
      • writeDataByYouself (commands As List, tag As Object)
    • Properties:
      • CharsetName As String [write only]
 

Attachments

  • PosPrintSDKex.zip
    6.9 KB · Views: 416
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
writeDataByYouself (commands As List, tag As Object)
Is the main method to print.
Create a List in B4A and add commands to the List using the results of the library method resultvalues.... Usually they return a Bytearray. Add the array as object to the List.
Use the generated List to call writeDataByYouself. tag can be used to get infos about this call in one of the result-events.

Again: As i do not have such a printer i can not test it. But i just ordered one ;-)
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi there..
did you installed first on windows ?

New POS Printer (at chinese model..) have settings at driver properties (at windows)- that you must turn on like auto cutter... seems like my mine pos-80c but your's bluetooth..
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
@DonManfred, it's too advanced for me..
from your test:
Logger connected to: BMXC K107
--------- beginning of system
--------- beginning of main
** Activity (main) Pause, UserClosed = false **
java.lang.Exception: Sub activity_pause signature does not match expected signature.
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
binder connected
POS_onBind()
POS_onSuccess(BDconnectBtPort)
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi there..
did you installed first on windows ?

New POS Printer (at chinese model..) have settings at driver properties (at windows)- that you must turn on like auto cutter... seems like my mine pos-80c but your's bluetooth..
yes, printing and cut the paper when connected by USB to win7
 

Attachments

  • xp-80c.png
    xp-80c.png
    58 KB · Views: 409
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Looks like you have a connection... Looks GOOD.

What is the code you are using to connect to this printer?

could we try from the example..

https://www.b4x.com/android/forum/t...printer-with-matching-codepage.76831/#content

B4X:
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("Printer001") Then 'Insert the BT-Name of the printer or use the MAC address
                'Printer001 -> DC:0D:30:06:98:71
                Exit
            End If
        Next
        BTConnection.Connect(MacAddress)
    End If
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I would like to start with the example i provided with the library! The Serial library is not needed ( i think). Please note that the Lib is using another aproach as using the Serial library.

You can not mix them. But you dont need to as i expect you can use all features of the printer in the way the printer is expecting them.

Based on your code the devicemac is
DC:0D:30:06:98:71
so you should use
B4X:
    pos.BDconnectBtPort("DC:0D:30:06:98:71","BDconnectBtPort")
to connect to the printer. BAsed on the log you posted i guess you are using just this line?
 
Upvote 0
Top