繁体   English   中英

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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM