简体   繁体   English

如何在Android中检查WiFi网络的密码是否正确?

[英]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.

相关问题 如何在Android应用程序中检查WiFi网络是否受浏览器密码保护? - How can I check if wifi network is protected with browser password check inside Android application? Android - 看看wifi的密码是否正确 - Android - see if password to wifi was correct 在android中连接到wifi网络,如果密码不正确则返回 - Connect to wifi network in android, return if incorrect password 如何在android 10中验证wifi密码并连接/断开特定wifi网络 - How to validate password of wifi and connect/disconnect specific wifi network in android 10 android - 如何在没有密码的情况下检查设备是否已连接到wifi? - android - how to check if device has connected to wifi without password? Android,如何在连接到wifi网络时覆盖互联网连接检查? - Android, how to override the internet connectivity check when connecting to wifi network? Android设备上如何检查wifi或3g网络 - how to check wifi or 3g network is available on android device Android:检查wifi密码有效性,无需连接到预先配置的网络 - Android: check wifi password validity without connecting to pre-configured network 如何知道当前WIFI网络是否设置了密码? - How to know if password is set for current WIFI network? 如何以编程方式更改 Android 中保存的 wifi 网络的密码(API 级别 23,Android 6.0) - How to programmatically change password of a saved wifi network in Android (API level 23, Android 6.0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM