WifiManager.addNetwork成功但enableNetwork失败

时间:2013-11-12 22:12:14

标签: android android-wifi

我可以配置和添加网络(WPA2-PSK),但是当我尝试启用它时(使用WifiManager.enableNetwork),我收到了失败。调用之间的日志中没有有用的调试信息(W/ActivityManager﹕ Activity idle timeout除外)。这可能是什么?

以下是代码:

        String quotedSSID = "\"" + networkSSID + "\"";

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = quotedSSID;
        conf.preSharedKey = "\""+ networkPass +"\"";
        conf.hiddenSSID = true;
        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

        WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

        Log.w(MainActivity.APP_TAG, "Adding " + conf.SSID);
        int networkId = wifiManager.addNetwork(conf);
        if (networkId < 0) {
            Log.e(MainActivity.APP_TAG, "Failed to configure " + conf.SSID);
        }
        if (!wifiManager.enableNetwork(conf.networkId, true)) {
            Log.e(MainActivity.APP_TAG, "Failed to enable " + conf.SSID);
        } else {
            Log.i(MainActivity.APP_TAG, "Enabled " + conf.SSID);
        }

1 个答案:

答案 0 :(得分:3)

回答我自己的问题。启用网络时,我应该使用新的网络ID,即networkId而不是conf.networkId