philleonard
philleonard

Reputation: 45

How to tell if WPA key is incorrect Android

So in my application I am connecting to a WiFi network given the SSID and the network key. It works well, and the user is notified if the connection is successful using a Broadcast Receiver. Now I just need to be able to tell the user if there is an authentication problem (i.e. the given key is incorrect). I understand there is a getSupplicantState() method for the WifiInfo class but it doesn't provide a sufficient Supplicant State for what I need. Any help around the matter would be much appreciated.

Here is my current Wifi Broadcast Receiver onReceive method;

@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");
}

Upvotes: 0

Views: 384

Answers (1)

Michael
Michael

Reputation: 524

One option is to monitor the state transitions of the supplicant states. For example, I noticed when I put in a bad wifi password, I got the state transition FOUR_WAY_HANDSHAKE to DISCONNECTED. If I see this transition, I mark the network with an authentication error on the screen.

Upvotes: 1

Related Questions