0

Everything practically works, with one exception. If we first assign transmission through mobile data, then everything works well, the data goes through the mobile network. Then I choose wifi data, everything is also good. But if you choose mobile data again after that, then the transfer is already going through wifi, why so? Why does all OK work at the first start, and when wifi is turned on again, data goes through wifi, and not through mobile communication? Who knows the reason? Thank you in advance. Code below.

public void setTransportType(int transportType) {

    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (networkCallback != null) {
        connectivityManager.unregisterNetworkCallback(networkCallback);
        networkCallback = null;
    }

    NetworkRequest.Builder request = new NetworkRequest.Builder();

    if (transportType == TRANSPORT_TYPE_WIFI) {
        request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    } else {
        request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
        request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    }

    networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            super.onAvailable(network);

            connectivityManager.bindProcessToNetwork(null);
            connectivityManager.bindProcessToNetwork(network);

            /*Operation after switching over*/
        }

    };

   connectivityManager.requestNetwork(request.build(), networkCallback); 
}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
ChazeRRR
  • 1
  • 2

1 Answers1

0

It's because you've bound your connection to the Wifi network with the line:

connectivityManager.bindProcessToNetwork(network);

When you want to switch to using mobile data, you need to reset the bound network:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    manager.bindProcessToNetwork(null);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    ConnectivityManager.setProcessDefaultNetwork(null);
}
matdev
  • 4,115
  • 6
  • 35
  • 56
  • Did you watch the code I attached? When I need mobile data, I clean wifi (bindProcessToNetwork (null)) and do (bindProcessToNetwork (network)) on mobile data. – ChazeRRR Sep 09 '20 at 09:43
  • this works only from Android SDK 23. Below this version, you need to use ConnectivityManager.setProcessDefaultNetwork(null); – matdev Sep 09 '20 at 12:33
  • You say obvious things. The question was different! – ChazeRRR Sep 09 '20 at 15:40
  • What is your OS version and manufacturer ? Have you checked this: https://stackoverflow.com/questions/55178150/bindprocesstonetwork-not-working-on-a-specific-device – matdev Sep 10 '20 at 07:14
  • The problem is not with the device. Since the first time when switching on transmission through mobile data, everything works perfectly, and when turning on again (after turning on wifi transmission), the transfer goes through wifi, although the actions are the same as when first turning on. – ChazeRRR Sep 10 '20 at 11:28
  • If both WiFi with internet and Mobile data are available, and if the phone is currently tranfering data using WiFi, it's possible that the OS will choose to stick with the WiFi connexion and not switch as requested. – matdev Sep 10 '20 at 12:35
  • Then why does she have to switch the first time? And when again decides to think?)) If the system itself decided, then there would be no API for switching data transmission. – ChazeRRR Sep 10 '20 at 14:42