Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
749 views
in Technique[技术] by (71.8m points)

android - How to continuously get RSSI without connecting to the BLE device?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...