Android wifimanager总是返回true

发布于 2024-10-21 04:31:45 字数 2274 浏览 2 评论 0原文

这简直要了我的命,任何帮助将不胜感激。

我想使用 wifi 管理器连接到开放网络。我遇到的问题是该代码声称连接到任何网络 - 甚至是不存在的网络。下面是执行并使用网络 SSID 调用的完整代码。无论您将什么字符串作为网络的 SSID 传递给它,即使不存在任何形状或形式的此类网络,enableNetwork 声明都会返回 true,我认为这意味着它已连接到该网络。

我需要做的是确保我有联系。因此,如果我传递的网络 SSID 不存在(例如,超出范围),则 API 在尝试连接时应返回失败。

任何想法/提示/建议将不胜感激。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-10-28 04:31:45

我认为问题在于您的代码基于这样的假设:连接到 WiFi 网络是一个同步过程,但事实并非如此。 enableNetwork() 方法在连接发生时不会阻塞,然后返回连接成功或失败的结果,它立即返回,结果只是请求是否正确到达 WiFi 服务和请求者。

如果您想了解设备连接状态的情况,则需要监视 WifiManager。 SUPPLICANT_STATE_CHANGED_ACTION 和 WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION 使用 BroadcastReceiver 广播 Intent 操作。一旦您启动连接过程,这些回调将告诉您事情的进展情况。例如,如果您传入一个不存在的网络,则连接状态几乎应立即直接变为“DISCONNECTED”。而一个有效的网络会经历关联、认证、连接的过程。所有这些步骤都是通过这些广播枚举的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文