5

In my Android app I'm trying to connect my Android device to "WPA2-PSK" secured connection and after lot of search I have written following code--

if (securityMode.equalsIgnoreCase("WPA2")) // WPA2
                {

                   wifiConfiguration.SSID = "\"" + networkSSID + "\"";
                    wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
                    wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
                    wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                    wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                    wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                    wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    wifiManager.setWifiEnabled(true);
                    wifiManager.saveConfiguration();
                    int added = wifiManager.addNetwork(wifiConfiguration);
                    System.out.println("added network: " + added);
                    boolean enabled = wifiManager.enableNetwork(added, true);
                    System.out.println("enableNetwork returned: " + enabled);                   

                }

Above code is executing fine (without any error) but don't know why, connection is not getting establish. Please help. Thank you.!

user1758835
  • 215
  • 4
  • 17

1 Answers1

8

Below id clear api for all type of wifi security-type :

  public void addWifiConfig(String ssid,String password,String securityParam,String securityDetailParam) {
    Log.d(TAG, "Inside addWifiConfig...");
    if (ssid == null) {
        throw new IllegalArgumentException(
                "Required parameters can not be NULL #");
    }

    String wifiName = ssid;
    WifiConfiguration conf = new WifiConfiguration();
    // On devices with version Kitkat and below, We need to send SSID name
    // with double quotes. On devices with version Lollipop, We need to send
    // SSID name without double quotes
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        conf.SSID = wifiName;
    } else {
        conf.SSID = Constants.BACKSLASH + wifiName + Constants.BACKSLASH;
    }
    String security = securityParam;
    Log.d(TAG, "Security Type :: " + security);
    if (security.equalsIgnoreCase("WEP")) {
        conf.wepKeys[0] = password;
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    } else if (security
            .equalsIgnoreCase("NONE")) {
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    } else if ("WPA"
            .equalsIgnoreCase(security)
            || "WPA2"
                    .equalsIgnoreCase(security)
            || "WPA/WPA2 PSK"
                    .equalsIgnoreCase(security)) {
        // appropriate ciper is need to set according to security type used,
        // ifcase of not added it will not be able to connect
        conf.preSharedKey = Constants.BACKSLASH
                + password + Constants.BACKSLASH;
        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.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.TKIP);
        conf.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.CCMP);
        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    }
    String securityDetails = securityDetailParam;
    if (securityDetails
            .equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_TKIP)) {
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        conf.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.TKIP);
    } else if (securityDetails
            .equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_AES)) {
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        conf.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.CCMP);
    } else if (securityDetails
            .equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_WEP)) {
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    } else if (securityDetails
            .equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_NONE)) {
        conf.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.NONE);
    }
    WifiManager wifiManager = (WifiManager) mContext
            .getSystemService(Context.WIFI_SERVICE);

    int newNetworkId = wifiManager.addNetwork(conf);
        wifiManager.enableNetwork(newNetworkId, true);
        wifiManager.saveConfiguration();
        wifiManager.setWifiEnabled(true);
}

This is working example

KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • let me know if any issues the code execute 100% fine, btw in ur code conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); is **MISSING**, And if ur running your code in lollipop , plz see the updates here-->http://stackoverflow.com/questions/26645943/how-to-change-wifi-advanced-option-from-code-that-chrome-lost-access-to-internet – KOTIOS Feb 04 '15 at 04:33
  • how can we get receiving parameters of `addWifiConfig()` method? I mean from where this method is called? – Kushal Mar 24 '15 at 11:22
  • @Kushal that is provide by user-input or any api that wants "specific wifi configration" should be connect – KOTIOS Mar 24 '15 at 11:46
  • @Diva Can you help to solve http://stackoverflow.com/questions/29232706/connecting-to-mobile-hotspot-network-programmatically-from-android-device] I have added description on this question – Kushal Mar 24 '15 at 13:05
  • 1
    Hello, can i see your "Constants" class to see params? – kemdo Sep 01 '17 at 06:56