我是一名Android开发人员。我正面临着使用WiFi配置的问题。当有人打开我的应用程序时,我需要连接到WiFi。用户必须配置WiFi,并且仅在该应用继续之后。我提供最近的网络列表,用户必须选择他的已知网络。用户必须输入所选网络的密码。如何检查用户是否输入了有效/正确的密码以启用WiFi连接。
请指导我。
提前致谢。
答案 0 :(得分:0)
您可以为BroadcastReceiver
的{{3}}注册WifiManager
。
当Wifi状态发生变化时,您的接收器将被呼叫,以便当用户将手机连接到一个wifi网络。
答案 1 :(得分:0)
好问题。只需尝试连接到已定义的AP并验证结果:
WifiConfiguration wc = new WifiConfiguration();
...
// If password is empty, it should be left untouched
if (!TextUtils.isEmpty(pswd)) {
if (pswd.length() == 64) {
// Goes unquoted as hex
wc.preSharedKey = pswd;
} else {
// Goes quoted as ASCII
wc.preSharedKey = TextUtil.convertToQuotedString(pswd);
}
}
int res = -1;
res = m_WifiManager.addNetwork(wc);
boolean b = m_WifiManager.enableNetwork(res, true);
if(b == true){
fixSupplicant();
m_WifiManager.saveConfiguration();
//success
}
else{
//failed
....
private void fixSupplicant() {
final SupplicantState state = m_WifiManager.getConnectionInfo().getSupplicantState();
boolean isReconnectDone = false;
if ((state == SupplicantState.SCANNING)
|| (state == SupplicantState.DISCONNECTED)
|| (state == SupplicantState.DORMANT)) {
isReconnectDone = m_WifiManager.reconnect();;
}
}