Android: Update bluetooth rssi every second

强颜欢笑 提交于 2019-12-13 14:20:06

问题


I'm trying to display the bluetooth signal strength (rssi) evry second (Timer()) from detected device but i couldn't call onRecive() multiple times because Receiver Lifecycle.

I need a way(idea) to refresh the RSSI, or some other way to measure a signal every second? Is it easier if the device is connected?

App gives a constant value always:

DeviceName 2b:3c:d5:6c:3x:0X 40 db

Part of the app stored in Timer() method:

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            // Add the name and address to an array adapter to show in a ListView

           msg = device.getName() + " " + device.getAddress() + " " +rssi+ " db";

        }

}};

回答1:


You can only get the RSSI value during a device discovery scan. (Bluetooth Discovery vs Connection)

"Is it easier if the device is connected?" Accordingly with android docs: "if you already hold a connection with a device, then performing discovery can significantly reduce the bandwidth available for the connection, so you should not perform discovery while connected."

Do you really need to perform an inquiry every 1 second? Will consume a lot of battery...

Since you need to perform the measure periodically, why not use AlarmManager or CountDownTimer?

In your specific case, I believe you should be using AlarmManager, since it can repeat indefinitely. If you want something to execute every second for 10 seconds, for example, use CountDownTimer.



来源:https://stackoverflow.com/questions/16252242/android-update-bluetooth-rssi-every-second

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