Last exercise show how to "Get Wifi IP using WifiManager". This exercise it will be further extended to monitor Wifi Connectivity status, and show more information also; such as SSID, BSSID, Speed, Rssi and MAC address.
In order to monitor the Wifi Connectivity status, we have to implement a BroadcastReceiver to receive "ConnectivityManager.CONNECTIVITY_ACTION" intent.
AndroidWifiMonitor.java
package com.exercise.AndroidWifiMonitor;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidWifiMonitor extends Activity {
TextView textConnected, textIp, textSsid, textBssid, textMac, textSpeed, textRssi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textConnected = (TextView)findViewById(R.id.Connected);
textIp = (TextView)findViewById(R.id.Ip);
textSsid = (TextView)findViewById(R.id.Ssid);
textBssid = (TextView)findViewById(R.id.Bssid);
textMac = (TextView)findViewById(R.id.Mac);
textSpeed = (TextView)findViewById(R.id.Speed);
textRssi = (TextView)findViewById(R.id.Rssi);
DisplayWifiState();
this.registerReceiver(this.myWifiReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver myWifiReceiver
= new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
NetworkInfo networkInfo = (NetworkInfo) arg1.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
DisplayWifiState();
}
}};
private void DisplayWifiState(){
ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
textMac.setText(myWifiInfo.getMacAddress());
if (myNetworkInfo.isConnected()){
int myIp = myWifiInfo.getIpAddress();
textConnected.setText("--- CONNECTED ---");
int intMyIp3 = myIp/0x1000000;
int intMyIp3mod = myIp%0x1000000;
int intMyIp2 = intMyIp3mod/0x10000;
int intMyIp2mod = intMyIp3mod%0x10000;
int intMyIp1 = intMyIp2mod/0x100;
int intMyIp0 = intMyIp2mod%0x100;
textIp.setText(String.valueOf(intMyIp0)
+ "." + String.valueOf(intMyIp1)
+ "." + String.valueOf(intMyIp2)
+ "." + String.valueOf(intMyIp3)
);
textSsid.setText(myWifiInfo.getSSID());
textBssid.setText(myWifiInfo.getBSSID());
textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " " + WifiInfo.LINK_SPEED_UNITS);
textRssi.setText(String.valueOf(myWifiInfo.getRssi()));
}
else{
textConnected.setText("--- DIS-CONNECTED! ---");
textIp.setText("---");
textSsid.setText("---");
textBssid.setText("---");
textSpeed.setText("---");
textRssi.setText("---");
}
}
}
Modify AndroidManifest.xml to add permission of ACCESS_WIFI_STATE and ACCESS_NETWORK_STATE.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidWifiMonitor"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidWifiMonitor"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/Connected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Wifi IP:"
/>
<TextView
android:id="@+id/Ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SSID:"
/>
<TextView
android:id="@+id/Ssid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BSSID:"
/>
<TextView
android:id="@+id/Bssid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Speed:"
/>
<TextView
android:id="@+id/Speed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Rssi:"
/>
<TextView
android:id="@+id/Rssi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MAC:"
/>
<TextView
android:id="@+id/Mac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Download the files.
This exercise, the app "Monitor Wifi status and information with BroadcastReceiver" by monitoring of ConnectivityManager.CONNECTIVITY_ACTION, it will be broadcasted when change in network connectivity has occurred; connection has either been established or lost.
In case of RSSI change will not trigger the event. To detect RSSI change, refer to another article "Check RSSI by monitoring of WifiManager.RSSI_CHANGED_ACTION".