Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android wifimanager always returns true

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;
    }
like image 369
Bob Avatar asked Mar 10 '11 19:03

Bob


1 Answers

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.

like image 62
devunwired Avatar answered Sep 27 '22 17:09

devunwired