Weird behavior when trying to detect when Wifi network connected android

耗尽温柔 提交于 2020-01-25 11:01:07

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/29709489/weird-behavior-when-trying-to-detect-when-wifi-network-connected-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!