Android:暂时连接到WiFi网络?

时间:2012-11-28 09:44:47

标签: android wifi wlan

当我使用此代码连接到8open)WLAN网络时:

WifiConfiguration wifiConfig = new WifiConfiguration(); 
wifiConfig.BSSID =result.BSSID; // BSSID of detected network
wifiConfig.priority = 1; 
wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE); 
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
wifiConfig.status=WifiConfiguration.Status.ENABLED; 

int netId = scanData.wifiManager.addNetwork(wifiConfig); 
scanData.wifiManager.enableNetwork(netId, true);

...此WiFi连接存储在首选网络列表中。因此,当我经常尝试时,我会得到一个非常庞大的连接列表 - 这就是我不想要的东西。

所以我的问题是:如何暂时建立与WLAN网络的连接并让Android永久存储它?或者有没有办法在不再需要的情况下删除这样的网络?

谢谢!

2 个答案:

答案 0 :(得分:1)

断开与网络的连接后,只需将其从存储列表中删除即可。详情请见In Android, How do you clear the WI-FI list of networks in Settings via Java?

答案 1 :(得分:0)

你可以搜索wifiConfig是否已经保存,然后在连接之前将其删除:像这样:

List <WifiConfiguration> wifiConf = wifiManager.getConfiguredNetworks();
int netId;
for(WifiConfiguration wf: wifiConf){
    if(wf.BSSID.equals(desiredBSSID)){
        netId = wf.networkId;
        wifiManager.removeNetwork(netId);
    }
}