Restrict Android Internet usage to WiFi without Internet Access

0 votes

I am building an android app that needs to communicate over a WiFi network that will not have any internet access. The problem is that even when the WiFi is connected android chooses to use cellular/mobile data when no connection internet is present on the wifi network.

I have read many posts on the issue many of which involve rooting the device but that is not possible with a production app (rooting devices is not an option). other solution (like my code bellow) suggest using bindProcessToNetwork() which works perfectly on my Sony Z2 but not on other devices I have tested on (all running 6.0.1)

private void bindToNetwork() {
    final ConnectivityManager connectivityManager = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new NetworkRequest.Builder();
        //set the transport type do WIFI
        builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
        connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


                    connectivityManager.bindProcessToNetwork(null);
                    if (barCodeData.getSsid().contains("screenspace")) {
                        connectivityManager.bindProcessToNetwork(network);
                    }

                } else {
                    //This method was deprecated in API level 23
                    ConnectivityManager.setProcessDefaultNetwork(null);
                    if (barCodeData.getSsid().contains("screenspace")) {

                        ConnectivityManager.setProcessDefaultNetwork(network);
                    }
                }

                connectivityManager.unregisterNetworkCallback(this);
            }
        });
    }
}
Jul 17, 2018 in IoT (Internet of Things) by Matt
• 2,270 points
1,567 views

1 answer to this question.

0 votes

You can check if wifi is connected then proceed else show a dialog to user asking him to connect to a wifi network

Since the method NetworkInfo.isConnected() is now deprecated in API-23, here is a method which detects if the Wi-Fi adapter is on and also connected to an access point using WifiManager instead:

private boolean checkWifiOnAndConnected() {
    WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON

        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        if( wifiInfo.getNetworkId() == -1 ){
            return false; // Not connected to an access point
        }
        return true; // Connected to an access point
    }
    else {
        return false; // Wi-Fi adapter is OFF
    }
}
answered Jul 17, 2018 by anonymous2
• 4,280 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Android M - Sending request over WiFi (without connection) when Mobile data is ON (with connection)

Binding the network using ConnectivityManager.setProcessDefaultNetwork() will help ...READ MORE

answered Oct 11, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,981 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to send a data from arduino uno to a webpage through esp8266 wifi module?

You are missing a few \r\n and the length ...READ MORE

answered Aug 9, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
3,450 views
0 votes
1 answer

Connect Android Things based Raspberry Pi 3 to wifi network for the first time!

Hey, I think its alright!  Your Raspberry Pi ...READ MORE

answered Jul 19, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
975 views
0 votes
1 answer
0 votes
1 answer

Android AllJoyn: Connection with second machine gives error of BusAttachement

Why dont you create two Interfaces, one ...READ MORE

answered Jul 25, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
555 views
0 votes
1 answer
0 votes
1 answer

Unable to communicate to device with .local domain using android, corova-zeroconf-plugin

Use IP address instead of http://iotdevice.local..Use htt ...READ MORE

answered Aug 2, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
1,521 views
0 votes
1 answer

I want to restrict the opening of Browser on URL Click

You can call it as a web ...READ MORE

answered Aug 7, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
4,930 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP