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 に答える