我正在为我的 BLE 应用程序使用RxAndroidBle库。像 nrfConnect 应用程序一样,我想从 BLE 设备读取 RAW 数据。
如何从设备读取 RAW 数据?
我正在为我的 BLE 应用程序使用RxAndroidBle库。像 nrfConnect 应用程序一样,我想从 BLE 设备读取 RAW 数据。
如何从设备读取 RAW 数据?
您看到的原始数据是您的蓝牙设备公布的十六进制值。
您的应用可以使用android.bluetooth.le.BluetoothLeScanner
's 方法读取这些数据:
public void startScan(List<ScanFilter> filters, ScanSettings settings,
final ScanCallback callback);
这是一个ScanCallback
实现示例代码,您可以将其作为参数传递以读取广告数据:
ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
byte[] scanRecord = result.getScanRecord().getBytes();
int rssi = result.getRssi();
// Search through raw data for the type identifier 0xFF, decode
// the following bytes over the encoded packet length ...
// yourCallback.onScanResult(device, scanRecord, rssi);
}
};
在您的情况下,原始数据仅包含0xFF类型,它是制造商特定数据类型,长度为 30 个字节。您的回调处理应在原始数据中搜索类型标识符 0xFF,并在编码的数据包长度上解码以下字节。
广告数据中包含的数据类型因设备制造商而异,但至少应包括制造商特定数据,该数据以公司标识符的两个字节开头。
还有其他类型的 BLE 广告数据,例如:
此页面列出了各种类型的 BLE 广告数据:
https://www.novelbits.io/bluetooth-low-energy-advertisements-part-1/
要访问广告数据的原始字节,RxAndroidBle
您需要:
ScanResult
( RxBleClient#scanBleDevices(ScanSettings, ScanFilter...)
)ScanRecord
( ScanResult#getScanRecord
)byte[]
( ScanRecord#getBytes
)