Connect to wifi without internet programmatically
Asked Answered
S

1

14

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.

Salinasalinas answered 22/11, 2016 at 22:41 Comment(4)
You might want to try the solution given by @KodeMechanic in this SO post wherein he used this to setup to cast from PC to chromecast using a mobile wireless connection. ChromeCast -> Local Router -> PC with PDANet -> USB connect to Mobile Phone with FoxFi app installed.Reis
Additionally, you may want to also check the steps to setup and use Chromecast without internet data in this thread. Hope that works for you too!Reis
Chromecast is here just an example that it is working. I want to implement a custom wifi accesspoint without this message that there is no internet connection, like chromecast :) It has nothing to do with chromecast exactly.Salinasalinas
@Salinasalinas Did you get this working i am working on a similar app and its working but is a bit buggy on some devices.Epigeal
E
10

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);
        }
    });
}
Epigeal answered 13/1, 2017 at 11:22 Comment(3)
I am having trouble establishing the connection itself. [#41712932 my question) Any pointers?Intonate
why is the "ACTION_MANAGE_WRITE_SETTINGS" needed? why is it specific to 6.0?Sauers
@FabiodeToledo jeah and it doesn't help at all. Android 8 (and 9) things they now better than you what network you should use - incredible annoying.Civic

© 2022 - 2024 — McMap. All rights reserved.