android 开启wifi失败,连接到特定的WiFi有时失败android
这就是发生了什么事。基本上,您可以告诉操作系统禁用网络,并且可以告诉操作系统启用网络,但无法告诉操作系统连接哪个网络。如果设备上配置了两个范围内的多个WiFi接入点(且两者均处于enabled状态),则操作系统将决定要连接哪一个WiFi接入点。强制操作系统连接到其中一个网络而不是另一个网络的唯一方法是在您不想连接的范围内的网络上拨打disableNetwork()。让我们通过代码逐行:mWifiM
这就是发生了什么事。基本上,您可以告诉操作系统禁用网络,并且可以告诉操作系统启用网络,但无法告诉操作系统连接哪个网络。
如果设备上配置了两个范围内的多个WiFi接入点(且两者均处于enabled状态),则操作系统将决定要连接哪一个WiFi接入点。
强制操作系统连接到其中一个网络而不是另一个网络的唯一方法是在您不想连接的范围内的网络上拨打disableNetwork()。
让我们通过代码逐行:
mWifiManager.disconnect();
线之上告诉操作系统从当前连接的WiFi接入点断开连接。
mWifiManager.enableNetwork(config.networkId, true);
线之上告诉设备到网络设置为enabled状态,如果它在disabled状态以前。
mWifiManager.reconnect();
重新连接到当前工作中的接入点,如果我们目前 断开。这可能会导致异步传递状态 更改事件。
所以,当你说而是它连接回到以前连接的WiFi。,它的工作与预期完全一致,因为操作系统重新连接到它认为的当前活动接入点。
如果你真的要禁用其他网络,使操作系统将连接到您刚才点击的一个,你可以做这样的事情:
// Go through all the cached wifis and check if the selected GoPro was cached before
WifiInfo info = mWifiManager.getConnectionInfo(); //get WifiInfo
int id = info.getNetworkId(); //get id of currently connected network
for (WifiConfiguration config : configurations) {
// If it was cached connect to it and that's all
if (config.SSID != null && config.SSID.equals("\"" + mDrawerListView.getAdapter().getItem(position) + "\"")) {
// Log
Log.i("onReceive", "Connecting to: " + config.SSID);
mWifiManager.disconnect();
mWifiManager.disableNetwork(id); //disable current network
mWifiManager.enableNetwork(config.networkId, true);
mWifiManager.reconnect();
break;
}
}
更多推荐
所有评论(0)