How to continuously get RSSI without connecting to the BLE device?
Asked Answered
L

1

8

My application needs to constantly get RSSI value of a bluetooth device to make some distance-rssi approximation, without connecting. However, callback method of the BluetoothAdapter gets the RSSI only once when the device scanned.

// Device scan callback
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
        runOnUiThread(new Runnable() {
            public void run() {
                ...
            }
        });     
    }   
};

As a solution, I created two Runnable objects. One is for starting, other is for stopping scan process. These two objects call each other continuously until I get my desired RSSI value. After that my Handler removes them from message queue.

private Runnable startScan = new Runnable() {       
    @Override
    public void run() {
        bluetoothAdapter.startLeScan(leScanCallback);
        scanHandler.postDelayed(stopScan, SCAN_INTERVAL);
    }               
};

private Runnable stopScan = new Runnable() {        
    @Override
    public void run() {         
        bluetoothAdapter.stopLeScan(leScanCallback);                                            
        scanHandler.postDelayed(startScan, STOP_INTERVAL);          
    }
};

This seems to be working but I want to know if there is more viable option to do this without connecting.

Legere answered 18/11, 2013 at 21:37 Comment(4)
This seems to be the right way for me.. Scanning will stop automatically, so after scanning, just start it again...Entrenchment
This is the proper way. By the way, the scanning behaviour DEPENDS on your android device,for example : Galaxy S4 is firing the scan callback continuously for the same peripheral, while Nexus 7 report only once.Turbot
Forget to mention in some situation even on Nexus 7 the scan callback will fire repeatedly. This only happens for certain peripherals and I haven't find out why they are triggering multiple scan callbacks.Turbot
How to Handle this situation above solution work great with nexus device but it will not discover for samsung galaxy s3.Pasteurizer
T
1

If the ble beacon is advertising and not connectable, onLeScan is called continously. If the ble beacon is connectable, the feature depends on the device.

Tilbury answered 5/2, 2014 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.