Android的wifimanager始终返回true [英] Android wifimanager always returns true

查看:343
本文介绍了Android的wifimanager始终返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的命和任何帮助将大大AP preciated。

我想用无线管理器,连接到一个开放的网络。我遇到的问题是,code声称连接到任何网络 - 即使不存在的。下面是一个执行和被调用与网络的SSID整个code。不要紧,你传递什么样的字符串作为一个网络的SSID,即使没有这样的网络存在于任何形状或形式,在enableNetwork声称返回true,我认为意味着它连接到网络。

我需要做的是确保我有一个连接。因此,如果我通过一个不存在的网络的SSID(例如,它是超出范围)的API应该返回尝试连接时失败。

任何想法/提示/建议将不胜AP preciated。

 公共布尔conto(串网){

    WifiConfiguration wifiConfiguration =新WifiConfiguration();
    无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    名单< WifiConfiguration>的configs = NULL;
    INT inetId = -1;


    //确保有在配置没有有趣的东西
    的configs = wifi.getConfiguredNetworks();
    对于(WifiConfiguration配置:的configs){
        wifi.removeNetwork(config.networkId);
        Log.d(********,删除网络:SSID = [+ config.SSID +]和ID = [+ config.networkId +]);

    }

    //现在添加网络
    wifiConfiguration.SSID =\+网络+\;
    wifiConfiguration.hiddenSSID = FALSE;
    //wifiConfiguration.priority = 1;
    //wifiConfiguration.networkId = 999;

    inetId = wifi.addNetwork(wifiConfiguration);
    如果(inetId℃,){
            Log.d(********,不能添加网络......... [+ wifiConfiguration.SSID +]);

        }
        其他 {

            Log.d(********,添加的网络......... [+ wifiConfiguration.SSID +]);

            //让偏执和仔细检查配置文件
            Log.d(********,+++++++++++++++++++++++++这是我的配置文件 );
            的configs = wifi.getConfiguredNetworks();
            对于(WifiConfiguration配置:的configs){
                Log.d(********,在配置文件后添加,SSID =+ config.SSID +],ID =+ config.networkId +]);

            }

            //现在启用网络
            布尔successConnected = wifi.enableNetwork(inetId,真正的);
            //布尔successAssociated = wifi.reassociate();这并没有改变的结果

            如果(successConnected){
                Log.d(********,连接到......... [+ inetId +]);
            }
            其他 {

                Log.d(********,无法连接到......... [+ inetId +]);

            }


        }
        返回false;
    }
 

解决方案

我觉得问题是,你的code是基于这样的假设连接到WiFi网络是一个同步的过程,它不是。该方法 enableNetwork()不同时连接时发生块,然后返回结果的成功连接或失败,它会立即返回,结果是请求简直是否正确到达WiFi服务和请求者

如果你想洞察什么与设备的连接状态怎么回事,你需要监视 WifiManager。 SUPPLICANT_STATE_CHANGED_ACTION WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION 的BroadcastReceiver 广播意图的行动。一旦你开始连接过程中,这些回调会告诉你事情的进展。例如,如果你传递了一个不存在的网络,连接状态应该立即直接进入断开。而有效的网络将经过关联,认证,和连接的过程。所有这些步骤都通过这些广播列举

This is killing me and any help would be greatly appreciated.

I want to connect to an open network using wifi manager. The problem I am having is that the code claims connection to any network - even non-existing ones. Below is the entire code that gets executed and gets called with the SSID of a network. It does not matter what string you pass to it as the SSID of a network, even if no such network exists in any shape or form, the enableNetwork claims returns true, which I believe means it connected to the network.

What I need to do is to make sure I have a connection. So if I pass a network SSID that does not exist (for example, it is out of range) the API should return a failure when attempting to connect.

Any ideas/hints/suggestions would be greatly appreciated.

public boolean conto (String network){

    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    List<WifiConfiguration> configs = null;
    int inetId = -1;


    // make sure there are no funny stuff in the config
    configs = wifi.getConfiguredNetworks();
    for (WifiConfiguration config : configs) {
        wifi.removeNetwork(config.networkId);
        Log.d("********", "Removed Network: SSID=[" + config.SSID + "] and ID=[" + config.networkId + "]");

    }

    // Now add the network
    wifiConfiguration.SSID = "\"" + network + "\"";
    wifiConfiguration.hiddenSSID = false;
    //wifiConfiguration.priority = 1;
    //wifiConfiguration.networkId = 999;

    inetId = wifi.addNetwork(wifiConfiguration); 
    if(inetId < 0) {
            Log.d("********", "Could Not Add Network......... [" + wifiConfiguration.SSID + "]"); 

        } 
        else { 

            Log.d("********", "Added Network......... [" + wifiConfiguration.SSID + "]");

            // Lets be paranoid and double check the config file
            Log.d("********", " +++++++++++++++++++++++++ This is what I have in Config File");
            configs = wifi.getConfiguredNetworks();
            for (WifiConfiguration config : configs) {
                Log.d("********", "In the Config file after add, SSID=[" + config.SSID + "], ID=[" + config.networkId + "]");

            }

            // Now Enable the network
            boolean successConnected = wifi.enableNetwork(inetId, true); 
            //boolean successAssociated = wifi.reassociate(); This did not change the results

            if(successConnected) { 
                Log.d("********", "Connected to......... [" + inetId + "]");
            } 
            else { 

                Log.d("********", "Could Not Connect to......... [" + inetId + "]"); 

            } 


        }
        return false;
    }

解决方案

I think the problem is that your code is based on the assumption that connecting to a WiFi network is a synchronous process, which it is not. The method enableNetwork() does not block while the connection occurs and then return the resulting success or failure of the connection, it returns immediately and the result is simply whether the request properly reached the WiFi service and the supplicant.

If you want visibility into what is going on with the connection status of the device, you need to monitor the WifiManager. SUPPLICANT_STATE_CHANGED_ACTION and WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION broadcast Intent actions with a BroadcastReceiver. Once you initiate the connection process, these callbacks will tell you how things progress. For example, if you pass in a network that doesn't exist, the connection status should almost immediately go straight to DISCONNECTED. While a valid network will go through the process of associating, authenticating, and connecting. All these steps are enumerated through these broadcasts.

这篇关于Android的wifimanager始终返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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