10

Is it normal to only have a broadcast intent with action NETWORK_STATE_CHANGED_ACTION (whose constant value is android.net.wifi.STATE_CHANGE) when a Wifi connection is coming back up? I.e. I don't get this intent when Wifi is being disconnected.

UPDATE: I am mostly interested to >= 2.2 Froyo

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • I had the same problems for weeks and I think it's normal (or a bug ;)). I know this doesn't help, but just for information... – RoflcoptrException Mar 21 '11 at 23:46
  • 2
    Just to clarify for future readers: that intent is for the state (disabled, enabling, enabled, disabling) of the wifi transceiver, basically telling you if wifi is on or off. You were looking for the state of connectivity, which is different. – JeffE Nov 04 '11 at 21:42
  • 1
    @JeffE: nope - __"android.net.wifi.STATE_CHANGE"__ corresponds to `NETWORK_STATE_CHANGED_ACTION` which is for net connectivity. `WIFI_STATE_CHANGED_ACTION` is for enable, disable etc - corresponds to __"android.net.wifi.WIFI_STATE_CHANGED"__ Please delete your confusing comment. To the OP - I think the answer by M Granja is the correct one – Mr_and_Mrs_D Nov 13 '13 at 23:22

2 Answers2

24

public static final String SUPPLICANT_CONNECTION_CHANGE_ACTION

Since: API Level 1

Broadcast intent action indicating that a connection to the supplicant has been established (and it is now possible to perform Wi-Fi operations) or the connection to the supplicant has been lost. One extra provides the connection state as a boolean, where true means CONNECTED.

See Also

EXTRA_SUPPLICANT_CONNECTED

Constant Value: "android.net.wifi.supplicant.CONNECTION_CHANGE"

In android's API it says that it's not a good idea to check STATE_CHANGE for network connectivity and instead you should use SUPPLICANT_CONNECTION_CHANGE_ACTION. this will notice an establishment to a wifi network, and the disconnection of a wifi network. I don't know if this might help you, but I do hope so. LINK

Community
  • 1
  • 1
  • I get a connection change event on "disconnect" now! Thanks! – jldupont Mar 22 '11 at 00:27
  • 1
    I don't know but SUPPLICANT_CONNECTION_CHANGE_ACTION didn't work for me. It was never getting fired. I had to use WifiManager.NETWORK_STATE_CHANGED_ACTION to do the same thing. And it worked like a charm!! – unrealsoul007 Mar 25 '15 at 10:36
21

I had a similar need in my project and ended up having to use both.

The android.net.wifi.supplicant.CONNECTION_CHANGE action sends a broadcast when the network is connected, but usually before the device has an IP address, so I needed the android.net.wifi.STATE_CHANGE action for that.

The android.net.wifi.STATE_CHANGE action receives a broadcast on disconnect only if the device is disconnecting from a network, but wifi is still enabled (when hotspot goes out of range, for example)

So you should put both actions for the receiver in the manifest:

<receiver android:name="net.moronigranja.tproxy.WifiReceiver">
            <intent-filter>
                    <action android:name="android.net.wifi.STATE_CHANGE"/>
                    <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
            </intent-filter>
</receiver>

and you put an if to check which action is being called in the intent. Here is the onReceive method of the BroadcastReceiver in my code:

public void onReceive(Context c, Intent intent) {
      if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){ 
          boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
          if(!connected) {
               //Start service for disconnected state here
          }
      }

      else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
          NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
          if( netInfo.isConnected() )
          {
              //Start service for connected state here.
          }   
      }
  }
M Granja
  • 845
  • 8
  • 26
  • even though i used a separate class for my broadcast receiver, checking the supplicant was the key to solving my problem. – tony gil Jul 20 '12 at 17:58
  • 2
    I don't know but SUPPLICANT_CONNECTION_CHANGE_ACTION didn't work for me. It was never getting fired. I had to use WifiManager.NETWORK_STATE_CHANGED_ACTION to do the same thing. And it worked like a charm!! – unrealsoul007 Mar 25 '15 at 10:38
  • Yes, NETWORK_STATE_CHANGED_ACTION worked like charm, no need to add on the manifest. – ssi-anik Sep 12 '15 at 00:29
  • 1
    @unrealsoul007 really really late answer, but in current state of affairs (Lollipop+), I noticed I stopped getting SUPPLICANT_CONNECTION_CHANGE_ACTION broadcasts whenever the connect/authenticate/acquire IP/disconnect flow was triggered by the system and not my app. So I'm guessing there is some process isolation preventing retrieval of BCs, either on the Android system or even low level Linux layers. Pretty much I'm assessing user apps can't ubiquitously establish a difference between connected, with-ip, without-ip, auth failed among other types of state of the supplicant – leRobot Apr 20 '16 at 11:28
  • I noticed that too. You still get the broadcast if mobile data is disabled, though, when wireless disconnects. – M Granja Apr 26 '16 at 17:06