Android wifi change intent

WIFI_STATE_CHANGED_ACTION intent not received upon connection to WiFi access point?

Part of my app’s functionality is to scan and display a list of WiFi access points, and then connect to the one chosen by the user. I have this functionality working. Now, I also wish to be notified when the connection «goes through». This should be fairly simple, but I find myself stumbling. I have read through various posts here at SO, and they all mention registering for WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION or WifiManager.WIFI_STATE_CHANGED_ACTION . However, neither of these works for me. Can anyone spot any mistake in this code? (I am leaving out the parts which do the scan and stuff) Expected behavior: As soon as the connection is successful (i.e, when I see the «connected» icon on the notification bar), the broadcast should be received and I should see the toast. Observed behavior: The broadcast is received when the app first starts, and whenever I switch back to it (i.e, whenever onResume() is called; or I suspect, whenever I register for the intent)

public class WifiScanActivity extends Activity < WifiManager mainWifi; WifiReceiver mWifiReceiver; IntentFilter mIntentFilter; private final static String TAG = "WifiScanActivity"; public static final String INTENT_FOR_WIFI_CONNECTED = WifiManager.WIFI_STATE_CHANGED_ACTION; // public static final String INTENT_FOR_WIFI_CONNECTED = // WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION; public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.main); mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); mWifiReceiver = new WifiReceiver(); mIntentFilter = new IntentFilter(); mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); mIntentFilter.addAction(INTENT_FOR_WIFI_CONNECTED); registerReceiver(mWifiReceiver, mIntentFilter); mainWifi.startScan(); >protected void onPause() < unregisterReceiver(mWifiReceiver); super.onPause(); >protected void onResume() < registerReceiver(mWifiReceiver, mIntentFilter); super.onResume(); >class WifiReceiver extends BroadcastReceiver < public void onReceive(Context c, Intent intent) < Log.d(TAG, "In WifiReceiver: Broadcast Received = " + intent.getAction()); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent .getAction())) < // Display the ListView and connect to the selected AP >else if (INTENT_FOR_WIFI_CONNECTED.equals(intent.getAction())) < if (WifiManager.WIFI_STATE_ENABLED == intent.getIntExtra( WifiManager.EXTRA_WIFI_STATE, 0)) < displayNetworkInfo(); >/*if(true == intent.getBooleanExtra( * WifiManager.EXTRA_SUPPLICANT_CONNECTED, false))< * displayNetworkInfo(); >*/ > > > private void displayNetworkInfo() < WifiInfo wifiInfo = mainWifi.getConnectionInfo(); String ssid = wifiInfo.getSSID(); int ip = wifiInfo.getIpAddress(); String message = "Connection established.\nSSID = " + ssid + "; IP Address mt24 mb12">
    androidandroid-wifi
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this question" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="question" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="1" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this question
)">edited Nov 13, 2013 at 22:41
Mr_and_Mrs_D
32k38 gold badges178 silver badges359 bronze badges
asked Jul 6, 2011 at 11:37
Add a comment|

3 Answers 3

Reset to default
23

Ok, I figured it out. It turns out I was registering for the wrong intent. I should be using WifiManager.NETWORK_STATE_CHANGED_ACTION.

Here are the snippets of relevant portions of code:

mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ; mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); public void onReceive(Context c, Intent intent) < if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) < NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if(NetworkInfo.State.CONNECTED.equals(nwInfo.getState()))>

Источник

android.net.wifi.STATE_CHANGE: not triggered on Wifi disconnect

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

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.

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

2 Answers 2

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

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

Источник

How to get network state change on android? [duplicate]

The only option they give me was using getActiveNetworkInfo or getActiveNetwork.

If I use that, I will get 2 broadcast which will give me same network info.

Is there a better way to handle network state in Android ?

Thanks for quick reply, but I am not looking for a way to check if I have internet connection or not.

I need to know which network has been disconnected and connected. The network state change is what I am looking for.

I get the idea which checking network online or not. But that is not what I am looking for. I need to execute code when there is network change from WIFI to 3G, and if I use that, I will execute the code twice.

@SaravInfern Thanks. That is what I was looking for. If you put ur answer, I will mark it as correct answer

6 Answers 6

Here is a simple code snippet that will help you identify what kind of internet connection a user has on her device.

First we need following permission in order to access network state. Add following permission to your AndroidManifest.xml file.

Permissions required to access network state:

Now check following utility class NetworkUtil. It has method getConnectivityStatus which returns an int constant depending on current network connection. If wifi is enabled, this method will return TYPE_WIFI. Similarly for mobile data network is returns TYPE_MOBILE. You got the idea!!

There is also method getConnectivityStatusString which returns current network state as a more readable string.

Create a class NetworkUtil.java and paste the following code:

import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class NetworkUtil < public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static int getConnectivityStatus(Context context) < ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) < if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; >return TYPE_NOT_CONNECTED; > public static String getConnectivityStatusString(Context context) < int conn = NetworkUtil.getConnectivityStatus(context); String status = null; if (conn == NetworkUtil.TYPE_WIFI) < status = "Wifi enabled"; >else if (conn == NetworkUtil.TYPE_MOBILE) < status = "Mobile data enabled"; >else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) < status = "Not connected to Internet"; >return status; > > 

You can use this utility class in your android app to check the network state of the device at any moment.

Broadcast Receiver to handle changes in Network state

You can easily handle the changes in network state by creating your own Broadcast Receiver. Following is a broadcast receiver class where we handle the changes in network.

Check onReceive() method. This method will be called when state of network changes. Here we are just creating a Toast message and displaying current network state. You can write your custom code in here to handle changes in connection state.

Create a class NetworkChangeReceiver.java and paste the following code:

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class NetworkChangeReceiver extends BroadcastReceiver < @Override public void onReceive(final Context context, final Intent intent) < String status = NetworkUtil.getConnectivityStatusString(context); Toast.makeText(context, status, Toast.LENGTH_LONG).show(); >> 

Once we define our BroadcastReceiver, we need to define the same in AndroidMenifest.xml file. Add following to your manifest file.

We defined our broadcast receiver class in manifest file. Also we defined two intent CONNECTIVITY_CHANGE and WIFI_STATE_CHANGED. Thus this will register our receiver for given intents. Whenever there is change in network state, android will fire these intents and our broadcast receiver will be called.

That's it Run Your Code.Hope this serves your purpose !!

Источник

BroadcastReceiver when wifi or 3g network state changed

I have an app which updates the database whenever the phone is connected to WiFi. I have implemented a Service and BroadcastReceiver which will run the Service (it will tell me what network is in use), but the problem is I don't know what to add in the manifest file to start BroadcastReceiver when the network state changes or when it connects to some kind of network

5 Answers 5

Or if you want more control over it, before registering BroadcastReceiver set these up:

final IntentFilter filters = new IntentFilter(); filters.addAction("android.net.wifi.WIFI_STATE_CHANGED"); filters.addAction("android.net.wifi.STATE_CHANGE"); super.registerReceiver(yourReceiver, filters); 

WIFI_STATE_CHANGED

Broadcast indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.

STATE_CHANGE

Broadcast indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String

Also, you'll need to specify the right permissions inside manifest tag:

To check connectivity, you can use ConnectivityManager as it tells you what type of connection is available.

ConnectivityManager conMngr = (ConnectivityManager)this.getSystemService(this.CONNECTIVITY_SERVICE); android.net.NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); android.net.NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

Источник

Читайте также:  Принтер phaser 3020 подключить вай фай
Оцените статью
Adblock
detector