无法连接WPA2 android

2exbekwf  于 2023-03-28  发布在  Android
关注(0)|答案(2)|浏览(200)

我正在使用以下代码在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;
}
gkn4icbw

gkn4icbw1#

下面是我连接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 library

3pmvbmvn

3pmvbmvn2#

只是为了丰富上面的优秀答案,
Android支持不同的WIFI安全机制,如WEPWPA/WPA2,这些机制在IEEE 802.11规范中有描述。Android WifiManager API允许您设置应用程序的WIFI网络参数,以便使用特定协议连接到特定接入点(您的wifi盒子)。
例如,在WPA/WPA2的情况下,通过执行密钥质询握手来授权WPA/WPA2网络上的连接。现在,要成功连接到WPA或WPA2网络 *,您只需添加设备在连接时将尝试使用的支持的协议、加密方法和密钥管理方法 *,否则它们将没有机会相互理解。
如果您使用预共享密钥作为WPA/WPA2的身份验证(在真实的世界的大多数WiFi热点中使用),那么您基本上使用的是设备和接入点预先知道的对称密钥短语,假设您使用WPA/WPA2网络(不是WEP也不是Enterprise WPA2服务器crt身份验证)
记住WPA和WPA2在包的密码类型上不同,第一个通常使用TKIP,另一个使用AES(类似CCMP),这更安全。也就是说,您可以使用相同的WIFI保护标准来混淆密码方法的组合,例如

WPA2:
NETWORK USE KEY CCMP=[WPA2-PSK-CCMP][ESS]
WPA:
NETWORK USE KEY CCMP=[WPA-PSK-CCMP][ESS]
NETWORK USE KEY TKIP=[WPA-PSK-TKIP+CCMP][ESS]
NETWORK USE KEY TKIP=[WPA-PSK-TKIP][ESS]

因此,如果您有兴趣使用上述关键词访问WPA和WPA2网络,最好的方法是显式添加相应的支持配置选项,以便尽可能开放您想要潜在连接的网络类型,一旦配置完成,您只需添加应用使用的网络并开始连接挑战。
你可以使用这个代码,它花了我一段时间,但它的工作很棒!为***WPA和WPA2***使用相同的wifi配置。

public static boolean addNetwork8(Context context, String ssid, String password) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // setup a wifi configuration
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + ssid + "\"";
    wc.preSharedKey = "\""+ password +"\"";
    wc.status = WifiConfiguration.Status.ENABLED;
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    // connect to and enable the connection
    int netId = wifiManager.addNetwork(wc);
    wifiManager.enableNetwork(netId, true);
    wifiManager.setWifiEnabled(true);

    return true;
}

然后,您可以在需要连接到WPA/WPA2网络的任何位置调用该方法

boolean added = Connectivity.addNetwork8(this, network, password);
    Log.d(TAG, "Network added " + added);

享受,工作代码!!

**PS:**我不需要指定authalgorithm config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);对于WAP/WAP 2,我假设这是因为它使用x 0,幸运的是。

/** Open System authentication (required for WPA/WPA2) */public static 
final int OPEN = 0;

相关问题