[英]How to test wifi connection using given ssid and password and returns result of whether given ssid and password is correct or not
I want to test wifi connection through android app to check whether entered wifi ssid and password is correct or not.我想通过android应用程序测试wifi连接以检查输入的wifi ssid和密码是否正确。 How i can check whether the given ssid and password is correct?
如何检查给定的 ssid 和密码是否正确?
Here's what we do in our app:这是我们在我们的应用程序中所做的:
WifiConfiguration.SSID = "\\"example\\""
and WifiConfiguration.preSharedKey = "\\"password\\""
WifiConfiguration.SSID = "\\"example\\""
和WifiConfiguration.preSharedKey = "\\"password\\""
WifiManager.addNetwork(WifiConfiguration)
)WifiManager.addNetwork(WifiConfiguration)
)WifiManager.enableNetwork(netId, true)
)WifiManager.enableNetwork(netId, true)
) We check if the negotiation with this WifiConfiguration was successful.我们检查与此 WifiConfiguration 的协商是否成功。 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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.