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).