Application never gets RSSI_CHANGED_ACTION

I am new to Android programming and trying to understand the concept of BroadcastReceivers. To help myself, I'm just trying to write a small application that tracks Wi-Fi signal strength.

Now, from my understanding, I can just wait to get the RSSI_CHANGED_ACTION broadcast by the system. RSSI has to change often, which means I have to get this notification often ... however, I never get it once. I put my code to a minimum, so it just logs a message when it receives a notification.

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 already saw this Android post : how to control the Wi-Fi signal strength , and this does not seem to help me. I also tried the sample code here http://android-er.blogspot.com/2011/01/check-rssi-by-monitoring-of.html and it never updated the RSSI value. I am very confused why this is so. Any help you can give me would be greatly appreciated. Thanks!

+6
source share
3 answers

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

Like the problem you are facing, my callback will not be called correctly. Oddly enough, it was called only once when an 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(); 

You now have a loop in which the callback initiates a scan, receives the results, and initiates another scan.

It's rude and sucks a lot of energy, however you can watch the RSSI change as you walk.

Full code

(note that I use onResume and onPause to register and unregister, so it will repeatedly check, for example, a used battery, when activity is displayed on the screen)

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

It’s a pity that I am so late, I just needed to find out that I needed to solve your problem: P

+7
source

Do you want it to work (this means that you change the signal strength)? Have you read the BroadcastReciever Dokumentation?

Note. If you register the receiver in your implementation of Activity.onResume (), you should cancel it in Activity.onPause (). (You will not receive intentions upon suspension, and this will reduce unnecessary system costs). Do not unregister in Activity.onSaveInstanceState (), because it will not be called if the user returns to the history stack.

Try registering your receiver inside your AndroidManifest.

0
source

WifiManager.RSSI_CHANGED_ACTION starts when RSSI levels change. I.E. you lose or win a wifi bar. Often this does not happen. I assume it is sticky, so it starts when registering.

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

0
source

Source: https://habr.com/ru/post/905013/


All Articles