Android Question Check the connection in B4A

RAFA BRAVO

Active Member
Licensed User
Longtime User
With this communication configuration of the starter module, how could I check if I am connected to server or not?for example if Arduino is off:
STARTER:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private server As ServerSocket
    Private astream As AsyncStreams
    Public connected As Boolean
    Private ser As B4RSerializator
    

    
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    ser.Initialize
    server.Initialize(51042, "server")
    server.Listen   
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub



private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
'    Log($"NewConnection: ${Successful}"$)
    If Successful Then
        connected = True
        If astream.IsInitialized Then astream.Close
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
        
    End If
    
    
End Sub

Sub Reconectar
    server.Listen
End Sub

Private Sub AStream_NewData (Buffer() As Byte)
    Dim objects() As Object = ser.ConvertBytesToArray(Buffer)
    For Each o As Object In objects
'        Log(o)
        Select Case o
            Case "paro1T"
                
                CallSub(Main,"Input1")
                
            Case "paro2T"
                
                CallSub(Main,"Input2")
            Case "paro3T"
            
                CallSub(Main,"Input3")
            Case "paro4T"
                
                CallSub(Main,"Input4")
            Case "paro5T"
                
                CallSub(Main,"Input5")
            Case "paro6T"
            
                CallSub(Main,"Input6")
            Case "paro7T"
                
                CallSub(Main,"Input7")
            Case "paro8T"
            
                CallSub(Main,"Input8")
            Case "paro9T"
                
                CallSub(Main,"Input9")
            Case "paro10T"
                
                CallSub(Main,"Input10")
            Case "paro11T"
                
                CallSub(Main,"Input11")
            Case "paro12T"
            
                CallSub(Main,"Input12")
            Case "paro13T"
                
                CallSub(Main,"Input13")
            Case "paro14T"
                
                CallSub(Main,"Input14")
            Case "SETA"
                
                CallSub(Main,"InputSETA")
                
            Case "ACTIVADA"
                
                CallSub(Main,"InputACTIVADA")
        End Select
    Next
    
    
    Dim SALIDA = Main.output As String
    astream.Write(ser.ConvertArrayToBytes(Array(SALIDA)))
End Sub

Private Sub Astream_Error
'    Log("Disconnected")
    connected = False
    
End Sub

Private Sub Astream_Terminated
    Astream_Error
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

From Arduino if I get it with:
B4R:
Sub Connect(unused As Byte)
    If ethClient.ConnectIP(serverIp, serverPort) = False Then
'        Log("trying to connect again")
        CallSubPlus("Connect", 1000, 0)
        Return
    End If
'    Log("Connected to server")
    astream.Initialize(ethClient.Stream, "Astream_NewData", "Astream_Error")
    timer1.Enabled = True
End Sub

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