我需要使用Web Bluetooth 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个字节,因此我正在使用一个子字符串并尝试写入该值。
答案 0 :(得分:0)
App Engine,而推荐使用更具体的writeValue()和writeValueWithResponse()。尝试使用其中任何一种,看看是否适合您。
有关这两个API的更多详细信息,您可以查看关于它的writeValueWithoutResponse()条目。
答案 1 :(得分:0)
如果您在通过网页连接之前手动将计算机连接到蓝牙设备是否有效?
如果您使用的是 Chrome(或基于 Chromium 的浏览器),那么这可能是因为 Chrome 不会在所有操作系统上自动配对。要读取/写入加密特征,蓝牙设备必须与计算机 bonded。 macOS 和 Android 会在必要时绑定,但 Windows/Linux/ChromeOS 不会。这应该有望很快得到纠正。您可以关注https://crbug.com/960258来跟踪这项工作的进展。