MohsenSaber2019
Member
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
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 :
What i can do?
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)