user640853
user640853

Reputation: 155

Connect to WiFi Which Doesn't Have Internet

I'm trying to connect to a WiFi hotspot (open network) which doesn't have Internet access when the app starts.

However, there's another saved Wifi which has Internet. When I turn on Wifi, it always connects automatically to the one with Internet access.

I have been trying to fix this issue for like a week now! But nothing is working. In fact, my code disconnects from the network with internet but doesn't connect to the network I want. It doesn't make sense.

On the WiFi settings activity, it says "No Internet detected. Won't reconnect automatically."

private boolean tryConnect(WifiManager wifiManager, List<ScanResult> scanResults) {
    for (ScanResult scanResult : scanResults) {
        Log.d(TAG, "SCAN-RESULT: " + scanResult);
        if (scanResult.SSID.toLowerCase().contains(MainActivity.ARDRONE2_HOTSPOT_NAME) && WifiUtilities.getScanResultSecurity(scanResult) == WifiUtilities.NetworkSecurity.OPEN) {
            Log.d(TAG, "Trying Connecting to ARDrone2");
            WifiConfiguration wifiConfiguration = new WifiConfiguration();
            wifiConfiguration.SSID = String.format("\"%s\"", scanResult.SSID);
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfiguration.priority = Integer.MAX_VALUE - 1;
            List<WifiConfiguration> wifiConfigList = wifiManager.getConfiguredNetworks();
            int networkId = -1;
            for (WifiConfiguration wifiConfig : wifiConfigList) {
                if (wifiConfig != null) {
                    if (wifiConfig.SSID.equals("\"" + scanResult.SSID + "\"")) {
                        networkId = wifiConfig.networkId;
                    } else {
                        wifiManager.disableNetwork(wifiConfig.networkId);
                    }
                }
            }
            if (networkId == -1) {
                networkId = wifiManager.addNetwork(wifiConfiguration);
            } else {
                networkId = wifiManager.updateNetwork(wifiConfiguration);;
            }
            wifiManager.saveConfiguration();
            wifiManager.disconnect();
            wifiManager.enableNetwork(networkId, true);
            wifiManager.reconnect();
            return true;
        }
    }
    return false;
}

The debug log: "Trying Connecting to ARDrone2" appears and yet it doesn not connect!!!

A similar question has been asked here Android, automatically connecting to wifi networks that have no internet access but no comments or answers were provided.

Upvotes: 2

Views: 760

Answers (1)

user640853
user640853

Reputation: 155

It seems like the problem was with my own phone. I have CM13.0 and apparently they don't allow connecting to networks that don't have Internet access. Even manual connecting to a network without Internet is a lot of trouble.

I tried it on other phones with non-custom ROMs and they all worked as expected according to the code above:

  1. Disable all saved networks (this could be improved)
  2. Disconnect from currently connected network
  3. Connect to the ARDrone2

Upvotes: 1

Related Questions