I want to test wifi connection through android app to check whether entered wifi ssid and password is correct or not. How i can check whether the given ssid and password is correct?
Asked
Active
Viewed 1,281 times
1
-
by trying to connect th that network. If it connects - then ssid/password is correct – Vladyslav Matviienko Aug 16 '18 at 05:57
-
@VladyslavMatviienko If it connects but there's no Internet access, can you still tell? – wizzwizz4 Aug 16 '18 at 05:58
-
yes, you don't need to check internet access, just check the wifi connection state – Vladyslav Matviienko Aug 16 '18 at 05:58
-
once you connect the wifi you can get its states and all details like ssid etc.if its connected then its correct else its not. – Vishal Thakkar Aug 16 '18 at 06:00
1 Answers
1
Here's what we do in our app:
- We ensure that the wifi is enabled
- We create a WifiConfiguration with the ssid and pw (note that WifiConfiguration.SSID and WifiConfiguration.preSharedKey are surrounded in quotes so that if the ssid is example and the pw is password then
WifiConfiguration.SSID = "\"example\""
andWifiConfiguration.preSharedKey = "\"password\""
- We add the WifiConfiguration to the WifiManager (
WifiManager.addNetwork(WifiConfiguration)
) - We check the return value (which is a networkId) if it is -1 then the operation was not successful
- We tell the WifiManager fo enable the networkId from the previous step and attempt to connect (
WifiManager.enableNetwork(netId, true)
) We check if the negotiation with this WifiConfiguration was successful. This step has some ugly code, and I would love to get some other ideas, but it works:
private static boolean checkWifiNegotiation(WifiManager wifiManager, int netId) { boolean startedHandshake = false; boolean successful = false; for (int i = 0; i < 30; i++) { try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } SupplicantState currentState = wifiManager.getConnectionInfo().getSupplicantState(); if (!startedHandshake && currentState.equals(SupplicantState.FOUR_WAY_HANDSHAKE)) { startedHandshake = true; } else if (startedHandshake) { if (currentState.equals(SupplicantState.DISCONNECTED)) { break; } else if (currentState.equals(SupplicantState.COMPLETED)) { successful = true; break; } } wifiManager.enableNetwork(netId, true); } // no matter what happened above, if COMPLETED then we have the correct pw if (!successful && wifiManager.getConnectionInfo().getSupplicantState().equals(SupplicantState.COMPLETED)) { successful = true; } return successful; }

MidasLefko
- 4,499
- 1
- 31
- 44
-
-
We assume that if the wifi is on and the network is added and enabled then if the negotiation is not successful it must be a bad pw – MidasLefko Aug 16 '18 at 11:45
-
When we get the netId as -1?. And because of netId = -1 the the statement wifiManager.enableNetwork(netId, true); is not evaluating. – pooja Aug 16 '18 at 12:19
-
statements after wifiManager.enableNetwork(netId, true); in checkWifiNegotiation() function are not execute. – pooja Aug 16 '18 at 12:33
-
-
If you get -1 then you were not able to add the wificonfiguration. There are many reasons why this might be happening. Check [here](https://stackoverflow.com/questions/12016918/android-wifimanager-addnetwork-returns-1) – MidasLefko Aug 16 '18 at 17:01
-
-
-
But we need to reset the network setting to return successful result, without reset it reurns false value for correct credentials why there is need of reset network settings. – pooja Aug 24 '18 at 06:42