我正在创建一个从蓝牙设备获取数据的 Android 应用程序。 我只有一个问题,我的 ble 设备给我的数据是这样的:
Accelerometer + Gyroscope: "zLAi/wD/SpwiAOv/VwBnAML/xQE="
Pressure: "bgBvAHAAcQByAA="
Battery: "ChYKARoA"
虽然应该有数字,但我如何转换这些值??
例如我有这个功能来管理特性
async setupNotifications(device) {
for (const id in this.AccGyrMg) {
const service = this.serviceUUID(id)
const characteristicAccGyrMg = this.notifyUUIDAccGyrMg(id)
device.monitorCharacteristicForService(service, characteristicAccGyrMg, (error, characteristic) => {
if (error) {
this.error(error.message)
return
}
this.updateValue(characteristic.uuid, characteristic.value)
})
}
这是更新值:
updateValue(key, value) {
// const readebleData = base64.decode(value);
this.setState({ values: { ...this.state.values, [key]: value } })
}
我尝试使用 base64.decode(value) 但我收到的值不是数字:/
最佳答案
显然这些值被十六进制编码为 base64。我找不到任何文档,但它基于这个 Github 问题:monitorCharacteristicForService value is fragmented
我用了this tool将您的 base64 转换为十六进制:
Accelerometer + Gyroscope: "zLAi/wD/SpwiAOv/VwBnAML/xQE="
cc b0 22 ff 00 ff 4a 9c 22 00 eb ff 57 00 67 00 c2 ff c5 01
Pressure: "bgBvAHAAcQByAA="
6e 00 6f 00 70 00 71 00 72 00
Battery: "ChYKARoA"
0a 16 0a 01 1a 00
不幸的是,如果不知道该设备的规范,就无法将这些十六进制值转换为数字。制造商可以从无数种方法中选择将数字映射为十六进制值。您将必须找到协议(protocol)文档或对其进行逆向工程。
这是 samples of how another BLE device converted numbers to hex (搜索“通知包含”)。你会发现这样的部分:
The notification consists of 6 byte:
byte 0: 02 byte 1: 01 byte 2: XY (see below) byte 3: valve open state in % (from 0x00 to 0x64) byte 4: undefined (battery level?) byte 5: (temperature * 2)
关于javascript - 将值隐藏为 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56355850/