Tuesday, January 18, 2011

Monitor Wifi status and information with BroadcastReceiver

Monitor Wifi status and information with BroadcastReceiver

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.

Monitor Wifi status and information with BroadcastReceiver

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".



5 comments:

cubechubby said...

Thanks... Can you show how do it with get the connection speed via mobile instead of wifi?

Anil said...

Sir, I am developing an application in which I want to know the IP address of WiFi networks.How can I know the IP address of WiFi network after scanning the available networks?Is it possible to get IP of networks?Thank you in advance...

Nayanesh Gupte said...

your code couldn't give me proper IP Address

so i got this

int myIp = wifiInfo.getIpAddress();

String IPAddress = intToIP(myIp);

public String intToIP(int i) {
return ((i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + ((i >> 24) & 0xFF));
}

Arun said...

i have developd an app which will geive the details of the network to which it is connected.But when the station is changing automatically from one AP to other AP with same SSID(roaming scenario) then how to detect the BSSID of the changed network automatically.Any API or Broadcast receiver info will be helpful.

Adam Geller said...

Check via NetworkInfo for wifi-type if it is connected.
And then use wifiinfo getSSid(). You might want to remove double slashes from returnd SSID

https://play.google.com/store/apps/details?id=com.connect.freewifi

You should check out this application and developer api from
http://developer.android.com/reference/android/net/wifi/WifiInfo.html

It will help you with your task.