我正在工作的Wifi项目,有一个模块,使用户可以编程加入WiFi.
在kitkat和以下它正在成功工作,但在Lollipop它不工作.
这是代码:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = "\"testSSID\""; wifiConfiguration.hiddenSSID = true; wifiConfiguration.status = WifiConfiguration.Status.ENABLED; wifiConfiguration.allowedKeyManagement .set(WifiConfiguration.KeyMgmt.NONE); netId = wifiManager.addNetwork(wifiConfiguration); Log.d("WifiPreference","add Network returned " + netId); boolean checkEnableWifi = wifiManager.enableNetwork(netId,true); Log.d("WifiPreference","enableNetwork returned " + checkEnableWifi);
我的测试设备是nexus 5 build number LRX21O,在我的代码或Lollipop上的错误中有什么问题吗?
解决方法
面对类似的棒棒糖问题.
手动禁用其他网络,然后重新连接的WiFi管理器解决了这个问题.
boolean enableNework(String ssid,Context cxt) { boolean state = false; WifiManager wm = (WifiManager) cxt.getSystemService(Context.WIFI_SERVICE); if (wm.setWifiEnabled(true)) { List<WifiConfiguration> networks = wm.getConfiguredNetworks(); Iterator<WifiConfiguration> iterator = networks.iterator(); while (iterator.hasNext()) { WifiConfiguration wifiConfig = iterator.next(); if (wifiConfig.SSID.equals(ssid)) state = wm.enableNetwork(wifiConfig.networkId,true); else wm.disableNetwork(wifiConfig.networkId); } wm.reconnect(); } return state; }