tyc*_*czj 11 android bluetooth-lowenergy android-bluetooth
我有一个支持蓝牙LE的蓝牙条码扫描器,我试图在扫描时从中获取条形码信息.
我可以连接到它很好onServicesDiscovered
在我的调用,BluetoothGattCallback
但我不知道该怎么做.
通过经典的蓝牙连接,您可以InputStream
从a 获得BluetoothSocket
,您只需等待read()
给您提供数据,但我不确定它如何与蓝牙LE配合使用.我试图通过循环中BluetoothGattCharacteristic
的检查属性,如果它的读取性能我打电话gatt.readCharacteristic(characteristic);
,但只是给我无用的信息之前,我尝试扫描的东西,甚至.
那么如何从扫描仪获取条形码数据?
这是我的扫描仪https://www.zebra.com/us/en/support-downloads/scanners/ultra-rugged-scanners/li3608-li3678.html
Ger*_*ssy 11
BLE设备提供的数据称为特征.这些数据包是专门形成的,紧密压缩的字节数组,用于编码特定服务的特定值.您可以在官方蓝牙网站上查看服务.在这里,您将找到定义的(权威的)GATT服务和所属特征.
例如,您有一台BLE单车计算机,可以报告速度和节奏.您在列表中查找Cycling Speed和Cadence项目.此条目包含服务的UUID(0x1816)以及包含特征的数据表的链接.现在,如果您转到服务特征表,您将找到几个条目.您需要速度和节奏,因此您将打开CSC测量(条目的类型字段),将您带到特性的数据表.在这里,您将看到" 值字段"表,该表定义了可以从特征中读取的特定值.
这是蓝牙LE的一般部分,现在回到Android.请注意,您必须查找这些字段才能从特征中获取值.我只是假设你已经拥有了想从中获取数据的特性.这是一个快速的样本,用于检索车轮和曲柄转数(如果有的话).
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;
}
Run Code Online (Sandbox Code Playgroud)
flags
需要检查该字段的特定位,因为该设备可能不报告每种类型的数据,例如它不计算车轮转数.所选特征的工作表始终包含有关此字段的相关信息(如果存在).
值得注意的是,文档说明了这一点
CSC测量特性(CSC指的是Cycling Speed和Cadence)是一个包含Flags字段的可变长度结构,并且根据Flags字段的内容,可能包含一个或多个附加字段[...]
这就是为什么你不能假设在7个字节(8 + 32 + 16位;分别为1 + 4 + 2个字节)偏移处找到累积曲柄转数值,并且当你沿着字段前进时应该计算偏移量.
这是从BLE设备读取Cycling Speed和Cadence值的示例.您必须在应用程序中查找要支持的每个设备(或更确切地说是服务)的这些可用字段和值.如果设备是特殊设备且无法在此GATT目录中找到,则需要查阅设备手册,SDK或供应商以获取更多信息.
归档时间: |
|
查看次数: |
2541 次 |
最近记录: |