I am currently trying to do a relatively simple task in Android.
I want to detect when a wireless network is fully connected. At this moment I do not care whether there is internet connection, I just want to know the exact moment when my device is considered connected to network.
I will try to describe what I did, and how miserably had I failed.
Bare in mind that I have tried official recommendations as well as some answers here but to no avail.
Activity registers receiver:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(ReceiverClass,intentFilter);
In my receiver I have tried stuff from this answer
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(info != null) {
if(info.isConnected()) {
// Do your work
Log.d("tag", "connected");
}
}
But my app is not working as expected. When I kill Wifi (swipe from top and turn it off) I receive a broadcast (obviously) but both of my If's go trough and I get this logged! After I turn on wifi I get 2 more logs that everything is connected. I have tried a thing or two like this
WifiInfo wi = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
if(wi != null) {
SupplicantState ss = wi.getSupplicantState();
Log.d("I am desperate", ss.equals(SupplicantState.COMPLETED));
}
}
but the same stuff happens. I get true 3 times.
I am not an expert in android, I believe I am missing something quite obvious. Any help for me?
UPDATE
I am more an more considering the possibility that this is an issue in Android system. At the moment I stopped trying to get informations from Intent.
I have tried to use EXTRA_WIFI_STATE but the command:
Log.d("WifiState" getIntExtra(WifiManager.EXTRA_WIFI_STATE, -9000));
Logs me always the default (-9000) for every single broadcast I receive.
If I try to SUPPLICANT_CONNECTION_CHANGE_ACTION broadcast - that is never even caught. I am really puzzled here...