polidea BLE解码本机反应中的特征值

polidea BLE decode characteristic value in react native

我正在尝试解码我​​从 BLE 收到的包,但我没有任何运气。如果您有任何想法并且可以帮助我,我将不胜感激。这是我收到的 base64 示例:

A0IARQ==
AcgAyQ==
Av0BAA==
BGQEbA==

我应该得到速度、电池电压、选项字节和odo

OPCODE APP  Bluetooth
START_PACK, OPCODE, LENGTH, D0, D1 . . DN, CHECKSUM         (START_PACK = 0x55)
OPCODE
0x01 – Speed
0x02 – Battery Voltage
0x03 – Option Byte
[7 6 5 4 3 2 1 0] bit 7 = Zero Start, bit 6 = Km/Mile, bit 5 = Unlock/Lock, bit 4 = Light On/Off
0x04 – ODO

查看 Polidea/react-native-ble-plx 库,读取特征的数据似乎以 base64 格式返回。因此,作为字节数组,您的值将是:

AcgAyQ== is [1, 200, 0, 201]
Av0BAA== is [2, 253, 1, 0]
A0IARQ== is [3, 66, 0, 69]
BGQEbA== is [4, 100, 4, 108]

这看起来第一个字节是操作码,最后一个字节是校验和。我假设中间的字节是小端字节序的值。

我试验了一些代码:

import toUint8Array from 'urlb64touint8array';
import { Buffer } from 'buffer'

var speed_bytes = toUint8Array('AcgAyQ==');
var battery_bytes = toUint8Array('Av0BAA==');
var option_bytes = toUint8Array('A0IARQ==');
var odo_bytes = toUint8Array('BGQEbA==');

var speed = Buffer(speed_bytes).readInt16LE(1) / 10;
var battery = Buffer(battery_bytes).readInt16LE(1) / 10;
var options = (Buffer(option_bytes).readInt16LE(1) >>> 0).toString(2)
var odo = Buffer(odo_bytes).readInt16LE(1) / 10;

console.log('Processing speed: [' + speed_bytes + '] ' + speed);
console.log('Processing battery: [' + battery_bytes + '] ' + battery);
console.log('Processing option: [' + option_bytes + '] ' + options);
console.log('Processing odo: [' + odo_bytes + '] ' + odo);

给出了以下输出:

Processing speed: [1,200,0,201] 20 
Processing battery: [2,253,1,0] 50.9 
Processing option: [3,66,0,69] 1000010 
Processing odo: [4,100,4,108] 112.4