2014-06-17 92 views
0

因此,在我的應用程序中,我使用SSID和網絡密鑰連接到WiFi網絡。它運行良好,如果使用廣播接收器連接成功,用戶會收到通知。現在我只需要告訴用戶是否有認證問題(即給定的密鑰不正確)。據我所知,WifiInfo類有getSupplicantState()方法,但它沒有提供足夠的Supplicant State來滿足我的需求。任何有關此事的幫助將非常感激。如何判斷WPA密鑰是否不正確Android

這是我目前的Wifi Broadcast Receiver onReceive方法;

@Override 
public void onReceive(Context context, Intent intent) { 
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = conMan.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
     Log.d("WifiReceiver", "Have Wifi Connection"); 
     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
     String scannedSSID = sp.getString("SSID", ""); 

     final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
     final WifiInfo connectionInfo = wifiManager.getConnectionInfo(); 

     String SSID; 
     if (connectionInfo != null) { 
      SSID = connectionInfo.getSSID(); 
      Log.d("WifiReceiver", "SSID is " + SSID); 
      Log.d("WifiReceiver", "Scanned SSID is " + scannedSSID); 
      String compScanned = "\"" + scannedSSID + "\""; 
      if (SSID.equalsIgnoreCase(compScanned)) { 
       Log.d("WifiReceiver", "Connected to " +scannedSSID); 
       Toast.makeText(context, "Connected to " + scannedSSID, Toast.LENGTH_LONG).show(); 
       context.unregisterReceiver(this); 
      } 
     } 

     else 
      return; 
    } 
    else if (netInfo.isConnectedOrConnecting()) { 
     Toast.makeText(context, "Connecting...", Toast.LENGTH_LONG).show(); 
    } 

    else 
     Log.d("WifiReceiver", "Don't have Wifi Connection"); 
} 

回答

1

一種選擇是監視請求者狀態的狀態轉換。例如,我注意到當我輸入一個不好的wifi密碼時,我得到了狀態轉換FOUR_WAY_HANDSHAKE到DISCONNECTED。如果我看到這個轉換,我會在屏幕上標識出一個認證錯誤。

相關問題