您如何从Bluetooth LE设备获取数据 [英] How do you get data from a Bluetooth LE device

查看:609
本文介绍了您如何从Bluetooth LE设备获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台支持蓝牙LE的蓝牙条形码扫描仪,我正在尝试从中获取条形码信息.

I have a bluetooth barcode scanner that supports bluetooth LE and I am trying to get the barcode information from it when one is scanned.

我可以连接到它,而BluetoothGattCallback可以在我的BluetoothGattCallback中被调用,但是我不确定该怎么做.

I can connect to it fine onServicesDiscovered gets called in my BluetoothGattCallback but I am not sure what to do from there.

使用经典的蓝牙连接,您会从BluetoothSocket中获得一个InputStream,而您只是等待read()提供数据,但是我不确定它如何与Bluetooth LE配合使用.我尝试遍历BluetoothGattCharacteristic的检查属性,如果它是re​​ad属性,我会调用gatt.readCharacteristic(characteristic);,但这只会给我无用的信息,甚至是在尝试扫描某些内容之前.

With a classic bluetooth connection you would get a InputStream from a BluetoothSocket and you would just wait for the read() to give you data but I am not sure how it works with Bluetooth LE. I tried looping through the BluetoothGattCharacteristic's checking the property and if its a read property I call gatt.readCharacteristic(characteristic); but that just gives me useless information and that's even before I attempt to scan something.

那我该如何从扫描仪中获取条形码数据?

So how do I get the barcode data from the scanner?

这是我拥有的扫描仪推荐答案

BLE设备提供的数据称为特征.这些数据包是特殊形成的紧密包装的字节数组,它们为特定的 Services 的特定值编码.您可以在蓝牙官方网站上查看服务.在这里,您将找到已定义的(权威的)GATT服务及其所属特征.

The data provided by BLE devices is called Characteristics. These data packages are specially formed, tightly packed byte arrays that encode specific values for specific Services. You can check out the Services at the official Bluetooth website. Here you'll find the defined (authoritative) GATT services and the belonging characteristics.

例如,您有一台BLE单车计算机,它报告速度和节奏.您查找自行车速度和踏频列表中的项目.此项包含服务的UUID(0x1816)和指向包含特性的数据表的链接.现在,如果进入服务特征表,您将找到几个条目.您需要速度和节奏,因此您将打开 CSC量测(条目的 Type 字段),可带您进入特性数据表.在这里,您会看到值字段表,该表定义了可以从特征读取的特定值.

For example, you have a BLE cycling computer that reports speed and cadence. You look up the Cycling Speed and Cadence item in the list. This entry contains the UUID (0x1816) of the service and a link to the data sheet that contains the characteristics. Now if you go to the Service Characteristics table, you'll find a couple entries. You want the speed and cadence, so you'll open CSC Measurement (the Type field of the entry) that takes you to the characteristic's data sheet. Here you'll see the Value Fields table that defines the specific values that can be read from the characteristic.

一般来说,这是Bluetooth LE的一部分,现在又回到了Android.注意,您必须查找这些字段才能从特征中获取值.我只是假设您已经具有要从中获取数据的特征.这是一个快速示例,它检索了车轮和曲柄的转数(如果有).

This was the Bluetooth LE part in general, now back to Android. Note, that you'll have to look up these fields in order to get the values from the characteristics. I'm just gonna assume that you already have the characteristic that you want to get the data from. Here's a quick sample that retrieves the wheel and crank revolutions (if available).

BluetoothGattCharacteristic characteristic = ... ;

int offset = 0; // we define the offset that is to be used when reading the next field

// FORMAT_* values are constants in BluetoothGattCharacteristic
// these represent the values you can find in the "Value Fields" table in the "Format" column
int flags = characteristic.getIntValue(FORMAT_UINT8, offset);

offset += 1; // UINT8 = 8 bits = 1 byte

// we have to check the flags' 0th bit to see if C1 field exists 
if ((flags & 1) != 0) {
    int cumulativeWheelRevolutions = characteristic.getIntValue(FORMAT_UINT32, offset);
    offset += 4; // UINT32 = 32 bits = 4 bytes

    int lastWheelEventTime = characteristic.getIntValue(FORMAT_UINT16, offset);
    offset += 2; // UINT16 = 16 bits = 2 bytes
}

// we have to check the flags' 1st bit to see if C2 field exists 
if ((flags & 2) != 0) {
    int cumulativeCrankRevolutions = characteristic.getIntValue(FORMAT_UINT16, offset);
    offset += 2;

    int lastCrankEventTime = characteristic.getIntValue(FORMAT_UINT16, offset);
    offset += 2;
}

需要检查flags字段的特定位,因为该设备可能不会报告每种类型的数据,例如它不算车轮转数.选定的特征表始终包含有关此字段的相关信息(如果存在).

The flags field needs to be checked for specific bits because it is possible that the device does not report every type of data, e.g. it doesn't count the wheel revolutions. The selected characteristic's sheet always contains the relevant information about this field (if exists).

还值得注意的是文档说明了

It's also worth noting that the documentation says that

CSC测量特征(CSC是指骑行速度和踏频")是一种可变长度的结构,其中包含标志"字段,并且根据标志"字段的内容,可能包含一个或多个其他字段[...]

这就是为什么您不能假定要在7个字节(分别为 8 + 32 + 16位;分别为1 + 4 + 2个字节)处找到累积曲柄转数的值,并且应该使用该偏移量的原因在您沿着田野前进时被计入.

This is why you cannot assume that cumulative crank revolutions value is to be found at the 7 bytes (8 + 32 + 16 bits; 1 + 4 + 2 bytes respectively) offset and the offset should be counted as you progress along the fields.

这是一个从BLE设备读取骑行速度和踏频值的示例.您必须为要在应用程序中支持的每个设备(或更确切地说是服务)查找这些可用字段和值.如果该设备是特殊设备,但在此GATT目录中找不到该设备,则需要查阅该设备的手册,SDK或供应商以获取更多信息.

This was an example for reading Cycling Speed and Cadence values from a BLE device. You'll have to look up these available fields and values for every device (or rather the service) you want to support in your application. If the device is a special one and it cannot be found in this GATT directory, you'll need to consult the device's manual, SDK, or vendor for more info.

这篇关于您如何从Bluetooth LE设备获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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