Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Wifimanager handle wifi-connection states

I've got an app which connect itself programatically to a wifi connection. My problem is, I want to handle the case, that the password is wrong. I want to detect that the password is not correct in runtime. To be precise I've got a progressdialog running while the connection is established, so if the password is wrong the progressdialog is just shown all the time and can't be skipped. A further note: I handled a password which is less than 8 characters by using this code:

if(!m_wifiManager.enableNetwork(netId, true)) {
            progressDialogConnecting.dismiss();
            createInfoMessageDialog(CONST.WIFI_CON_FAILED_TITLE, CONST.WIFI_CON_FAILED_MSG_CONFAILURE);
            m_wifiManager.reconnect();
            return;
        }

If the key for the wifi connection is less than 8 characters, this if-case gets triggered. But if it is longer than 8 characters and wrong I get an endless state of showing the progress dialog. What I exactly want to ask: how do I handle 1. wrong password 2. connection states (just like Android system showing me the toasts "Connected to Wifi xyz") ? AND is it even possible to handel the first one (wrong password)?

Here is the code, that did not work for handling connection established event (this is just the wifirecevier, I also registered it in the activity):

 public class WifiReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
                            if(wrongNetworkConnected)
                            progressDialogConnecting.dismiss();
                        }
                    }
                } else {

                }
            }
        }
    }

Edit: What I am currently doing, is that I have a Handler which tells me to whom I am connected. That's useful because I can say that after the reconnect() I am reconnected to the old network (current network) and not the new one - so apparently the password could be wrong (or something else), because I could not connect to the new network.

The problem about this method is that first of all it takes too much time and secondly it is not reliable. I can lie and say that if you will get reconnected to your current network it is the fault of a wrong password, but actually it is not 100% sure that you cannot reconnect because of this - it may also have other reasons. So I am still searching for a simple feedback/handle from the suplicant that the password is wrong, just like the android api does in the wifi settings of each android device...

like image 265
David Avatar asked Sep 03 '25 02:09

David


1 Answers

My problem is, I want to handle the case, that the password is wrong.

After some research I found this post which is not marked as answered but it still worked for me very well.

Here is the if-case in which the program jumps (already tested several times by me) if there is an authentication error --> e.g. wrong password:

int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);

if(supl_error==WifiManager.ERROR_AUTHENTICATING){
          // DO SOMETHING 
}

NOTE: As seen in the linked post above this if-case should appear in a BroadcastReceiver adding the intent WifiManager.SUPPLICANT_STATE_CHANGED_ACTIONto the receiver-registration in your activity-class.

like image 76
David Avatar answered Sep 07 '25 06:09

David