Skip to content Skip to sidebar Skip to footer

Weird Behavior When Trying To Detect When Wifi Network Connected Android

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 interne

Solution 1:

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

publicclassWiFiConnectionsextendsBroadcastReceiver {

        @OverridepublicvoidonReceive(Context context, Intent intent) {
            Stringaction= intent.getAction();

            if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                NetworkInfoinfo= 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

Solution 2:

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

publicclassWifiReceiverextendsBroadcastReceiver {


@OverridepublicvoidonReceive(Context context, Intent intent) {

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

        WifiManagerwifi= (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        booleanwen= 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();
        }
        elseif(!(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

Post a Comment for "Weird Behavior When Trying To Detect When Wifi Network Connected Android"