Android Question Being stuck in PreferWifiRouting

Hi All.
I'm working on connection between ESP8266 and my B4A app. For a proper connection in perent of phone mobile data I should use PreferWifiRouting function that Erel presented here :
Erel: If you are using Android 5+ then you can try this solution

PreferWifiRouting:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Wait For (PreferWifiRouting) Complete (Success As Boolean)
   Log(Success)
End Sub

Sub PreferWifiRouting As ResumableSub
   Dim p As Phone
   If p.SdkVersion >= 21 Then
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       Dim builder As JavaObject
       builder.InitializeNewInstance("android.net.NetworkRequest.Builder", Null)
       Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("connectivity"))
       builder.RunMethod("addTransportType", Array(1)) 'NetworkCapabilities.TRANSPORT_WIFI
       Dim event As JavaObject
       event.InitializeNewInstance(Application.PackageName & ".main$NetworkCallback", Null)
       manager.RunMethod("requestNetwork", Array(builder.RunMethod("build", Null), event))
       Wait For network_available (Network As Object)
       Log("Network found.")
       Dim cm As JavaObject
       Return cm.InitializeStatic("android.net.ConnectivityManager").RunMethod("setProcessDefaultNetwork", Array(Network))
   End If
   Return False
End Sub

#if Java
public static class NetworkCallback extends android.net.ConnectivityManager.NetworkCallback {
  public void onAvailable(android.net.Network network) {
         processBA.raiseEventFromUI(null, "network_available", network);
  }
}
#End If
But there is a problem on re-connect to Esp8266 (Frist connection try is OK).
I mean when the connection to esp lost, my B4A app cant reconnect to it again.
I found the problem was in PreferWifiRouting and specially in this line :
network_available:
 Wait For network_available (Network As Object)
What i can do?
 
Sounds like the device no longer finds the ESP8266 network.
Hi Erel ; thanks for your response.
Actually device can connect to ESP8266 again.
As you can see when everything is ok the activity seem like this :
Screenshot_20200715-120736.png

I turned off wifi manually and then clicked on button, So app check for connection and if lost(like this case), start for finding ESP8266 network (button become inactive):

Screenshot_20200715-120802.png

But although as you can see, connection to ESP8266 successfully achieved again(wifi icon on notification bar); the program being stucked and do nothing.
I debugged app and find that it remains in wait for line and don't let program to run anymore.


UPDATE:
here is preview of log panel:

Screenshot_12.jpg
 
Last edited:
Start with debugging it while the "data network" is disabled. This way you will know that Android will not switch to any other network automatically.
Hi Erel, Unfortunately, no progress was made. Troubleshooting did not help either. I'm really disappointed. And this has caused serious problems for my business.
I do not know. Do you think the problem can be solved by disabling mobile internet through coding?
 
You cannot disable it programmatically.
Thanks Erel. So is this possible that I guide user to a dialog for turning off mobile data manually?

UPDATE: and a new question: isMobileConnected() method of MlWifi couldn't return true answer in android 10 . is there the alternative method for check mobile data status that be reliable in android 10?
 
Last edited:

MicroDrie

Active Member
Licensed User
Be aware that in a network a connection between 2 device can have a time-out. The result is that after the time-out value the connection details are lost. In that case a re-connection could only be achieved after a network search activity in the network. And that is usually part of setting up an initial connection.

The simple way to overcome your challenge is creating a "keep-alive" session by period reading the status of your device. This will lead first that Android OS sees that your software is "live" and secondly the network will see and keep the session information live. Be also aware that both timers for the software and network can have different values. So you can start with a small value and increase it step by step to find a optimum between network and software resources.

You can start with a ping to your device from your PC from a DOS command box and / or move your communication to a thread that is always active. Good luck.
 
Top