Home » Android » How to check if password for WiFi network is correct in Android ?

How to check if password for WiFi network is correct in Android ?

The following sample code in android can be used to check if the Password entered to connect to wifi is incorrect. If password is correct, it should connect to WiFi successfully.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

public class WifiCheckWrongPasswordActivity extends AppCompatActivity {

    private WifiManager wifiManager;
    private ConnectivityManager connectivityManager;
    private BroadcastReceiver wifiStateReceiver;

    protected void onCreate(Bundle savedInstanceState) {

        // Initialize WiFi and connectivity manager
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        // Register a BroadcastReceiver to listen for WiFi state changes
        wifiStateReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
                    NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                    if (networkInfo != null && networkInfo.isConnected()) {
                        // Successfully connected to a WiFi network
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                        String ssid = wifiInfo.getSSID();
                        Toast.makeText(context, "Connected to WiFi: " + ssid, Toast.LENGTH_SHORT).show();
                    } else if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.DISCONNECTED) {
                        // WiFi connection failed (potentially due to incorrect password)
                        Toast.makeText(context, "Failed to connect to WiFi", Toast.LENGTH_SHORT).show();

        // Register the BroadcastReceiver
        IntentFilter intentFilter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(wifiStateReceiver, intentFilter);

        // Connect to a WiFi network with potentially wrong password
        connectToWifi("YourSSID", "WrongPassword");

    private void connectToWifi(String ssid, String password) {
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = "\"" + ssid + "\"";
        wifiConfig.preSharedKey = "\"" + password + "\"";

        int networkId = wifiManager.addNetwork(wifiConfig);

        if (networkId != -1) {
            // Enable the network
            wifiManager.enableNetwork(networkId, true);
        } else {
            // Failed to add network (incorrect password)
            Log.e("WifiCheck", "Failed to add network (incorrect password)");

    protected void onDestroy() {
        // Unregister the BroadcastReceiver

In this code:

  1. We create a WifiCheckWrongPasswordActivity that initializes the WifiManager and ConnectivityManager.
  2. We register a BroadcastReceiver (wifiStateReceiver) to listen for WiFi state changes using WifiManager.NETWORK_STATE_CHANGED_ACTION.
  3. In the onReceive method of the wifiStateReceiver, we check if the network state has changed and whether it’s connected. If the network is not connected, it could indicate a failed connection attempt, potentially due to an incorrect password.
  4. In the connectToWifi method, we attempt to connect to a WiFi network with a potentially incorrect password. We add the network configuration with the SSID and password to wifiManager.
  5. If the addNetwork method returns a network ID that is not -1, it means the network was added successfully. If it returns -1, it suggests that the network configuration might be incorrect (e.g., wrong password).
  6. We unregister the wifiStateReceiver in the onDestroy method to avoid memory leaks.

Please replace "YourSSID" and "WrongPassword" with the actual SSID and the password you want to test. This code will help you detect when the connection attempt fails, which may indicate an incorrect password.

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment