Unable to connect with WPA2 android

2019-09-05 04:43发布

I am using following code to connect with WPA2 in android (I can connect with WEP and WPA). But I am getting only 'Scanning' status. And I am unable to connect with WPA2 network. Can you tell me what changes I need to make this code relevant with wpa2 WiFi.

private boolean saveWepConfigAndEnableNetwork(String ssid, String pass) {
    isAlreadyPresend = false;
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + ssid + "\""; // IMP! This should be in Quotes!!
    wc = checkPreviousConfiguration(wc);
    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.DISABLED;
    wc.priority = 40;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wc.preSharedKey = "\"" + pass + "\"";

    wc.wepKeys[0] = "\"" + pass + "\""; // This is the WEP Password
    wc.wepTxKeyIndex = 0;

    boolean res1 = wifi.setWifiEnabled(true);
    int res = 0;
    if(isAlreadyPresend){
        res = wifi.addNetwork(wc);
    }else{
        res = wifi.updateNetwork(wc);
    }

    Log.d("WifiPreference", "add Network returned " + res);
    boolean es = wifi.saveConfiguration();
    Log.d("WifiPreference", "saveConfiguration returned " + es);
    boolean b = wifi.enableNetwork(res, true);
    Log.d("WifiPreference", "enableNetwork returned " + b);
    return b;
}

// Check if this SSID is already stored. If it is, return that
// configuration.
// If not, return the configuration being tested.
public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
    List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
    for (WifiConfiguration config : configs) {
        if (config.SSID.equals(wc.SSID)){
            isAlreadyPresend = true;
            return config;
        }
    }
    return wc;
}

1条回答
虎瘦雄心在
2楼-- · 2019-09-05 04:53

Here is the code which worked for me to connect with WPA2

// Adding a WPA or WPA2 network
public static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) {
    WifiConfiguration config = changeNetworkCommon(ssid);
    // Hex passwords that are 64 bits long are not to be quoted.
    config.preSharedKey = quoteNonHex(password, 64);
    config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
    config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    updateNetwork(wifiManager, config);
}

Code: From Zxing library

查看更多
登录 后发表回答