1

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

1 回答 1

3

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

于 2013-11-13T05:02:02.157 回答