9

I'm trying to temporarily force the device to use mobile data. I'm using ConnectivityManager.requestNetwork(),

    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);

    connectivityManager.requestNetwork(request.build(), newRequestTransportCallback(connectivityManager),
            (int) TimeUnit.SECONDS.toMillis(10));

Where newRequestTransportCallback() returns,

new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                Log.d(TAG,"Network available: " + network + ", binding...");
                if (cm.bindProcessToNetwork(network)) {
                    Log.d(TAG,"Bind successful to network: " + network);
                } else {
                    Log.w(TAG,"Bind failed to network: " + network);
                }
            }

            @Override
            public void onUnavailable() {
                Log.w(TAG,"Network not available");
            }
        };

I can see from my log statements that the request and bind succeed. However, when I subsequently call ConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) it tells me the network is disconnected (and I've tried polling to see if there's some delay in updating the network info with no success either).

halfer
  • 19,824
  • 17
  • 99
  • 186
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Think that you can't just force device to use particular type of network. addTransportType method is dedicated for checking the connection on selected layers of networks. – Serhii Hrabas Aug 11 '22 at 09:15

0 Answers0