我正在学习使用网络蓝牙 api 编写网络应用程序并使用 chrome/chromium 运行它。但是通知只响应几次,我不知道为什么以及如何调试它(看看发生了什么)。
蓝牙外围设备是血氧仪,使用 BLE 发送实时血氧饱和度、心率等。我的浏览器使用基于 Debian 9.1 构建的 Chromium 60.0.3112.78,在 Debian 9.1(64 位)上运行。
下面是我的javascript:
var serviceUuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
characteristicUuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
// Sorry, I hide the UUID.
document.querySelector('#button').addEventListener('click', function(event) {
onStartButtonClick();
}});
async function onStartButtonClick(){
let options = {};
options.acceptAllDevices = true;
options.optionalServices = [serviceUuid];
try{
const device = await navigator.bluetooth.requestDevice(options);
device.addEventListener('gattserverdisconnected', onDisconnected);
console.log('Got device:', device.name);
console.log('id:', device.id);
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);
myCharacteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
await myCharacteristic.startNotifications();
console.log('> Notifications started');
} catch(error) {
console.log('Argh! ' + error);
}
}
async function disconnect(){
await myCharacteristic.stopNotifications();
onDisconnected();
}
function onDisconnected(event) {
// Object event.target is Bluetooth Device getting disconnected.
console.log('> Bluetooth Device disconnected');
}
var tmp_count=0;
async function handleNotifications(event) {
// I will read data by Uint8Array.
// var databuf = new Uint8Array(event.target.value.buffer);
tmp_count++;
console.log(tmp_count);
}
铬显示器的控制台:
03:41:49.893 (index):192 Connecting to GATT Server...
03:41:50.378 (index):195 Getting Service...
03:41:51.237 (index):198 Getting Characteristic...
03:41:51.359 (index):204 > Notifications started
03:41:51.781 (index):228 1
03:41:51.782 (index):228 2
03:42:22.573 (index):217 > Bluetooth Device disconnected
之后没有反应03:41:51.782 (index):228 2
,所以我关掉了血氧仪。
问题是什么 ?我能做什么?谢谢。