1

I want to test wifi connection through android app to check whether entered wifi ssid and password is correct or not. How i can check whether the given ssid and password is correct?

pooja
  • 159
  • 1
  • 13

1 Answers1

1

Here's what we do in our app:

  1. We ensure that the wifi is enabled
  2. We create a WifiConfiguration with the ssid and pw (note that WifiConfiguration.SSID and WifiConfiguration.preSharedKey are surrounded in quotes so that if the ssid is example and the pw is password then WifiConfiguration.SSID = "\"example\"" and WifiConfiguration.preSharedKey = "\"password\""
  3. We add the WifiConfiguration to the WifiManager (WifiManager.addNetwork(WifiConfiguration))
  4. We check the return value (which is a networkId) if it is -1 then the operation was not successful
  5. We tell the WifiManager fo enable the networkId from the previous step and attempt to connect (WifiManager.enableNetwork(netId, true))
  6. We check if the negotiation with this WifiConfiguration was successful. This step has some ugly code, and I would love to get some other ideas, but it works:

    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;
    }
    
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
  • How we can check the password for given ssid is correct or incorrect? – pooja Aug 16 '18 at 11:38
  • We assume that if the wifi is on and the network is added and enabled then if the negotiation is not successful it must be a bad pw – MidasLefko Aug 16 '18 at 11:45
  • When we get the netId as -1?. And because of netId = -1 the the statement wifiManager.enableNetwork(netId, true); is not evaluating. – pooja Aug 16 '18 at 12:19
  • statements after wifiManager.enableNetwork(netId, true); in checkWifiNegotiation() function are not execute. – pooja Aug 16 '18 at 12:33
  • Thanks for your response. – pooja Aug 16 '18 at 12:53
  • If you get -1 then you were not able to add the wificonfiguration. There are many reasons why this might be happening. Check [here](https://stackoverflow.com/questions/12016918/android-wifimanager-addnetwork-returns-1) – MidasLefko Aug 16 '18 at 17:01
  • Just curious, did you figure this out? – MidasLefko Aug 19 '18 at 07:12
  • Thanks for your response above code is now working for me. – pooja Aug 20 '18 at 07:06
  • But we need to reset the network setting to return successful result, without reset it reurns false value for correct credentials why there is need of reset network settings. – pooja Aug 24 '18 at 06:42