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 a wifi manager. The problem I am facing is that the code claims that the connection to any network is even non-existent. Below is all the code that is launched and called from the SSID of the network. No matter which string you pass to it as the SSID of the network, even if such a network does not exist in any form or form, enableNetwork claims return true, which, in my opinion, means that it is connected to the network.

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

Any ideas / hints / suggestions are welcome.

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;
    }
+5
source share
2 answers

I think the problem is that your code is based on the assumption that connecting to a WiFi network is a synchronous process that is not there. The method is enableNetwork()not blocked during the connection, and then returns the received success or connection failure, it returns immediately, and the result is simply whether the request correctly requested the WiFi service and the supplicant.

, , WifiManager. SUPPLICANT_STATE_CHANGED_ACTION WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION BroadcastReceiver. , , , . , , , DISCONNECTED. , . .

+9

. .

WiFi Android

, Android WiFi. !

0

All Articles