Reputation: 179
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?
Upvotes: 1
Views: 1634
Reputation: 4559
Here's what we do in our app:
WifiConfiguration.SSID = "\"example\""
and WifiConfiguration.preSharedKey = "\"password\""
WifiManager.addNetwork(WifiConfiguration)
)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;
}
Upvotes: 1