Stepan Maksymov
Stepan Maksymov

Reputation: 2606

Checking wifi credentials if WIFI SSID was already connected on a device

Situation: 1) I have android device already successfully connected to my SSID so password is stored, and it connects well. 2) In my application I have option for user to input wifi credential for this network (SSID is determined here no problem, but password without root cannot be determined, so user input it manually)

Problem: After user input wifi password - i need to check if password is correct - only the way to check this is connect to this wifi network - How to do this correctly?

What tried: When I disconnect from wifi ssid, and i add config for wifi with credential user input - even if it's bad password - wifi reconnects successful, coz i guess previously entered credentials from config outside of my application was correct. So whatever password user will input - in connects in anyway and i cannot know if credential user entered is correct.

Any help - welcome! Thanks.

Upvotes: 0

Views: 560

Answers (1)

Stepan Maksymov
Stepan Maksymov

Reputation: 2606

Finally guys after several hours of research was able to determine next:

Pre conditions: 1) device is connected to wifi network with standard device options with right password; 2) M trying to check user entered credentials for this wifi network.

Results: Warn! (only exact flow of actions is working for me):

1) disconnect from wifi:

wifiManager.disconnect();

2) create configuration:

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + wifiSsid + "\"";
if(authType == 0){
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if(authType == 1) {
    conf.preSharedKey = "\""+ wifiPassword+"\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else {
    conf.preSharedKey = "\""+ wifiPassword+"\"";
}
conf.status = WifiConfiguration.Status.ENABLED;

3) disable and remove all configs (actually they will not be removed if not created in your aplication):

 List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
 for (WifiConfiguration i : list) {
     wifiManager.disableNetwork(i.networkId);
     wifiManager.removeNetwork(i.networkId);
 }

4) add created config:

final int networkId = wifiManager.addNetwork(conf);

5) Activate config but only with this command:

wifiManager.enableNetwork(networkId, true);

What is in result, if wifi password in config is correct you get connection to wifi in around 2 - 4 seconds, but if password from wifi in config is wrong - you get connected (if this network was connected before via settings) much longer around 8 - 12 seconds.

Here is you are not connected in 2-4 seconds, you can write that wifi credentials check failed or connect to network failed.

I requested from google developers feature to wifi manager to check credentials ability, But for now for me this is all that could be done.

Guys, any other solutions welcome!

Upvotes: 1

Related Questions