Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application never receives RSSI_CHANGED_ACTION

I am new to Android programming and am trying to understand the concept of BroadcastReceivers. In order to help myself, I am just trying to write a small application that monitors Wifi signal strength.

Now, from my understanding I can simply wait to receive the RSSI_CHANGED_ACTION broadcasted by the system. The RSSI should change frequently which means I should be receiving this notification frequently...however, never do I receive it once. I have watered my code down to the bare minimum so it just logs a message when the notification is received.

public class RssiActivity extends Activity {

    public BroadcastReceiver rssiReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.d("Rssi", "RSSI changed");
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onResume() {
        super.onResume();
        registerReceiver(rssiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
        Log.d("Rssi", "Registered");
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(rssiReceiver);
        Log.d("Rssi", "Unregistered");
    }
}

I have already seen this post Android: How to monitor WiFi signal strength and it doesn't seem to help me. I have also tried the code sample here http://android-er.blogspot.com/2011/01/check-rssi-by-monitoring-of.html and it never updated the RSSI value either. I'm quite confused as to why this is. Any help you can give me would be greatly appreciated. Thanks!

like image 273
rach5000 Avatar asked Jan 03 '12 19:01

rach5000


1 Answers

So, I had the same problem that you did, wanting to an updated RSSI value as the user walked around, etc, and I could not solve it using RSSI_CHANGED_ACTION.

Like the issue you're having, my callback would not be called correctly. Strangely, it was only called once, when the activity was created, and then never again.

My Workaround

In your onCreate(), register a callback for SCAN_RESULTS_AVAILABLE_ACTION. Then call WifiManager.startScan().

Now, in your callback, do:

WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
int newRssi = wifiMan.getConnectionInfo().getRssi();
wifiMan.startScan();

Now you have a loop, where the callback initiates a scan, receives the results, and initiates another scan.

It's gross and will suck a lot of power, however, you can watch the RSSI values change as you walk around.

Full Code

(note that I use onResume and onPause to register and unregister, so it will only scan repeatedly, e.g. waste battery, when the activity is onscreen)

@Override
public void onResume() {
    super.onResume();
    //Note: Not using RSSI_CHANGED_ACTION because it never calls me back.
    IntentFilter rssiFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    this.registerReceiver(myRssiChangeReceiver, rssiFilter);

    WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
    wifiMan.startScan();
}


    @Override
public void onPause() {
    super.onPause();
    this.unregisterReceiver(myRssiChangeReceiver);

}
/**
 * Broadcast receiver to update 
 */
private BroadcastReceiver myRssiChangeReceiver
        = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
        wifiMan.startScan();
        int newRssi = wifiMan.getConnectionInfo().getRssi();
        Toast.makeText(getActivity(), ""+newRssi, Toast.LENGTH_SHORT).show();

}};

Sorry I'm so late, I just had to find out I had to solve your problem :P

like image 132
Eagle Avatar answered Nov 20 '22 11:11

Eagle