ala*_*laa 7 android bluetooth rssi bluetooth-lowenergy
我是android编程的新手,并尝试从远程测量的BLE设备获取rssi值.我可以扫描并获取设备的名称和mac地址但我已经尝试了代码来获取rssi但是无法获得有用的结果,我也使用android开发者网站上的示例.有人可以给我正确的代码吗?
AAn*_*kit 16
两种解决方案.对于4.0和5.0设备,可以使用不同的方法来搜索/扫描BLE设备.你没有提到你正在使用哪一个,因此在下面添加了两个解决方案.
1)对于Android 4.4 + 5.0,您必须通过BluetoothAdapter's startLEScan方法启动LE扫描,这将为您提供RSSI值以下的回调.看到方法的签名.
onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
Run Code Online (Sandbox Code Playgroud)
//上面的第二个参数是RSSI值.
2)对于Android 5.0+设备,你必须通过BluetoothLeScanner类的startScan方法开始扫描,如下所示
getBluetoothLeScanner().startScan
Run Code Online (Sandbox Code Playgroud)
其中有回调onScanResult来通知新的扫描设备,你可以使用下面的代码来获取rssi.
public void onScanResult(int callbackType, ScanResult result) {
final int rssi = result.getRssi(); //RSSI value
Run Code Online (Sandbox Code Playgroud)