【发布时间】:2020-09-18 14:08:06
【问题描述】:
我需要使用 Web 蓝牙 API 将加密值写入 BLE 设备的特征。我可以记录服务和特性。但我无法写入值。
我们指的是一个可以成功写入特征的python脚本。写入的数据经过加密后转换为字节数组写入特征。
我们正在尝试通过 javascript 将相同的值写入特征,但我们得到 NotSupportedError。
有人可以帮我找出解决方案吗?
代码:
navigator.bluetooth
.requestDevice({
filters: [{ name: deviceName }],
optionalServices: [GATT_SERVICE],
})
.then((device) => {
// Step 2: Connect to it
console.log("device:", device);
return device.gatt.connect();
})
.then((server) => {
// Step 3: Get the Service
console.log("server: ", server);
return server.getPrimaryService(GATT_SERVICE);
})
.then((service) => {
// Step 4: get the Characteristic
console.log("service: ", service);
return service.getCharacteristic(GATT_CHARACTERISTIC);
})
.then((characteristic) => {
// data contains the value to be written to the BLE device
writeBuffer(data, 0);
function writeBuffer(data, start) {
writeOut(data, start);
}
function writeOut(data, start) {
if (start >= data.length) return;
characteristic
.writeValue(
new TextEncoder().encode(data.substring(start, start + 20))
)
.then(() => {
writeOut(data, start + 20);
});
}
})
.catch((error) => {
console.log(error);
});
由于长度超过 512 个字节,我正在取一个子字符串并尝试写入值。
【问题讨论】:
标签: bluetooth-lowenergy web-bluetooth