我可以配置和添加网络(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);
        }

最佳答案

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

10-01 13:14