Android - check if the password for Wi-Fi was correct

I plan to create an application that checks if a particular Wi-Fi network uses a shared password. I have a list of the 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 to check if the password I was trying to login with was accepted or rejected?

+4
source share
2 answers

You use the reassociate() reconnect() / reassociate() WiFiManager to check the weather or the connection failed, but the boolean returned by it means something else. The returned value only reports the results of starting this operation. This is due to the fact that linking and connecting to a WiFi network takes time. These methods, however, will immediately return and will not be blocked. The actual task of connecting or communicating with the network is performed asynchronously in the background.

You can track what is happening with the Wi-Fi connection by listening to a specific broadcast broadcast system:

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

Learn more about this HERE - Check Torsten's answer.

+3
source

As for checking whether the password was accepted or rejected using the return value, I really don't know, but can I imagine two โ€œpossibleโ€ alternative strategies that can provide the same result?

Iโ€™m not 100% sure how I didnโ€™t do this before, but I wonder if the supplicant's Pinging works?

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

Or is it or a check if you have an IP address now?

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

0
source

Source: https://habr.com/ru/post/1433170/


All Articles