hubert
hubert

Reputation: 933

Android - see if password to wifi was correct

I plan to make an app that checks if a certain wifi network uses common password. I have a list of most common passwords, but the following function:

public boolean connectTo(String psKey){
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + dbw.getSsid() + "\"";
    wc.preSharedKey  = "\"" + psKey + "\"";

    wc.status = WifiConfiguration.Status.ENABLED;   
    wc = configureCapabs(wc, dbw.getCapability());

    int res = wifi.addNetwork(wc);
    Toast.makeText(this, "add Network returned " + res , Toast.LENGTH_SHORT).show();
    boolean b = wifi.enableNetwork(res, true);        
    Toast.makeText(this, "enableNetwork returned " + b , Toast.LENGTH_SHORT).show();
    if(!b) return false;
    boolean fin = /*b ? wifi.reassociate() : */wifi.reconnect();
    return fin;
}

it returns true, even if the password was incorrect. Is there a way of checking if the password that I tried to log in with was accepted or rejected?

Upvotes: 4

Views: 7523

Answers (2)

Paul
Paul

Reputation: 5223

You are using the reconnect() / reassociate() methods of WiFiManager to check weather or not the connection succeeded but the boolean value that they return means something else. The returned value only tells you about the result of STARTING a given operation. This is because associating and connecting to a WiFi network takes time. These methods however will return instantly and will not block. The actual task of connecting or associating with the network is done asynchronously in the background.

You can monitor the what is happening with the WiFi connection by listening to a specific system wide broadcast:

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>

You can find more info about this HERE - Check Torsten's answer.

Upvotes: 5

onaclov2000
onaclov2000

Reputation: 5851

As far as checking if the password was accepted or rejected by way of a return value, I don't actually know however I can think of two 'possible' alternate strategies that might net the same result?

Not 100% sure as I haven't done it before, however I wonder if Pinging the supplicant would work?

http://developer.android.com/reference/android/net/wifi/WifiManager.html#pingSupplicant()

Either that or checking if you have an IP address now?

http://developer.android.com/reference/android/net/wifi/WifiInfo.html

Upvotes: 0

Related Questions