0

I have created an application to connect to selected Wifi from the list. This works fine in the android version of 4.1.2(network id = 10) but if I try to run the same application on version 4.4.2 and 4.4.4 it shows the network id value as -1.

My code to achieve this is :

private void connectToWifi2() {
    // TODO Auto-generated method stub

    // Main method. Write code here.
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = String.format("\"%s\"", selWifi.SSID);
    conf.preSharedKey = String.format("\"%s\"", pwd.getText().toString());


    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    conf.status = WifiConfiguration.Status.ENABLED;
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);

    conf.hiddenSSID = true;
    // remember id
    int netId = wifiManager.addNetwork(conf);
    Log.d("RMGWiFiConfig", "netId returned" + netId);
    if (netId >= 0) {
        Log.i("RMGWiFiConfig", "Connecting with " + selWifi.SSID);
        wifiManager.disconnect();
        Boolean statusInBoolean = wifiManager.enableNetwork(netId, true);
        Log.d("RMGWiFiConfig", "Enabled network returned "
                + statusInBoolean);
        Log.d("RMGWiFiConfig", "Reassociate : " + wifiManager.reassociate());
        Log.d("RMGWiFiConfig", "Reconnect " + wifiManager.reconnect());

    } else {
        Log.d("RMGWifiConfig", "Failed to add network configuration");
    }

}

I tried to refer the below links but not able to resolve the problem. Or may be I am unable to fit in the solution in the right way provided in the below links.Please let me know the changes I need to do w.r.t my code.

Can not connect another android device with wifi with android lollipop

Android WifiConfiguration shows -1 for ID. How can I fix it for SSID to be recognized?

Android 6.0 Cannot add WifiConfiguration if there is already another WifiConfiguration for that SSID

android wifiManager.addNetwork returns -1

WifiConfiguration enable network in Lollipop

Android 5.0 Lollipop and 4.4 KitKat ignores my WiFi network, enableNetwork() is useless

Please Help!

Thanks Shruti

Community
  • 1
  • 1
Shruti Joshi
  • 53
  • 1
  • 10
  • In this link I find a solution as to enableNetwork() but unable to fix it. Please help -http://stackoverflow.com/questions/26986023/wificonfiguration-enable-network-in-lollipop/27358531#27358531 – Shruti Joshi Feb 08 '16 at 14:03

1 Answers1

0

I can't find an obvious critical reason, but I have similar code working on Android 6.x. The differences I can find are:

  • wc.StatusField = WifiStatus.Disabled; (enableNetwork() will update this later)
  • wc.PreSharedKey = "\"" + pw + "\"";
  • at the end: wm.SaveConfiguration();

If I had to guess, I'd say it was the first one that's causing the failure.

Eliot Gillum
  • 832
  • 9
  • 18