连接到特定的无线有时无法在机器人 [英] Connecting to specific wifi sometimes fails in android

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

问题描述

我创建一个应用程序,它可以列出所有可用的无线网络在一个ListView。如果我选择的无线网络的列表,这是之前缓存在名单,其中之一; WifiConfiguration>表= wifiManager.getConfiguredNetworks(); 那么它应该连接到它。如果 WifiConfiguration 列表中不包含所选择的无线网络,然后什么也没有发生。我的问题是,有时我从列表中选择一个WiFi(我知道肯定是在 WifiConfiguration 列表),但它并没有连接到它。相反,它连接回previously连接无线网络。一些尝试后(选择一次又一次同样的WIFI)把它连接到它最后。这并不总是发生,只是有时候。可能是什么问题?这是我的code片断:

  //遍历所有缓存wifis和检查所选GOPRO之前缓存
对于(WifiConfiguration配置:配置){
    //如果被缓存连接到它,这一切
    如果(config.SSID = NULL和放大器;!&安培; config.SSID.equals(\+ mDrawerListView.getAdapter()的getItem(位置)+\)){
        //日志
        Log.i(的onReceive,连接到+ config.SSID);
        mWifiManager.disconnect();
        mWifiManager.enableNetwork(config.networkId,真正的);
        mWifiManager.reconnect();
        打破;
     }
}
 

解决方案

这是发生了什么事情。基本上,你可以告诉操作系统禁用网络,你可以告诉操作系统,使网络,但它不能告诉操作系统什么样的网络连接。

如果有在范围内的多个WiFi接入点的设备上同时配置(无一不是在启用状态),操作系统将决定连接哪一个到。

要强制操作系统连接到范围,而不是另一种网络中的一个,唯一的办法就是叫 disableNetwork()是在范围内的网络上您不想要连接。

让我们通过您的code一行行:

  mWifiManager.disconnect();
 

行上述告诉操作系统从当前连接的WiFi接入点断开连接。

  mWifiManager.enableNetwork(config.networkId,真正的);
 

行上述通知设备的网络设置为启用状态,如果它是$ P pviously $在禁用状态。

  mWifiManager.reconnect();
 

文档

  

重新连接到当前活动的接入点,如果我们目前   断开。这可能导致状态的异步传送   更改事件。

所以,当你说的相反,它连接回previously连接无线网络。,它的工作完全符合预期,因为OS重新连接到它所认为的当前活动的接入点

如果你真的想禁用其他网络,使操作系统将连接到您刚才点击的一个,你可以做这样的事情:

  //遍历所有缓存wifis和检查所选GOPRO之前缓存

WifiInfo信息= mWifiManager.getConnectionInfo(); //获取WifiInfo
INT的id = info.getNetworkId(); //获取当前连接的网络ID

对于(WifiConfiguration配置:配置){
    //如果被缓存连接到它,这一切
    如果(config.SSID = NULL和放大器;!&安培; config.SSID.equals(\+ mDrawerListView.getAdapter()的getItem(位置)+\)){
        //日志
        Log.i(的onReceive,连接到+ config.SSID);

        mWifiManager.disconnect();

        mWifiManager.disableNetwork(ID); //禁用当前网络

        mWifiManager.enableNetwork(config.networkId,真正的);
        mWifiManager.reconnect();
        打破;
     }
}
 

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.

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.

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.

Let's go through your code line by line:

mWifiManager.disconnect();

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

mWifiManager.enableNetwork(config.networkId, true);

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

mWifiManager.reconnect(); 

From the documentation:

Reconnect to the currently active access point, if we are currently disconnected. This may result in the asynchronous delivery of state change events.

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

这篇关于连接到特定的无线有时无法在机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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