Wednesday, January 19, 2011

Check RSSI by monitoring of WifiManager.RSSI_CHANGED_ACTION


In last 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.

In order to monitor RSSI change, we have to monitor WifiManager.RSSI_CHANGED_ACTION. In this exercise, another BroadcastReceiver, myRssiChangeReceiver, will be implemented to monitor the broadcast of WifiManager.RSSI_CHANGED_ACTION, to update RSSI if any change.

Modify AndroidWifiMonitor.java only, the other files are same as last exercise "Monitor Wifi status and information with BroadcastReceiver".
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));

this.registerReceiver(this.myRssiChangeReceiver,
new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));

}

private BroadcastReceiver myRssiChangeReceiver
= new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int newRssi = arg1.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0);
textRssi.setText(String.valueOf(newRssi));
}};

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("---");
}

}
}


Download the files.

Related Article:
- Detect Wifi ON/OFF state



3 comments:

Marco Aurélio said...

It look likes that the BroadcastReceiver for RSSI chage event is never called. At leadt in my tests on my device. Seems that the OS never trigger this method.

But when I open a app that lookup for the RSSI change, like WiFi Analizer than the method is triggered. That what debug shows.

cemile said...

i run and test code in Android4.0 but emulator couldn't connect wireless.Each time output is DISCONNECT.In emulator browser connect the internet.But why your code say disconnect?

saklik said...

this code does not shows anything .. i have test it on my android phone...
is this supports the 2.1 aur higher then this .......
plz do reply sir.... i will be thankfull..