13

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.

marklam
  • 5,346
  • 1
  • 24
  • 26
ForJ9
  • 735
  • 8
  • 24
  • You might want to try the solution given by @KodeMechanic in this [SO post](http://stackoverflow.com/a/27370352) 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`. – Teyam Nov 25 '16 at 10:52
  • Additionally, you may want to also check the steps to setup and use Chromecast without internet data in this [thread](https://www.reddit.com/r/LifeProTips/comments/38sb3v/lpt_use_chromecast_without_internetmobile_data_by/crxfse8/). Hope that works for you too! – Teyam Nov 25 '16 at 10:53
  • 1
    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. – ForJ9 Nov 25 '16 at 19:18
  • @ForJ9 Did you get this working i am working on a similar app and its working but is a bit buggy on some devices. – Lonergan6275 Jan 13 '17 at 11:13

1 Answers1

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);
        }
    });
}
Lonergan6275
  • 1,938
  • 6
  • 32
  • 63
  • I am having trouble establishing the connection itself. [http://stackoverflow.com/questions/41712432/connecting-to-wifi-network-automatically-in-android-5-and-6](See my question) Any pointers? – user2489122 Jan 23 '17 at 07:03
  • 1
    why is the "ACTION_MANAGE_WRITE_SETTINGS" needed? why is it specific to 6.0? – Easypeasy May 02 '17 at 11:31
  • 2
    @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. – kilian eller Mar 16 '19 at 13:09