Search code examples
javaandroidbroadcastreceiverandroid-broadcastreceiverwifimanager

wifi broadcast receiver multiple intents


I have developped an application using Wifi broadcast receiver. My requirement is get function call when Wifi is connected to a router by obtaining the IP address as well as call another function when Wifi is disconnected from previously connected router.

I used this in Manifest

<action android:name="android.net.wifi.STATE_CHANGE" />

for registering broadcast receiver.

and in Broadcast Receiver class in OnReceive :

NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
NetworkInfo.State state = networkInfo.getState(); 

if (state == NetworkInfo.State.CONNECTED) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wifiManager.getConnectionInfo();

    Log.e("++++WiFi Conected","++++WiFi Conected");

    Flags.wifiState = true;
    updateWifiSyncTrue(context);

    if (wi.getIpAddress() != 0) {
        intent = new Intent(context, ConnectionService.class);
        intent.putExtra("intentValue", "signalOn");
        intent.putExtra("bssId", wi.getBSSID());
        context.startService(intent);
    }
}

if (state == NetworkInfo.State.DISCONNECTED) {
    Log.e("++++WiFi DisConected","++++WiFi DisConnected");
    Flags.wifiState = false;
    updateWifiSyncFalse(context);
    updateRouterSyncFalse(context);

    intent = new Intent(context, ConnectionService.class);
    intent.putExtra("intentValue", "signalOff");
    context.startService(intent);
}

Every thing worked till KitKat But when I test this with lollipop version On Receive call many times for.

When I turn my WiFi on its resulting as State : Idle --Connecting-- Authenticating -- Captive Check -- Connected -- Connected . Sometimes Disconnected also.

while turning off. Connected -- Disconnected -- Disconnected -- Disconnected -- Disconnected

The Debug result of NetworkInfo is as below:

[type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/AUTHENTICATING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/CONNECTING, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/OBTAINING_IPADDR, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/CAPTIVE_PORTAL_CHECK, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTING/CAPTIVE_PORTAL_CHECK, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "DJ Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

[type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "DJ     Hostspot", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false].

I kindly request to help me in the same. Using my code when wifi is turned on CONNECTED state is executing multiple times and DISCONNECTED state also.

And when WIFI is turned off Connected state is generated and then 4-5 Disconnected State.

I Used HTC phone for testing.

I used simple coding, Do I have to get more deeper condition checking for this? (Detailed State etc...)

What is the best way to handle this solution ?


Solution

  • When ever you are using intent-filter android.net.wifi.STATE_CHANGE it will trigger the broadcast receiver multiple times. For example if you are switching on wifi then the onreceive of the broadcast will call multiple times anywhere from 1-5 times. So, it will give you multiple NetworkInfo.State.CONNECTED and same with disconnected also.

    So if you want to have only one connected or disconnected it output means simply use a boolean flag and set it properly in a way that first if wifi enabled comes means it should ignore remaining wifi connected intents and similarly wifi disconnected also.