Connect to wifi without internet programmatically

ForJ9 picture ForJ9 · Nov 22, 2016 · Viewed 7.3k times · Source

I'm using the following code to connect to a WiFi network without internet connection, because it hosts its own web-server where I want to connect even if there is no internet connection available.

WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = String.format("\"%s\"", scanResult.SSID);
wifiConfiguration.preSharedKey = String.format("\"%s\"", "secret");

int netId = wifiManager.addNetwork(wifiConfiguration)
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();

This works fine on every device below Android Marshmallow (I'm using CM13, so is it maybe related to CyanogenMod?): When I use this code to connect, Android still uses the mobile connection and mark the WiFi symbol with an exclamation mark. Seconds later it shows a notification with the question if I want to stay connected, because the network has no internet connection.

Problem: My app tries to connect automatically to my web-server which fail, because there is obviously no internet connection. Of course it would be possible to wait until I can reach my web-server, but it should work without user interaction.

How does ChromeCast or any other IoT device solve this? I never saw a notification like this when I was setting up my ChromeCast device.

Answer

Lonergan6275 picture Lonergan6275 · Jan 13, 2017

I solved this by binding (connectivityManager.bindProcessToNetwork(network);) the current process to the connected network. This prevents the "keep using this network" Dialog and allows you to communicate with the device over wifi.

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) {
                if (Build.VERSION.RELEASE.equalsIgnoreCase("6.0")) {
                    if (!Settings.System.canWrite(mActivity)) {
                        Intent goToSettings = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        goToSettings.setData(Uri.parse("package:" + mActivity.getPackageName()));
                        mActivity.startActivity(goToSettings);
                    }
                }
                connectivityManager.bindProcessToNetwork(null);
                if (mSsid.contains("my_iot_device-xxxxxxxxx")) {
                    connectivityManager.bindProcessToNetwork(network);
                } else {

                }

            } else {
                //This method was deprecated in API level 23
                ConnectivityManager.setProcessDefaultNetwork(null);
                if (mSsid.contains("my_iot_device-xxxxxxxxx")) {
                    ConnectivityManager.setProcessDefaultNetwork(network);
                } else {

                }

            }
            try {
                //do a callback or something else to alert your code that it's ok to send the message through socket now
            } catch (Exception e) {
                Crashlytics.logException(e);
                e.printStackTrace();
            }
            connectivityManager.unregisterNetworkCallback(this);
        }
    });
}