B4A Class Bluetooth ESC/POS Printer Class

aeric

Expert
Licensed User
Longtime User
Try clean the project and try again.
I put a Log(Success) after the line, it returns true. But I think this is wait for user answer is completed, not printer is connected.
 

vecino

Well-Known Member
Licensed User
Longtime User
I have come up with something like this, it looks like it might work for what I need.
I don't know if that loop can bring any problems.

B4X:
iConnected = -1
Printer1.Connect                  
Do While iConnected = -1
  Sleep(0)
Loop

If iConnected=1 Then
  PrintDocu
Else
  ToastMessageShow("Printer Not connected",False)
End If
         

Sub Printer1_Connected( Success As Boolean )
    If Success Then iConectada=1 Else iConectada=0
End Sub
 

Xfood

Expert
Licensed User
I believe if you have any problems, you may never get out of this cycle,
if you really want to use this method I would do something like this to establish a maximum time for the possible exit.

B4X:
iConnected = -1
Dim now As Long =DateTime.now
Printer1.Connect                 
Do While iConnected = -1
  Sleep(0)
If DateTime.now-now>=1000 Then Exit   ' whatever you want'
Loop
 

vecino

Well-Known Member
Licensed User
Longtime User
Hello, I have a question:
In the example program included with the library a reset (printer1.Reset) is done just before printing a text, before printing a barcode, before printing an image, etc.
Is that something necessary, why is it done?
Thank you very much.
 

agraham

Expert
Licensed User
Longtime User
[Name] Select cut mode and cut paper
[Format] ①ASCII GS V m ② ASCII GS V m

①ASCII GS V m
Hex 1D 56 m
Decimal 29 86 m

②ASCII GS V m n
Hex 1D 56 m n
Decimal 29 86 m n

[Range] ① m= 1,49 ② m=66 , 0 <= n <= 255
[Description] Selects a mode for cutting paper and executes paper cutting.

The value of m selects the mode as follows:
1, 49 Partial cut(one point center uncut)
66 Feeds paper(cutting position + [n x(vertical motion unit)]) , and cuts the paper partially(one point center uncut)

[Details for①and②]
·This command is effective only processed at the beginning of a line.

[Note for ①]
·Only the partial cut is available; there is no full cut.
 

cwt

Active Member
Licensed User
Longtime User
The example works fine for me - connects to the printer and prints as expected with no errors.

When I add the printer code to my app, I get the below error when attempting to connect to the printer, on same device and printer where the example works:

escposprinter_connect (java line: 247)
java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.BA.setLastException(java.lang.Exception)' on a null object reference
at ikonpro.android.escposprinter._connect(escposprinter.java:247)
at ikonpro.android.sale_new$ResumableSub_B4XPage_MenuClick.resume(sale_new.java:415)
at ikonpro.android.sale_new._b4xpage_menuclick(sale_new.java:304)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA$1.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8376)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:640)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)

The crash happens on this line: PairedDevices = Serial1.GetPairedDevices

in the EscPosPrinter module sub: Public Sub Connect As Boolean

I can see no difference between the example app and my app, other than my app is B4XPages and not Activities.
 

cwt

Active Member
Licensed User
Longtime User
Actually, I am using your example almost verbatim in my own code. I call Connect from the title bar menu just as your example does. So the B4XPage has already loaded and displayed.
 

cwt

Active Member
Licensed User
Longtime User
I had not done Printer1.Initialize prior to calling Connect (stupidly). Now I just need to get Bluetooth permissions working but the permissions are not as easy as I expected.
 
@agraham
I'm trying to get one of these printers to work. I have downloaded your folder in the pinned post and I'm trying to get it to run. It connects to the printer but then doesn't print, any ideas/suggestions?

The only change that I've made to your code is the codepage.
 

nakuable

Member

Hi, first, I apologize for reviving an old post and thanks you @agraham for the class.

I'm ussing in my project and it work very well, but i'm trying print NV logo, but i can't.

Please, can you help me?

I tried some ESC/POS commands, and generary some character is printed in to the ticket. So, I think that I'm do any bad.

Thank you very much in advanced.

Best regards.
 

nakuable

Member
Hi Friends, I solved my issue.

Finally I add this function and work well for me with 3 different printers and logo in NVRam

B4X:
'--------------
' Print Memory Logo
'--------------

Public Sub PrintMemoryLogo()
    Dim cmd(4) As Byte
    cmd(0) = 28     ' FS
    cmd(1) = 112    ' "p"
    cmd(2) = 1      ' n = número de imagen
    cmd(3) = 0      ' m = modo de impresión normal
    WriteBytes(cmd)
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…