android 连接到特定 wifi 有时会失败
Connecting to specific wifi sometimes fails on android
我正在创建一个应用程序,它可以在 ListView 中列出所有可用的 wifi。如果我 select 列表中的 wifi 之一,之前缓存在 List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
中,那么它应该连接到它。如果 WifiConfiguration
列表不包含 selected wifi,则什么也不会发生。我的问题是,有时我 select 列表中的 wifi(我确定它在 WifiConfiguration
列表中),但它没有连接到它。相反,它会连接回之前连接的 wifi。经过一些尝试(select 一次又一次地连接同一个 wifi),它终于连接到了它。这并不总是发生,只是有时会发生。可能是什么问题?这是我的代码片段:
// Go through all the cached wifis and check if the selected GoPro was cached before
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.enableNetwork(config.networkId, true);
mWifiManager.reconnect();
break;
}
}
这是怎么回事。基本上,您可以告诉 OS 禁用网络,也可以告诉 OS 启用网络,但无法告诉 OS 连接到哪个网络。
如果范围内有多个 WiFi 接入点都在设备上配置(并且都处于 enabled
状态),OS 将决定连接到哪个。
强制 OS 连接到范围内的网络之一而不是另一个网络的唯一方法是在您不在范围内的网络上调用 disableNetwork()
想连接到。
让我们逐行检查您的代码:
mWifiManager.disconnect();
上面的行告诉 OS 断开与当前连接的 WiFi 接入点的连接。
mWifiManager.enableNetwork(config.networkId, true);
如果设备之前处于 disabled
状态,则上面的行告诉设备将网络设置为 enabled
状态。
mWifiManager.reconnect();
Reconnect to the currently active access point, if we are currently
disconnected. This may result in the asynchronous delivery of state
change events.
所以,当你说 相反,它会连接回之前连接的 wifi。,它的工作完全符合预期,因为 OS 正在重新连接到什么它考虑当前活动的接入点。
如果您真的想禁用其他网络以便 OS 连接到您刚刚点击的网络,您可以这样做:
// 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;
}
}
您好,您可以将要连接的网络的优先级更改为大于所有其他已配置网络的优先级,然后在您重新连接()时更改。它将连接到范围内优先级最高的网络。
wificonfig.priority = 10000;
wifiManager.updateNetwork(wificonfig);
wifiManager.saveConfiguration();
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, false);
wifiManager.reconnect();
我正在创建一个应用程序,它可以在 ListView 中列出所有可用的 wifi。如果我 select 列表中的 wifi 之一,之前缓存在 List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
中,那么它应该连接到它。如果 WifiConfiguration
列表不包含 selected wifi,则什么也不会发生。我的问题是,有时我 select 列表中的 wifi(我确定它在 WifiConfiguration
列表中),但它没有连接到它。相反,它会连接回之前连接的 wifi。经过一些尝试(select 一次又一次地连接同一个 wifi),它终于连接到了它。这并不总是发生,只是有时会发生。可能是什么问题?这是我的代码片段:
// Go through all the cached wifis and check if the selected GoPro was cached before
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.enableNetwork(config.networkId, true);
mWifiManager.reconnect();
break;
}
}
这是怎么回事。基本上,您可以告诉 OS 禁用网络,也可以告诉 OS 启用网络,但无法告诉 OS 连接到哪个网络。
如果范围内有多个 WiFi 接入点都在设备上配置(并且都处于 enabled
状态),OS 将决定连接到哪个。
强制 OS 连接到范围内的网络之一而不是另一个网络的唯一方法是在您不在范围内的网络上调用 disableNetwork()
想连接到。
让我们逐行检查您的代码:
mWifiManager.disconnect();
上面的行告诉 OS 断开与当前连接的 WiFi 接入点的连接。
mWifiManager.enableNetwork(config.networkId, true);
如果设备之前处于 disabled
状态,则上面的行告诉设备将网络设置为 enabled
状态。
mWifiManager.reconnect();
Reconnect to the currently active access point, if we are currently disconnected. This may result in the asynchronous delivery of state change events.
所以,当你说 相反,它会连接回之前连接的 wifi。,它的工作完全符合预期,因为 OS 正在重新连接到什么它考虑当前活动的接入点。
如果您真的想禁用其他网络以便 OS 连接到您刚刚点击的网络,您可以这样做:
// 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;
}
}
您好,您可以将要连接的网络的优先级更改为大于所有其他已配置网络的优先级,然后在您重新连接()时更改。它将连接到范围内优先级最高的网络。
wificonfig.priority = 10000;
wifiManager.updateNetwork(wificonfig);
wifiManager.saveConfiguration();
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, false);
wifiManager.reconnect();