简体   繁体   中英

Lollipop Bad WiFi connection and Fallback to Mobile Network, how to Detect?

Lollipop has a new feature which is causing a huge problem for me. The system will fall back, transparently, to Mobile data when the WiFi network is "connected" but has no internet. http://www.androidpolice.com/2014/10/18/lollipop-feature-spotlight-android-now-defaults-to-mobile-data-when-wi-fi-has-no-internet-access-signal-icon-adds-a-for-no-connection/

Since one of the apps I make for my company is a WiFi testing application, similar to Fing etc, this is causing issues in Lollipop. I NEED to send packets out on the bad WiFi signal, so that problems can be located and fixed, ex Traceroute, Ping etc. While external IP's may not work, internal IP addresses should still be reachable in a scenario like this. However with the connection automatically falling back to the mobile network this is not longer possible.

I have not yet found any solution to detecting this. NetworkInfo.getState() and NetworkInfo.getDetailedState() both report "DISCONNECTED" even though the WiFi is connected and just has a bad external connection. Does anyone have any suggestions? All research thus far has led me to believe I can't force packets over the WiFi connection in Android, and I haven't figured out if I can detect this state. Other than detecting if Mobile data is on, and asking the user to turn it off, what are my options?

You can request a specific connection type (or a specific network) using ConnectivityManager#requestNetwork() .

You can then use a NetworkRequest.Builder to request a WiFi network like so:

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

NetworkRequest request = new NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .build();

cm.requestNetwork(request,
                  new ConnectivityManager.NetworkCallback() {
                      void onAvailable(Network network) {
                         // Do something once the network is available
                      }
                  });

The builder also allows you to specify additonal constraints (called "capabilities"), so you can optionally speficy more requirements such as a specific SSID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM