如何在不连接BLE设备的情况下连续获取RSSI? [英] How to continuously get RSSI without connecting to the BLE device?
问题描述
我的应用程序需要不断获取蓝牙设备的 RSSI 值以进行一些距离 RSSI 近似,无需连接.但是,BluetoothAdapter
的回调方法在设备扫描时只获取一次RSSI.
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() {
...
}
});
}
};
作为解决方案,我创建了两个 Runnable
对象.一个用于启动,另一个用于停止扫描过程.这两个对象不断地相互调用,直到我得到我想要的 RSSI 值.之后,我的 Handler
从消息队列中删除它们.
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.
推荐答案
如果 ble 信标正在广播且不可连接,则继续调用 onLeScan.如果 ble 信标可连接,则该功能取决于设备.
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.
这篇关于如何在不连接BLE设备的情况下连续获取RSSI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!