1

我正在使用以下代码在 android 中连接 WPA2(我可以连接 WEP 和 WPA)。但我只得到“扫描”状态。而且我无法连接到 WPA2 网络。你能告诉我我需要做哪些更改才能使此代码与 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;
}
4

1 回答 1

2

这是我连接 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);
}

代码:来自Zxing 库

于 2012-05-08T09:40:37.527 回答