在Android上连接到特定的wifi有时会失败 [英] Connecting to specific wifi sometimes fails on android

查看:576
本文介绍了在Android上连接到特定的wifi有时会失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用,该应用可以列出ListView中所有可用的wifi.如果我在列表中选择了其中一个wifi,该wifi之前已缓存在List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();中,则它应该连接到它.如果WifiConfiguration列表不包含所选的wifi,则什么都不会发生.我的问题是,有时我会从列表中选择一个wifi(我肯定知道它在WifiConfiguration列表中),但它没有连接到它.相反,它会连接回先前连接的wifi.经过一番尝试(一次又一次选择相同的wifi),它终于连接到它了.这并非总是发生,只是有时.可能是什么问题?这是我的代码段:

I am creating an app, which can list out all the available wifi's in a ListView. If I select one of the wifi's in the list, which was cached before in List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); then it should connect to it. If the WifiConfiguration list doesn't contain the selected wifi, then nothing happens. My problem is, that sometimes I select a wifi from the list (which I know for sure is in the WifiConfiguration list), but it doesn't connects to it. Instead it connects back to the previously connected wifi. After some attempts (selecting again and again the same wifi) it connects to it finally. This doesn't happen always, just sometimes. What can be the problem? Here is my code snippet:

// 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;
     }
}

推荐答案

这就是正在发生的事情.基本上,您可以告诉操作系统禁用网络,也可以告诉操作系统启用网络,但是无法告诉操作系统要连接到哪个网络.

This is what's going on. Basically, you can tell the OS to disable a network, and you can tell the OS to enable a network, but it's not possible to tell the OS what network to connect to.

如果设备中同时配置了多个WiFi接入点(并且都处于enabled状态),则操作系统将决定要连接到哪个WiFi接入点.

If there are multiple WiFi access points in range that are both configured on the device (and both are in the enabled state), the OS will decide which one to connect to.

强制操作系统连接到范围内的一个网络而不是另一个网络的唯一方法是在您不想连接的范围内的网络上调用disableNetwork().

The only way to force the OS to connect to one of the networks in range instead of the other one is to call disableNetwork() on the network that is in range that you don't want to connect to.

让我们逐行浏览代码:

mWifiManager.disconnect();

上面的行告诉操作系统断开与当前连接的WiFi接入点的连接.

The line above tells the OS to disconnect from the currently connected WiFi access point.

mWifiManager.enableNetwork(config.networkId, true);

上面的行告诉设备如果以前处于disabled状态,则将网络设置为enabled状态.

The line above tells the device to set the network to the enabled state if it was previously in the disabled state.

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.,它的运行完全符合预期,因为操作系统正在重新连接到它认为当前有效的访问方式点.

So, when you say Instead it connects back to the previously connected wifi., it's working exactly as expected, as the OS is re-connecting to what it considers the currently active access point.

如果您确实要禁用其他网络,以便操作系统可以连接到您刚刚单击的网络,则可以执行以下操作:

If you really want to disable the other network so that the OS will connect to the one you just clicked on, you could do something like this:

// 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;
     }
}

这篇关于在Android上连接到特定的wifi有时会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆