如何忘记目前不可用的wifi?

时间:2018-12-19 07:11:13

标签: android wifi android-wifi

我想用此代码示例忘记wifi网络,但它不能忘记不在范围内的wifi。

channelRegistered(...)

如何删除网络?

UPD1:

我说的是我自己添加的WIFI信息。

private void removeNetwork(WifiManager wifiManager, WifiConfiguration wc) {
        if (!wifiManager.isWifiEnabled()) enableWifi();
        int id = wifiManager.addNetwork(wc);
        wifiManager.enableNetwork(id, true);
        wifiManager.removeNetwork(id);
        wifiManager.saveConfiguration();
    }

1 个答案:

答案 0 :(得分:2)

我测试了此代码示例,并且代码运行良好,这与您实施此代码有误。

这取决于wifi配置,这是我的工作示例。

private void WPA(String networkSSID, String networkPass, WifiManager wifiManager, String command) {
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"" + networkSSID + "\"";
        wc.preSharedKey = "\"" + networkPass + "\"";
        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);
        if (command.equals("add")) {
            addNetwork(wifiManager, wc);
        }else  if (command.equals("remove")) {
            removeNetwork(wifiManager,wc);
        }
    }