Android Question WIFI connection problem

TSW2017

Member
Licensed User
Hi, i'm new of B4A. I'm a android and IOS developer. iw ant tp say thank you and hello to all the community. .

I have a little problem about wifi connection with my app.

When i estabilish a wifi connection before start the app the socket answer to my commands.
If i try to connect during the app socket asnwer me ( " no connection")

it's quite strange. this is my code

B4X:
Sub Process_Globals
    Dim AStream As AsyncStreams
     Dim sk As Socket
    
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("2")
    If CheckWifi = 0 Then Return
    sk.Initialize("sk")
    sk.Connect(example, 8899, 3000) ' try to connect - 3000mS timeout

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If sk.IsInitialized Then SocketClose
If AStream.IsInitialized Then AStream.Close
If UserClosed = True Then
Activity.Finish
End If
End Sub

Sub CheckConnectionStatus As String
    Dim InterCon As ServerSocket
    Dim s As String
    InterCon.Initialize(0, "")
    s = InterCon.GetMyIP
    InterCon.Close
    Return s
End Sub

Private Sub CheckWifi() As Int
    Dim ls As String
    Dim k As Int
    ls = CheckConnectionStatus ' get smartphone IP
    k = example.LastIndexOf(".") ' find last dot in the exampleIP
    If ls.SubString2(0, k) <> example.SubString2(0, k) Then ' if the WiFI is not in the same class of the hotspot LA203RG
        Msgbox("Connect Wifi to example", "INFO")
        Dim i As Intent
        i.Initialize("android.settings.WIFI_SETTINGS", "")
        StartActivity(i)
        Return 0
    End If
    Return 1
End Sub

Private Sub SocketClose()
    If sk.Connected = True Then
        sk.Close
    End If
End Sub

Sub sk_Connected(Successful As Boolean)
    If Successful = True Then
        If AStream.IsInitialized Then AStream.Close
        AStream.Initialize(sk.InputStream, sk.OutputStream, "AStream") 'initialize AsyncStreams with the socket streams.
    End If
End Sub

I can't see the error/ errors. Thank you in advance for help. I really appreciate that
 

sorex

Expert
Licensed User
Longtime User
building up the wifi connection takes a few seconds and your socket init was already passed before it had network.

you need to check on network state changes and connect it there
 
Upvote 0
Top