4

尝试以编程方式从 Android 连接到我的无线网络。

安全类型是 WPA2,加密 AES。

这不能按预期工作:

private WifiConfiguration saveWepConfig(String password, String networkSSID) {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";
        // conf.wepKeys[0] = "\"" + password + "\"";
        conf.preSharedKey = "\"" + password + "\"";
        conf.wepTxKeyIndex = 0;
            conf.hiddenSSID = true;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        return conf;
    }

我必须在这里加密密码吗?它只保存连接,不连接。

4

2 回答 2

8

啊...我在发布问题后立即找到了解决方案:

    WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";
        conf.preSharedKey = "\"" + password + "\"";
//      conf.hiddenSSID = true;
//      conf.wepTxKeyIndex = 0;
//      conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//      conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        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);

它对我有用。谢谢。

于 2013-11-02T22:40:10.850 回答
8

您只需要设置SSIDpreSharedKey。其他一切都默认为 WPA/WPA2(对于真正的旧版本的 Android 可能不是真的)。

public static void saveWpaConfig(Context context, String ssid, String passphrase)
{
    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifiConfiguration.SSID = "\"" + ssid + "\"";
    wifiConfiguration.preSharedKey = "\"" + passphrase + "\"";

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int networkId = wifiManager.addNetwork(wifiConfiguration);
    if (networkId != -1)
    {
        wifiManager.enableNetwork(networkId, true);
        // Use this to permanently save this network
        // Otherwise, it will disappear after a reboot
        wifiManager.saveConfiguration();
    }
}
于 2015-08-28T17:28:55.607 回答