簡體   English   中英

如何使用給定的 ssid 和密碼測試 wifi 連接並返回給定 ssid 和密碼是否正確的結果

[英]How to test wifi connection using given ssid and password and returns result of whether given ssid and password is correct or not

我想通過android應用程序測試wifi連接以檢查輸入的wifi ssid和密碼是否正確。 如何檢查給定的 ssid 和密碼是否正確?

這是我們在我們的應用程序中所做的:

  1. 我們確保 wifi 已啟用
  2. 我們使用 ssid 和 pw 創建一個 WifiConfiguration(注意 WifiConfiguration.SSID 和 WifiConfiguration.preSharedKey 用引號括起來,這樣如果 ssid 是 example 並且 pw 是密碼,那么WifiConfiguration.SSID = "\\"example\\""WifiConfiguration.preSharedKey = "\\"password\\""
  3. 我們將 WifiConfiguration 添加到 WifiManager ( WifiManager.addNetwork(WifiConfiguration) )
  4. 我們檢查返回值(它是一個 networkId),如果它是 -1 則操作不成功
  5. 我們告訴 WifiManager 啟用上一步中的 networkId 並嘗試連接( WifiManager.enableNetwork(netId, true)
  6. 我們檢查與此 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; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM