Application never receives RSSI_CHANGED_ACTION
Asked Answered
K

3

6

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!

Kinaesthesia answered 3/1, 2012 at 19:43 Comment(2)
Upon first creating your app (in onCreate) you aren't registering the receiver. I can't remember if onResume is always called (even upon first start).Immunology
Hey Jack, thanks for your response. As it turns out, onResume() is called upon first start. I have tried it your way just in case, and this didn't seem to help.Kinaesthesia
C
8

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

Celia answered 11/7, 2012 at 19:1 Comment(2)
Thanks, Eagle. The requirements of my project actually changed, so I ended up abandoning the RSSI monitoring altogether. This is good to have for future reference, though. It's unfortunate that there doesn't seem to be a more efficient way of handling this.Kinaesthesia
StartScan() is now deprecated.Gradate
C
0

WifiManager.RSSI_CHANGED_ACTION is triggered when the RSSI levels change. I.E. you lose or win a wifi bar. It does not happen that often. My guess is it's sticky so it triggers when registered.

As said, the best way I found to solve the problem is through WifiManager.SCAN_RESULTS_AVAILABLE_ACTION .

Camfort answered 12/2, 2015 at 20:20 Comment(0)
H
-1

are you shure that it has to trigger (meaning are you shure the signal strength is changing)? have you read the BroadcastReciever Dokumentation?

Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.

Try to register your reciever inside of your AndroidManifest.

Hardihood answered 3/1, 2012 at 22:6 Comment(1)
Hi Rafael, thanks for responding. I am sure that my code is not working properly because I don't even receive the intent when I enter a Wifi dead spot. Also, the signal is known to change wildly. I have already tried adding the receiver statically in the manifest file with no luck. I have also tried creating a separate class extending BroadcastReceiver and registering the receiver dynamically, also with no luck. The code is straightforward, I must be missing something fundamental here.Kinaesthesia

© 2022 - 2024 — McMap. All rights reserved.