[英]How to check if password for WiFi network is correct in Android?
In my application I ask user to select WiFi network and to enter password (if needed). 在我的应用程序中,我要求用户选择WiFi网络并输入密码(如果需要)。 How can I check if password is correct?
如何检查密码是否正确?
Here is my code for connecting to WPA networks 这是我连接到WPA网络的代码
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;
}
}
}
two option: 1) Use BroadcastReceiver like this .You receive an intent after your success connected. 两个选项:1)像这样使用BroadcastReceiver。连接成功后你会收到一个意图。 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. 2)稍后检查(如2s后)使用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.
因为关联和连接到WiFi网络需要花费时间,并且连接或与网络关联的实际任务是在后台异步完成的。 So just check it out later( 1s is too short and 2 second is work for me. You might try other number).
所以稍后再检查一下(1s太短,2秒对我有效。你可以尝试其他号码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.