首页 > 解决方案 > 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

例如,包装应该如何打开灯?

标签: react-nativebluetoothbase64bluetooth-lowenergy

解决方案


我对您所包含的文档的理解是,要打开灯,在 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= 

推荐阅读