To whom it may help, I´ll try to explain the situation, and what did happen. The App I develop, what it does, is that it opens gates (relays) in community condos that are attached to an ESP32, subscribed to mdash.net. The problem comes, when houses that are near the main entrance, the user device tries to connect to their homes Wifi with a very weak signal, so it is very dificult to transmit data to the internet.
Given that problem, is that I asked
@Erel how could I (if the user forced it) redirect internet trafic only by the mobile network, which came with a
solution (that can't be used in B4I)
This solution gives us a Network ID, when it request a PreferMobileRouting sub. When the mobile network gets disconnected (modem reset I think), I put a broadcastReceiver to get this event, and then I call again PreferMobileRouting, because the ID of PreferMobileRouting has changed. This solved the whole issue.
Broadcast Receiver:
Sub BroadcastReceiver_OnReceive (Action As String, i As Object)
Dim retIn As Intent
retIn = i
Dim IsConnected As String
Dim TheType As String
If Action = "android.net.conn.CONNECTIVITY_CHANGE" Then
Dim jo As JavaObject = retIn
Dim NetworkInfo As JavaObject = jo.RunMethod("getParcelableExtra", Array("networkInfo"))
TheType = NetworkInfo.RunMethod("getTypeName", Null)
IsConnected = NetworkInfo.RunMethod("getState", Null)
If TheType.ToUpperCase="MOBILE" And IsConnected.ToUpperCase="CONNECTED" Then CallSub2(Main, "PreferMobileRouting", True)
End If
BroadCast.AbortBroadcast
End Sub
How to suggest a Mobile Connection Route for the App:
Sub PreferMobileRouting(prefer As Boolean) As ResumableSub
Dim p As Phone
If p.SdkVersion >= 21 Then
' Else
' builder.RunMethod("addTransportType", Array(0))
' builder.RunMethod("addTransportType", Array(1))
' builder.RunMethod("addCapability", Array(12)) 'NetworkCapabilities.NET_CAPABILITY_INTERNET
' Log("NET_CAPABILITY_INTERNET")
If prefer Then
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("connectivity"))
Dim builder As JavaObject
builder.InitializeNewInstance("android.net.NetworkRequest.Builder", Null)
builder.RunMethod("addTransportType", Array(0)) 'NetworkCapabilities.TRANSPORT_CELLULAR
Log("TRANSPORT_CELLULAR")
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.")
Log(Network) '<--THIS ID CHANGES EVERY TIME THE MODEM RESTARTS OR RESETS
Dim cm As JavaObject
Return (cm.InitializeStatic("android.net.ConnectivityManager").RunMethod("setProcessDefaultNetwork", Array(Network)))
End If
End If
Return False
End Sub