Issue with clientsocket and astreams disconnecting B4A

flyingpole

Member
Licensed User
Longtime User
Hello,

I have a brewing controller that I manufacture that has an integrated wifi radio. I use a Socket to communicate via TCP to my controller. I pass the socket to a astreams.

I've had issues with random disconnects from the B4A app to the radio. For years I've attributed these random disconnects to the embedded wifi radio I was using.

I recently switched to a different wifi device, and still get random disconnects. I then began to suspect it was my B4A app that had the issue. To test this theory, I installed 2 different TCP telnet terminal apps on my Android tablets and connected to the controller that way. With either one of those apps, the controller has never disconnected! With my B4A app, I get random disconnects ranging from 2 to 124 disconnects in a 24 hour period. I'm convinces it is my App causing the disconnects, not the hardware. Please look at what I'm doing and I'd appreciate anyone pointing out any issues with the code or maybe another approach. Thank you in advance for any help!

below is my code that handles the various socket connection routines.

B4X:
 Dim BBClientSocket As Socket  'for wifi implementation to  Controller
 Dim BBAstreams As AsyncStreams  'used for USB or wifi


the following is in my Activity_Create:

    If MapBBParams.Get("wifiEnabled") = "true" Then

    'using wifi, initialize that connection
    BBClientSocket.Initialize("BBClientSocket")

    'V1.20 use mapped parameters for ip address and port
    BBClientSocket.Connect(MapBBParams.Get("BBIPAddress"), MapBBParams.Get("BBPort"), 5000)


    lblBBConnectionStatus.Text = "Connecting"
    End If





    Sub BBClientSocket_Connected (Successful As Boolean)

    If Successful Then

    Try
    BBClientSocket.TimeOut=10000 'added to force astreams error if clientsocket disconnects

    BBAstreams.Initialize(BBClientSocket.InputStream, BBClientSocket.OutputStream, "BBAstreams")

    Catch
    Log("Error in BBClientSocket_Connected Try/Catch => " & LastException)
    LogEvent("Error in BBClientSocket_Connected Try/Catch => " & LastException)

    End Try

    lblBBControllerConnectionStatus.Color = Colors.Green
    lblBBControllerConnectionStatus.TextColor = Colors.Black
    lblBBControllerConnectionStatus.Text="wifi Connected"
    Log("controller connected via wifi!")
    LogEvent("controller connected via wifi!")




    Else

    lblBBConnectionStatus.Color = Colors.Red
    lblBBConnectionStatus.TextColor = Colors.Black
    lblBBConnectionStatus.Text="Error Connecting"



    End If

    End Sub


    The following code is called periodically in a timer routine to validate that the connection is still valid, and this gets triggered randomly.


    'now check to see if we are connected via wifi
    If BBClientSocket.Connected = False Then 'we are not connected, retry connecting . closing socket first
    Log("Error -Controller Deemed Disconnected in RequestControllerDataTimer")
    LogEvent("Error -Controller Deemed Disconnected in RequestControllerDataTimer")


    BBAstreams.Close
    BBClientSocket.Close
    BBClientSocket.Initialize("BrewBossClientSocket")

    BBClientSocket.Connect(MapBBParams.Get("BBIPAddress"), MapBBParams.Get("BBPort"), 5000)
    End If



    Below are my astreams errod routines:
    Sub BBAstreams_Error
    Log("BBAstreams Error - Controller Disconnected: " & LastException)
    LogEvent("BBAstreams Error - Controller Disconnected: " & LastException)

    lblBBControllerConnectionStatus.Color = Colors.Red
    lblBBConnectionStatus.TextColor = Colors.Black
    lblBBConnectionStatus.Text="Disconnected" 'V1.20 changed prompt
    BBAstreams.Close
    If MapBBParams.Get("wifiEnabled") = "false" Then
    usb.Close
    Else
    BBClientSocket.Close
    End If


    End Sub

    Sub BBAstreams_Terminated
    Log("Astreams Terminated")
    LogEvent("Astreams Terminated")

    lblBBConnectionStatus.Color = Colors.Red
    lblBBConnectionStatus.TextColor = Colors.Black
    lblBBConnectionStatus.Text="Disconnected"
    BBAstreams.Close

    End Sub
 
Last edited:

flyingpole

Member
Licensed User
Longtime User
Thanks, I already do have PhoneWakeState set to not allow sleeping. I'll try the service route and see what happens.

Thanks again, great support as always!
 

flyingpole

Member
Licensed User
Longtime User
Erel, when you say "ALL" network related code, do you mean just the items currently in Activity_Create or even the data arrival, etc?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…