Android Question AsyncStream Error while reconnecting the application

Husain Ragib

Member
Licensed User
My application connects an Android device via bluetooth.
The application works fine when the device is connected with the application start.
But when, in between the connectivity is lost and is reconnected... eventhough the commands are being send, I am not able to receive any response back from the Async streams. Instead Async_error is received.

Can you please help me to figure out the cause of the error.

Below is my code for connecting with the device :
B4X:
Sub Reader_Connected (Success As Boolean)
    If Success Then   
        ReaderStatus.IsConnected = True
        BStream.Initialize(BTConnection.InputStream, BTConnection.OutputStream, "BStreams")
    Else
        ReaderStatus.IsConnected = False
    End If
End Sub

Below is my code for Sending commands :
B4X:
Public Sub SendCommand(Cmd As String)

    Starter.RBuffer = ""
    Dim buf As String = Cmd & CRLF
    Dim by() As Byte = buf.GetBytes("UTF7" )
    Starter.BStream.Write(by)
End Sub
 
Last edited:

Husain Ragib

Member
Licensed User
You should expect the connection to be broken from time to time. Try to reconnect after the connection breaks.
Dear Erel,
Many thanks for your reply.

Eventhough I am trying to reconnect with the device, the connection is broken again.

Sometimes, I could see that the device maintains the connection eventhough the application is closed. Then, if I again open the application it fails to connect.
Once connected and then exiting from the application, do i explicitly need to disconnect the connection?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
do i explicitly need to disconnect the connection?
No. As I wrote you should expect connections to be broken and your program should try to reconnect when needed.

In fact in many cases it is simpler to make a connection, send something and close it. Similar to how http requests are sent. This way the state is simpler.
 
Upvote 0
Top