Android Question PreferWifiRouting (Calling multiple times)

fbritop

Active Member
Licensed User
Longtime User
I did use this example:

Which work, but I did change it, as my requierments are that I need to route traffic through mobile network rather that WiFi for a specific function that the App does. Saying that, once I switch to NetworkCapabilities.TRANSPORT_CELLULAR it works fine, but then I cannot switch back to route through NetworkCapabilities.TRANSPORT_WIFI


Doing some tests with the code bellow:

-App starts and I call PreferWifiRouting(0) it replies with the mobile gateway ok: 186.11.19.210
-App starts and I call PreferWifiRouting(1) it replies with the wifi gateway ok: 200.111.224.20
-App starts and I call PreferWifiRouting(0) it replies with the mobile gateway ok: 186.11.19.210, again call (without restarting the app) PreferWifiRouting(1) and gateway still in 186.11.19.210 (Not switching to WIFI network routing on 200.111.224.20)
-App starts and I call PreferWifiRouting(1) it replies with the mobile gateway ok: 200.111.224.20, again call (without restarting the app) PreferWifiRouting(0) and gateway still in 200.111.224.20 (Not switching to MOBILE network routing on 186.11.19.210)

Is it possible only once to call PreferWifiRouting or there is something missing to reroute the traffic every time? Or on the other hand, how can we return the routing to the default options from the OS, that is Wifi if available, or mobile if there is no wifi, but to return the managment of the network to the OS

I have notice that after some time it return to WIFI routing, but I´m not sure if the OS does it, or just my call to the function takes that long to change.
Also, is this function available for B4I?

B4X:
Sub test
    '1=NetworkCapabilities.TRANSPORT_WIFI
    '0=NetworkCapabilities.TRANSPORT_CELLULAR
    Wait For (PreferWifiRouting(0)) Complete (Success As Boolean)
    Log(Success)
        Dim jobLocal As HttpJob
        jobLocal.Initialize("", Me)
        jobLocal.Download("https://api.ipify.org/")
        jobLocal.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
        jobLocal.GetRequest.Timeout=10000
        Wait for (jobLocal) JobDone(jobLocal As HttpJob)
        If jobLocal.Success Then
            Log(jobLocal.GetString)
        End If
       
       
    'End If
   
End Sub
Sub PreferWifiRouting(Method As Int) 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(Method)) 'NetworkCapabilities.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.")
        Dim cm As JavaObject
        Return (cm.InitializeStatic("android.net.ConnectivityManager").RunMethod("setProcessDefaultNetwork", Array(Network)))
    End If
    Return False
End Sub

#if Java
import anywheresoftware.b4a.BA;
import android.net.wifi.*;
import android.content.Context;

public static class NetworkCallback extends android.net.ConnectivityManager.NetworkCallback {
  public void onAvailable(android.net.Network network) {
         processBA.raiseEventFromUI(null, "network_available", network);
  }
}
#End if
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Also, is this function available for B4I?
No.

1. PreferWifiRouting is only a hint to the OS.
2. There was a similar discussion in the forum a few weeks ago. I suggested to create two separate apps as switching between the transport modes is likely to not work or at least not reliably.
 
Upvote 0
Top