react-native - polidea ble编码发送包到base64
问题描述
我想通过polidea ble向我的滑板车发送一个命令,但我不知道如何组合包并将其编码为base64,我尝试了不同的方法,但它似乎不起作用。这是我需要如何制作包的文档:
APP Bluetooth
START_PACK, OPCODESEND, LENGTH, D0, CHECKSUM (START_PACK = 0x55)
OPCODESEND
0x02 – Speed Limit
D0 – 1 is 6Km/k, 2 is 12Km/h, 3 is 20Km/h, 4 is 25Km/h, 5 No speed Limit
0x03 - Change Zero Start
D0 0 Zero Start OFF, 1 Zero Start ON
0x05 –Lock Unlock Scooter
D0 0 Unlock, 1 Lock
0x06 – On/Off light from display
D0 0 light OFF, 1 light ON
例如,包装应该如何打开灯?
解决方案
我对您所包含的文档的理解是,要打开灯,在 base64 中创建数据包的代码将是:
import { Buffer } from "buffer";
var start_pack = 0x55;
var opcode = 0x06 // light
var action = 0x01 // On
var length = 0x01 // Length of what? action?
var checksum = start_pack + opcode + action + length
var valueBytes = Buffer.alloc(5);
valueBytes[0] = start_pack;
valueBytes[1] = opcode;
valueBytes[2] = length;
valueBytes[3] = action;
valueBytes[4] = checksum;
var valueBase64 = valueBytes.toString('base64')
console.log("Data to write: " + valueBase64)
这给了我一个输出:
Data to write: VQYBAV0=
推荐阅读
- python - 我在 python 中提取 rarfile 时遇到错误(找不到工作工具)
- wordpress - 管理面板忽略 URL 路径
- html - 如何在 django 模板中制作引导卡始终保持相同的高度
- reactjs - 有没有办法检查/验证每个节点在 react-flow-renderer 中都有连接的边缘?
- flutter - 未使用局部变量“***”的值。Dart SWITCH 语句
- vba - VBA 查找/替换代码 .Execute Replace:=wdReplaceAll 不起作用
- javascript - 如果路由在不同的包中,如何使导航与路由一起使用?
- javascript - VS Code:如何排除 *.min.js (损坏的)工具提示提示?
- php - 联系表格不再有效 - 不推荐使用函数 eregi()
- php - 有没有办法从 redis 哈希中获取单个字段