在android中删除相同的网络后无法添加网络

时间:2014-12-15 07:35:59

标签: android

在下面的代码中,我有一个用于管理与WifiManager的Wifi连接的类。
我的问题是,当我移除网络时,我无法添加相同的网络,直到关闭和打开手机的无线网络。

public class ConnectWifi {

private Context _ctx;
private WifiManager _wifiManager;

public ConnectWifi(Context ctx) {
    // TODO Auto-generated constructor stub
_ctx=ctx;
_wifiManager= (WifiManager) _ctx.getSystemService(_ctx.WIFI_SERVICE);
}

void ConnectToWifi(String _SSID,String _KeyPass){

    //_wifiManager.setWifiEnabled(true);

    //wpa
    int Id=0;


    Id=wificonfig(_SSID, _KeyPass);

    if(Id!=-1)
     _wifiManager.enableNetwork(Id, true);        

}

void myRemoveNetwork(){


    int ID=_wifiManager.getConnectionInfo().getNetworkId();
    //_wifiManager.disconnect();
    _wifiManager.removeNetwork(ID);
    _wifiManager.saveConfiguration();
        }


int wificonfig(String _SSID,String _KeyPass){
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID =String.format("\"%s\"", _SSID); 
    wc.preSharedKey  = String.format("\"%s\"", _KeyPass);

    wc.hiddenSSID = true;
    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);
    int Id = _wifiManager.addNetwork(wc);
    return Id;
   }
 }

删除后如何添加网络?

1 个答案:

答案 0 :(得分:0)

我不熟悉WifiManager,这对我来说听起来像个错误。但是如果你想删除网络然后再添加它,那么也许你真的想要禁用它并启用它。