Android Question [B4A] [SOLVED] Very strange behavior when connected to Wireless AP

Jaco vd Walt

Member
Licensed User
Longtime User
I hope someone could shed some light on the issue I am having. I created a app which connect to a wifi AP, and then by means of http get a file from the webserver located on the access point. I tried to use a raspi, esp8266, even mikrotik, and getting the same results on all of them, and with 3 different phones (different makes, all android 8). The AP does not have internet. My b4a app would not ping the address of the ap. It would not connect any socket whatsoever. When I disable mobile data on the phone, it works. Now this is all good and well, but here is the strange thing, App with the name PING (apk from the app store, was written in pure java), does not have this problem. So this only happens with my b4a compiled app. It is as if the ping command in shell is trying to resolve the address 192.168.4.1 (the one of the ap) as if it is a named host. In other words, it is as if the app is trying to do a name resolution against the ip address, in stead of just using the ip. That is the one possibility, the other is that the phone is looking for the address on the internet because mobile data is still on. (this option is more plausible). Now this could be because of standard android routing, BUT why is the other app working ?


I have been struggling a week with this, hopefully what I am saying is making some sense to someone, and can point me in the right direction. I am not very strong on Java btw !

Thanks !
 

emexes

Expert
Licensed User
I don't know the answer, but I'll throw this in as a possible clue:

Working with the theory that B4A-Bridge has similar network selection/routing challenges, I had a look at its source code and found:
B4X:
Sub UpdateIp
    CurrentIp = server.GetMyWifiIP
    If CurrentIp = "127.0.0.1" Or CurrentIp.Contains(":") Then
        CurrentIp = "Not connected to wireless network."
    End If
    FTP_StateChanged
    lblIp.Text = "My IP: " & CurrentIp
End Sub
and so perhaps if you follow the GetMyWifiIP trail it might reveal a method of directing network operations to a specific adapter ie wifi.
 
Upvote 0

Jaco vd Walt

Member
Licensed User
Longtime User
Thank you, that is for sure something I could examine further. (I will first see if I get the same results on B4a bride, if not I will dig in the code of bridge :) ), will advise on my findings.
 
Upvote 0

Jaco vd Walt

Member
Licensed User
Longtime User
Thank you so much emexes for helping where you can, its much appreciated. I finally found the problem ! After the initial connection to the ssid, I issue a sleep(5000) command to give the wifi time to establish. This was the culprit. When I refined the code to rather doWhile until it gets the expected ip, and then sleep for 500 ms in the loop (with a timeout counter off course) all works like expected. I am not sure why the sleep breaks it, but that was it. Thanks again !


This works :

B4X:
       Dim x As Int = 0

        For Each result As String In Results
            If result.StartsWith("MT2219") Then
                wiindex = x
           
            End If
            x = x + 1
        Next
        Starter.scan.saveWifiAP(wiindex,"NONE","")
        Dim retries As Int = 0
        Do While Starter.wifi.WifiIpAddress.StartsWith("192.168.43") = False
            If retries > 5 Then Exit
            Sleep(500)
            retries = retries + 1 
        Loop
       
        Log (Starter.wifi.WifiIpAddress)

        Dim p As Phone
        Dim sb As StringBuilder
        sb.Initialize
        p.Shell("ping -c 1 192.168.43.1",Null,sb,Null)
        retries = 0
        Do While sb.ToString.Contains("100% packet loss")
            If retries > 10 Then Exit
            p.Shell("ping -c 1 192.168.43.1",Null,sb,Null)
            Log(sb)
            retries = retries + 1
        Loop

This does not :

B4X:
 Dim x As Int = 0

        For Each result As String In Results
            If result.StartsWith("MT2219") Then
                wiindex = x
           
            End If
            x = x + 1
        Next
        Starter.scan.saveWifiAP(wiindex,"NONE","")
        Sleep (5000)
        p.Shell("ping -c 1 192.168.43.1",Null,sb,Null)
        Log(sb)
 
Last edited:
Upvote 0
Top