Android Question Error when print bluetooth with AsyncStreams

Jose Cuevas

Member
Licensed User
Longtime User
Hi, I'm printing an invoice using a BT Printer, everything works fine, but when I turn off the printer, and try to print, triggers the AStreams_Error Event with this errors:

java.io.IOException: socket closed
java.io.IOException: read failed, socket might closed or timeout, read ret: -1

That's ok, that is how I know the printer is not available, but when I turn on the printer, I have the same errors, it's like is not reconnecting again.

Thanks in advance for your help.
 

Jose Cuevas

Member
Licensed User
Longtime User
Hi DonManfred, that's correct, This is the code:

B4X:
Sub Process_Globals

    Dim BtAdmin As BluetoothAdmin
    Dim BTConnection As Serial
    Dim lMacAddress As String = "00:17:AC:01:46:A9"
    Dim Printer As AsyncStreams

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("Main")
    
    If FirstTime Then
        ConnectPrinter
    End If

End Sub

Sub btnPrint_Click
    
    Try   

        Dim sText As String = "This is a test, This is a test, This is a test, This is a test" & CRLF & "This is a test, This is a test, This is a test" & CRLF
        
        BTSend(sText & CRLF & CRLF)
                
    Catch
        Log(LastException)
    End Try

End Sub

Sub BTSend(pText As String)

    Dim lBuffer() As Byte
    lBuffer=pText.GetBytes("UTF8")
    Printer.Write2(lBuffer,0,lBuffer.Length)
    
End Sub

Sub Printer_Connected (Success As Boolean)

    If Success Then
        Printer.Initialize(BTConnection.InputStream, BTConnection.OutputStream, "AStreams")
    Else
        Log("Printer_Connected Error: " & LastException.Message)
    End If

End Sub

Sub AStreams_Error
        
    Log("AStreams_Error:" & LastException.Message)

    If LastException.Message.Contains("bt socket close") Then
        ConnectPrinter
    Else
        Msgbox2("Unable to connect to printer","Error","OK","","", Null)
    End If
    
End Sub

Sub AStreams_Terminated
    Log("AStreams_Terminated")
End Sub

Sub ConnectPrinter
    
    BtAdmin.Initialize("BlueTooth")
    BTConnection.Initialize("Printer")
    
    Sleep(1000)
    
    BTConnection.Disconnect
    BTConnection.Connect(lMacAddress)
    
End Sub
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
When I turn off the printer, triggers the AStream_Error and Printer_Connected event at the same time:

AStreams_Error:java.io.IOException: bt socket closed, read return: -1
Printer_Connected Error: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

So, I try to reconnect at the AStreams_Error event, executing the ConnectPrinter Sub.

Regards.
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
The same thing if I send the print when the printer is off, then turn on the printer, send the print again, and shows the same 2 errors, I have to close de App,
so that I can print again.
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
Hi Erel, I just delete the line BTConnection.Disconnect, but now throw the AStreams_Error event, with this error: java.io.IOException: Broken pipe

If I exit from de App using Activity.Finish and execute the App again, shows the same error: java.io.IOException: Broken pipe
I have to force stop the App, in order to the printer works again.

Is like the app take over the printer, because the printer is disabled for other devices, until I force the App to stop, other devices can already print on that printer.

I have tried with 2 different printers and I have the same result.

Regards,
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
Thanks Erel, I wrote a new code (Connect -> Print -> Disconnect) now works like a charm.

This is the final code:

B4X:
Sub Process_Globals

    Dim BTConnection As Serial
    Dim BTPrinter As AsyncStreams

    Dim MACAddress As String = "00:17:AC:01:46:A9"
    
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Main")
    
    If FirstTime Then
        BTConnection.Initialize("Printer")
    End If

End Sub

Sub btnPrint_Click
    
    StartPrint       
    
End Sub

Sub StartPrint

    ProgressDialogShow("Printing")

    Try
        BTConnection.Connect(MACAddress)

    Catch
        Msgbox("Unable to connect to printer","Printer Error")
        BTPrinter.Close
        BTConnection.Disconnect
    End Try
    
End Sub

Sub Printer_Connected (Success As Boolean)
    
    Try
        If Success Then
            BTPrinter.Initialize(BTConnection.InputStream, BTConnection.OutputStream, "AStreams")
        
            BTPrint("This is a test, this is a test" & CRLF & _
                    "This is a test, this is a test" & CRLF & _
                    "This Is a test, this Is a test" & CRLF & _
                    "This is a test, this is a test" & CRLF & _
                    "This Is a test, this Is a test" & CRLF & CRLF)
                    
            ProgressDialogHide
            
            BTPrinter.Close
            BTConnection.Disconnect
        Else
            ProgressDialogHide

            If Msgbox2("Unable to connect to printer", "Printer Error","Try Again","Cancel","",Null) = DialogResponse.POSITIVE Then
                BTConnection.Disconnect
                StartPrint
            Else
                BTConnection.Disconnect
            End If
        End If
    
    Catch
        Log(LastException)
    End Try

End Sub


Sub BTPrint(pText As String)

    Dim lBuffer() As Byte
    lBuffer = pText.GetBytes("UTF8")
    BTPrinter.Write2(lBuffer, 0, lBuffer.Length)
    
End Sub

Sub AStreams_Error
    Log("AStreams_Error:" & LastException.Message)
End Sub

Sub AStreams_Terminated
    Log("AStreams_Terminated")
End Sub

Regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…