我有一台支持蓝牙 LE 的蓝牙条码扫描仪,我试图在扫描时从中获取条码信息。
我可以很好地连接到它
onServicesDiscovered
在我的BluetoothGattCallback
中被调用,但我不知道从那里该做什么。
使用经典的蓝牙连接,您会从
InputStream
获得 BluetoothSocket
,您只需等待 read()
为您提供数据,但我不确定它如何与蓝牙 LE 配合使用。我尝试循环遍历 BluetoothGattCharacteristic
检查属性,如果它是一个读取属性,我称之为 gatt.readCharacteristic(characteristic);
但这只会给我无用的信息,甚至在我尝试扫描某些内容之前。
那么如何从扫描仪获取条码数据呢?
这是我拥有的扫描仪https://www.zebra.com/us/en/support-downloads/scanners/ultra-rugged-scanners/li3608-li3678.html
BLE 设备提供的数据称为“特性”。这些数据包是专门形成的、紧密包装的字节数组,为特定的服务编码特定的值。您可以在蓝牙官方网站查看分配的号码。在这里您将找到定义的(权威的)GATT 服务及其所属特征。 例如,您有一个 BLE 自行车电脑,可以报告速度和踏频。您可以在指定的数字规格中查找骑行速度和踏频项目。
GATT 服务(第 3.4 章)包含服务的 UUID (0x1816)。然后,您转到蓝牙规格页面并执行以下操作:
搜索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
CSC 测量特性(CSC 指骑行速度和踏频)是一种包含标志字段的可变长度结构,并且根据标志字段的内容,可能包含一个或多个附加字段 [...]
这就是为什么您不能假设在 7 个字节(
8 + 32 + 16 位;分别为 1 + 4 + 2 个字节)偏移处找到累积曲柄转数值,并且应该在沿字段前进时计算偏移量。
这是从 BLE 设备读取骑行速度和踏频值的示例。您必须为您想要在应用程序中支持的每个设备(或更确切地说是服务)查找这些可用字段和值。如果该设备是特殊设备并且无法在此 GATT 目录中找到,您需要查阅该设备的手册、SDK 或供应商以获取更多信息。