Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Update bluetooth rssi every second

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";

        }

}};
like image 606
Алекса Митић Avatar asked Jul 19 '26 10:07

Алекса Митић


1 Answers

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.

like image 63
Blitz Avatar answered Jul 22 '26 01:07

Blitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!