0

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...

Community
  • 1
  • 1
Rouz
  • 1,247
  • 2
  • 15
  • 37

2 Answers2

0

It is getting interim states as well as the final state. You can do something like this:

 public class WiFiConnections extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (info != null && info.isConnectedOrConnecting()) {
                    if (info.isConnected()) {
                        Log.i("WiFi", "connected");
                    }
                }
                else{
                    Log.i("WiFi", "disconnected");
                }
            }
        }
    }

I just tested this, and here are the resulting logs:

04-17 14:55:31.479  13780-13780/com.wifitest.danu.wifitext I/WiFi﹕ disconnected
04-17 14:57:20.489  15740-15740/com.wifitest.danu.wifitext I/WiFi﹕ connected
04-17 14:57:34.769  15740-15740/com.wifitest.danu.wifitext I/WiFi﹕ disconnected
04-17 14:57:51.349  15740-15740/com.wifitest.danu.wifitext I/WiFi﹕ connected
04-17 14:58:38.069  15740-15740/com.wifitest.danu.wifitext I/WiFi﹕ disconnected
04-17 14:58:52.489  15740-15740/com.wifitest.danu.wifitext I/WiFi﹕ connected
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • daniel I have also tried to do this with && operator... is it something with ROM? i am using stock nexus device... – Rouz Apr 17 '15 at 22:06
  • nope, i have c/p solution of yours. when I turn off my wifi i get 1 connected (o.O) and 3 disconnected. And when i turn it back on i get 4x connected... I don't get this really – Rouz Apr 17 '15 at 22:12
  • @Rouz That is very strange. What version of Android are you on? My test was on 4.4.2. – Daniel Nugent Apr 17 '15 at 22:28
  • 5.1 but i have also seen this under 4.4.4 – Rouz Apr 17 '15 at 22:35
0

I had problem with code bellow to, but I tried to add something that helped me.

public class WifiReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    if(info != null) {

        WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        boolean wen = wifi.isWifiEnabled();

        if(info.isConnected()) {
            // Do your work.

            Log.wtf("Wifi", "Connected");

            // e.g. To check the Network Name or other info:
            //WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            //WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            //String ssid = wifiInfo.getSSID();
        }
        else if(!(wen)){
            Log.wtf("Wifi", "No");
        }
    }
}
}

As far as I noticed on Android, Wifi will not disable instantly. It have some sub-states like enabling and disabling that will log as enabled.

This is logging Connected as expected but sometimes it is not logging Disconnected as expected (it's actually logging nothing, at least not spamming Connected/No). I checked on multiple ways Wifi state when turned off but sometimes it just stuck on enabled (state 3/WIFI_STATE_ENABLED when trying to get via getWifiState()).

Keep in mind that I'm using Non-Stock and even Non-official CM11.0 - 4.4.4 and it's bit bugged so controls like disable Wifi is not working always as expected.

I don't know did you managed to make it work in meanwhile but good luck with it

  • this is not working as expected (although it should work according to documentation). I want to know when I am connected, with this solution I get multiple times "Connected" logged. – Rouz Apr 20 '15 at 10:49