应用程序永远不会收到RSSI_CHANGED_ACTION [英] Application never receives RSSI_CHANGED_ACTION
问题描述
我是Android编程的新手,正在尝试了解BroadcastReceivers的概念.为了帮助自己,我正在尝试编写一个监视Wifi信号强度的小型应用程序.
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.
现在,据我了解,我可以等待接收系统广播的RSSI_CHANGED_ACTION.RSSI应该经常更改,这意味着我应该经常收到此通知...但是,我永远不会收到一次.我已经将代码减少到最低限度,因此它仅在收到通知时记录一条消息.
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");
}
}
我已经看过这篇文章 Android:如何监控WiFi信号强度,但似乎对我没有帮助.我还在 http:上尝试过代码示例://android-er.blogspot.com/2011/01/check-rssi-by-monitoring-of.html ,并且它也从未更新过RSSI值.我很困惑为什么会这样.您能给我的任何帮助将不胜感激.谢谢!
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!
推荐答案
因此,我遇到了与您相同的问题,因为当用户走动时想要一个更新的RSSI值,等等,而我无法使用RSSI_CHANGED_ACTION来解决它
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.
在您的onCreate()中,为SCAN_RESULTS_AVAILABLE_ACTION注册一个回调.然后调用WifiManager.startScan().
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.
这很重要,并且会消耗很多功率,但是,您可以在走动时观察RSSI值的变化.
It's gross and will suck a lot of power, however, you can watch the RSSI values change as you walk around.
(请注意,我使用onResume和onPause进行注册和注销,因此只会在屏幕上显示活动时重复扫描,例如,废电池)
(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();
}};
对不起,我来晚了,我只好发现我必须解决您的问题:P
Sorry I'm so late, I just had to find out I had to solve your problem :P
这篇关于应用程序永远不会收到RSSI_CHANGED_ACTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!