如何获得蓝牙低功耗设备的 RSSI 值? [英] how to get the rssi value for a bluetooth low energy device ?

查看:35
本文介绍了如何获得蓝牙低功耗设备的 RSSI 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 编程新手,并试图从 BLE 设备中获取 rssi 值以进行距离测量.我可以扫描并获取设备的名称和 mac 地址,但我尝试过获取 rssi 的代码,但不能得到有用的结果,我也使用了 android 开发者网站上的示例.有人可以给我正确的代码吗?

I am new with android programming and trying to get rssi value form a BLE device for distance measurements.i can scan and get the name and mac address of the device but i've tried codes to get the rssi but can't get useful result,also i use the sample on the android developer site. can someone give me the right code to do so??

推荐答案

对此有两种解决方案.对于 4.0 和 5.0 设备可以采用不同的方法来搜索/扫描 BLE 设备.您没有提到您使用的是哪一种,因此在下面添加了两种解决方案.

Two solution for this.There is different approach one can have for 4.0 and 5.0 devices to search/scan BLE devices. You did not mentioned which one you are using, hence adding both the solution below.

1) 对于 Android 4.4 + 到 5.0,您必须通过 BluetoothAdapter's startLEScan 方法开始 LE 扫描,该方法为您提供以下带有 RSSI 值的回调.见方法签名.

1) for Android 4.4 + till 5.0, you have to start LE scanning via BluetoothAdapter's startLEScan method, which gives you below callback with RSSI value. see signature of method.

onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)

//上面的第二个参数是RSSI值.

// second param above is RSSI value.

2) 对于 android 5.0+ 设备,您必须通过 BluetoothLeScanner 类的 startScan 方法开始扫描,如下所示

2) For android 5.0+ devices you have to start scanning by BluetoothLeScanner class's startScan method, like below

getBluetoothLeScanner().startScan

有回调 onScanResult 来通知新扫描的设备,您可以使用以下代码获取 rssi.

which has callback onScanResult to notify for new scanned device, you can use below code to get rssi.

public void onScanResult(int callbackType, ScanResult result) {
final int rssi = result.getRssi(); //RSSI value

这篇关于如何获得蓝牙低功耗设备的 RSSI 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆