javascript - 在 chrome web BLE 中调用 startNotifications() 后未收到值
问题描述
我正在尝试用 chrome 连接Muse 头带,这样我就可以流式传输实时脑电波数据。
我可以使用端口在 python 中很好地连接和流式传输数据,但是无法通过 web BLE 流式传输数据。
使用下面的代码,我可以连接到设备并 startNotifications(),但是“characteristicvaluechanged”的事件侦听器没有被触发。我也尝试过连接其他蓝牙设备,但没有读取任何值,所以我认为这不是 Muse 问题。
<h1>Test HTML</h1>
<button id="read" onclick="onButtonClick(event)">Connect with the BLE device</button>
<script>
function onButtonClick(event){
connectBLE()
}
async function connectBLE() {
let serviceUuid = '5052494d-2dab-0341-6972-6f6861424c45'
let characteristicUuid = '43484152-2dab-3141-6972-6f6861424c45'
try {
console.log('Requesting Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
filters: [{services: [serviceUuid]}]});
console.log('Connecting to GATT Server...');
const server = await device.gatt.connect();
console.log('Getting Service...');
const service = await server.getPrimaryService(serviceUuid);
console.log('Getting Characteristic...');
myCharacteristic = await service.getCharacteristic(characteristicUuid);
await myCharacteristic.startNotifications();
console.log(myCharacteristic)
console.log('> Notifications started');
myCharacteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
} catch(error) {
console.log('Argh! ' + error);
}
}
function handleNotifications(event) {
console.log('YEAH!')
let value = event.target.value;
let a = [];
// Convert raw data bytes to hex values just for the sake of showing something.
// In the "real" world, you'd use data.getUint8, data.getUint16 or even
// TextDecoder to process raw data bytes.
for (let i = 0; i < value.byteLength; i++) {
a.push('0x' + ('00' + value.getUint8(i).toString(16)).slice(-2));
}
console.log('> ' + a.join(' '));
}
</script>
有人遇到过类似的问题吗?我在网上找不到任何解决方案,所以这是我最后的手段。
PS:我正在运行 Ubuntu 18.04 和 Chromium 83.0.4103.61
解决方案
推荐阅读
- php - html onclick不会触发php函数
- c - 使用 GDB 调试时 1ms posix 计时器未正确计时
- python - 在管理员更改表单中如何访问我的模型的当前实例?
- tensorflow - 张量流成本等于无
- git - Visual Studio 项目文件夹约定
- java - JSON多态反序列化java
- javascript - jQuery 或 Javascript 模拟移动双 Tab 屏幕调整大小
- xcode - 升级到 MacOS Mojave 后无法使用自制软件安装 Python 库
- angular - 如何在 Ngrx 中将值传递给 Effect - Angular
- python - 从可以在新类/窗口中使用的按钮获取价值