应用程序从未收到 RSSI_CHANGED_ACTION

发布于 2024-12-24 19:19:41 字数 1520 浏览 1 评论 0原文

我是 Android 编程新手,正在尝试理解 BroadcastReceiver 的概念。为了帮助自己,我只是想编写一个监控 Wifi 信号强度的小应用程序。

现在,根据我的理解,我可以简单地等待接收系统广播的 RSSI_CHANGED_ACTION 。 RSSI 应该经常变化,这意味着我应该经常收到此通知......但是,我从来没有收到过一次。我已经将我的代码淡化到最低限度,因此它只在收到通知时记录一条消息。

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别忘他 2024-12-31 19:19:41

因此,我遇到了与您相同的问题,希望在用户走动时更新 RSSI 值等,但我无法使用 RSSI_CHANGED_ACTION 解决它。

就像您遇到的问题一样,我的回调将无法正确调用。奇怪的是,它只在创建活动时被调用一次,然后就再也没有调用过。

我的解决方法

在 onCreate() 中,注册 SCAN_RESULTS_AVAILABLE_ACTION 的回调。然后调用WifiManager.startScan()。

现在,在回调中执行以下操作:

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

现在您有一个循环,回调在其中启动扫描、接收结果并启动另一次扫描。

它很恶心并且会消耗大量电量,但是,您可以在四处走动时观察 RSSI 值的变化。

完整代码

(请注意,我使用 onResume 和 onPause 来注册和取消注册,因此它只会重复扫描,例如,当活动在屏幕上时浪费电池)

@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

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

夜还是长夜 2024-12-31 19:19:41

当 RSSI 级别发生变化时,会触发 WifiManager.RSSI_CHANGED_ACTION。 IE你失去或赢得了一个wifi吧。这种情况并不经常发生。
我的猜测是它是粘性的,所以它会在注册时触发。

如前所述,我发现解决问题的最佳方法是通过 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION 。

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 .

涙—继续流 2024-12-31 19:19:41

您确定它必须触发吗(意味着您确定信号强度正在变化)吗?您阅读过 BroadcastReciever 文档吗?

注意:如果在 Activity.onResume() 实现中注册接收器,则应在 Activity.onPause() 中取消注册它。 (暂停时您不会收到意图,这将减少不必要的系统开销)。不要在 Activity.onSaveInstanceState() 中取消注册,因为如果用户在历史堆栈中移回,则不会调用此方法。

尝试在 AndroidManifest 中注册您的接收器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文