1

In my application I ask user to select WiFi network and to enter password (if needed). How can I check if password is correct?

Here is my code for connecting to WPA networks

conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
networkPass = input.getText().toString();
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if (list.size()>0)
{
    for (WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
             wifiManager.disconnect();
             wifiManager.enableNetwork(i.networkId, true);
             wifiManager.reconnect();               
             break;
         }           
    }
}
Mykhailo Granik
  • 368
  • 8
  • 23

1 Answers1

2

two option: 1) Use BroadcastReceiver like this.You receive an intent after your success connected. But you will not get one if the password is incorrect. So my suggestion is :

2) check it out later(like after 2s) use the Handler. Because associating and connecting to a WiFi network takes time and the actual task of connecting or associating with the network is done asynchronously in the background. So just check it out later( 1s is too short and 2 second is work for me. You might try other number).

Community
  • 1
  • 1
Jenkyn
  • 199
  • 2
  • 10