Android Question Use a socket in 2 activities

jlalexie

Member
Licensed User
Hello

I am trying to use a socket in 2 activities with the following sequence:

Main Activity: define socket and connect
Activity 1 : define Astreams , Astreams.Initialize, Write and Read
Activity 2: define Astreams , Astreams.Initialize , Write and Read

Main, Activity 1/Astreams is working correctly, then Activity 2 no response Astreams.


The code:

B4X:
' 
Main Activity ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Process_Globals
    Public CltSock As Socket
    Public Astreams As AsyncStreams
    Public cemp_ip As String = "192.168.4.1"
    Public cemp_port As Int = 1336
End Sub   

Sub Activity_Create(FirstTime As Boolean)
    ....
    CltSock.Initialize("Client")
    CltSock.Connect(cemp_ip,cemp_port,2000)
    .....
End Sub       

Sub Client_Connected(ConStatus As Boolean)
    cemp_connected=ConStatus   
    Log ("connected main: " & cemp_connected)
End Sub
    
'Activity 1''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   
Sub Process_Globals
    Public Astreams As AsyncStreams
    ............   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime And Main.cemp_connected  Then   
        Astreams.Initialize (Main.CltSock.InputStream,  Main.CltSock.OutputStream, "AStreams")
    End If
End sub

Sub DoSomeThing ()
    Astreams.Write(buffer_cmd)
End sub
    
Sub AStreams_NewData (Buffer() As Byte)       
    .......
End sub   

'Activity 2''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   
Sub Process_Globals
    Public Astreams As AsyncStreams
    ............   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime And Main.cemp_connected  Then   
        Astreams.Initialize (Main.CltSock.InputStream,  Main.CltSock.OutputStream, "AStreams")
    End If
End sub

Sub DoSomeThing ()
    Astreams.Write(buffer_cmd)
End sub
    
Sub AStreams_NewData (Buffer() As Byte)       
    .......
End sub
 
Top