Application never receives RSSI_CHANGED_ACTION

早过忘川 提交于 2019-12-10 13:09:21

问题


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!


回答1:


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




回答2:


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 .




回答3:


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.



来源:https://stackoverflow.com/questions/8717741/application-never-receives-rssi-changed-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!