2

I have both WIFI and mobile data enabled. In wifi I have connected to an OBD which has no internet connection but will stream some data if connected. In order to start that stream I used the below code:

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

        NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
        if (SDK_INT >= LOLLIPOP) {
            NetworkRequest.Builder request = new NetworkRequest.Builder();
            request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

            connectivityManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {

                @Override
                public void onAvailable(Network network) {
                    if (SDK_INT >= LOLLIPOP && SDK_INT < M) {
                        ConnectivityManager.setProcessDefaultNetwork(network);
                    } else if (SDK_INT >= M) {
                        connectivityManager.bindProcessToNetwork(network);
                    }
                }
            });
        }

I got the code from this answer.

How to stay connected through mobile network after WIFI is connected on Android?

When it comes to get the active network into at connectivityManager.getActiveNetworkInfo() I get the network type as MOBILE[LTE]. Once it hits the onAvailable method, whenever I try to get the active network type it says WIFI[].

After this the stream gets started. Now I want the mobile data to be activated again since it is enabled. So I tried the same code after the stream started by just replacing TRANSPORT_WIFI with TRANSPORT_CELLULAR in request.addTransportType.

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

        NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
        if (SDK_INT >= LOLLIPOP) {
            NetworkRequest.Builder request = new NetworkRequest.Builder();
            request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);

            connectivityManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {

                @Override
                public void onAvailable(Network network) {
                    if (SDK_INT >= LOLLIPOP && SDK_INT < M) {
                        ConnectivityManager.setProcessDefaultNetwork(network);
                    } else if (SDK_INT >= M) {
                        connectivityManager.bindProcessToNetwork(network);
                    }
                }
            });
        }

What happens is, when I debug this process, NetworkInfo always has the WIFI connection even after it hits the onAvailable method.

Please pinpoint if I was wrong somewhere or am I missing something.

Community
  • 1
  • 1
Sashi Kiran
  • 156
  • 9
  • I'm not sure this will not affect the OBD stream (never tried it), but did you try setting 'connectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);' after the stream started? – yakobom Jan 02 '17 at 08:13
  • Did you ever solve this? I am trying to do something very similar. – Mark A. Rupert Sep 21 '17 at 02:50
  • Were you able to get this sorted out? – Nigel Jan 12 '18 at 14:59
  • Sorry for the delayed reply. But we moved to another solution where we don't need the WiFi for the OBD. Couldn't find any solution for this problem. – Sashi Kiran Sep 28 '19 at 08:22

0 Answers0