I would like to constantly check whether the phone is connected to a specific wireless network. I thought of a service and the SSID of the network of course, but how?
-
1"Constantly check"? Do you realize how much battery that would kill? [Just check it once](http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html) then [register a receiver to check for changes in connectivity](http://stackoverflow.com/questions/3307237/how-can-i-monitor-the-network-connection-status-in-android). – Cat Feb 12 '13 at 18:48
-
There is WiFi_STATE_CHANGED broadcast. You can use that broadcast to detect changes in wifi network. – VendettaDroid Feb 12 '13 at 18:49
-
@Niek ok you're right I meant every 5 minutes in fact. – incredikid Feb 12 '13 at 18:51
-
Even checking every 5 min will kill most of the battery power.. Why not to check it when you are doing HTTP communication – AAnkit Feb 12 '13 at 18:51
-
ok and what would you offer as an alternative to handle a network change without exhausting the battery? – incredikid Feb 12 '13 at 18:53
-
Simple, use broadcast receiver. look at the answer which explains that. – VendettaDroid Feb 12 '13 at 18:54
1 Answers
You'll want
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
In your receiver tag.
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 intent action 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 intent action 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:
<user-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Whole Source Code: Download
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.temp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.temp.MyWiFiStateListener">
<intent-filter >
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
</application>
MainActivity.java
package com.example.temp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Broadcast Receiver:
package com.example.temp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
public class MyWiFiStateListener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.d("TEMP", action);
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(info.getType() == ConnectivityManager.TYPE_WIFI){
WifiManager myWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
Log.d("TEMP","BSSID :: " + wifiInfo.getBSSID() + "SSID :: " + wifiInfo.getSSID());
}
}
}
}

- 3,131
- 2
- 28
- 41
-
Sounds like a good solution. I'll check this out right now. thank you – incredikid Feb 12 '13 at 18:56
-
-
ok I got confused a little bit. So if I create a BroadcastReceiver and add the intent-filters how would I check for the BSSID? – incredikid Feb 12 '13 at 19:27
-
BSSID is the information that you can get from NetworkInfo object. There is a method on that to get SSID and BSSID both. You have to do that in your java code which is going to receive broadcast of state_change. – VendettaDroid Feb 12 '13 at 19:34
-
In the STATE_CHANGE broadcast you get the extra information about the new state of network in form of networkinfo object. – VendettaDroid Feb 12 '13 at 19:36
-
-
You may want to community wiki this answer before the question gets closed – Moog Feb 12 '13 at 20:33
-
-
-
@VendettaDroid I have a nice question. Why the network change broadcasts 3 times when connected to another network? I get three times a COMPLETED supplicantState which makes this run 3 times. – incredikid Feb 13 '13 at 08:49
-
@incredikid It is because a wireless connection go through many different state like SCANNING, CONNECTING, CONNECTED etc. Have a look at this for more detail http://developer.android.com/reference/android/net/wifi/SupplicantState.html. Also, if you just want to react when the supplicant state is for example connected then put a condition in the code which checks the supplicant state. – VendettaDroid Feb 13 '13 at 20:07