我想通过android应用测试wifi连接,以检查输入的wifi ssid和密码是否正确。我如何检查给定的ssid和密码是否正确?
答案 0 :(得分:1)
这是我们在应用程序中所做的:
WifiConfiguration.SSID = "\"example\""
和WifiConfiguration.preSharedKey = "\"password\""
WifiManager.addNetwork(WifiConfiguration)
)WifiManager.enableNetwork(netId, true)
)我们检查与此WifiConfiguration的协商是否成功。此步骤包含一些丑陋的代码,我很想得到其他一些想法,但是它可以起作用:
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;
}