我正在开发一种软件,它将使用Web蓝牙API连接到BT到串行适配器。它似乎支持Write和Notify。但我无法工作。
该事件永远不会被解雇。现在我正在Mac上的Canary进行测试。
由于 安德斯
我的搜索/配对和添加事件的代码:
var readCharacteristic;
var writeCharacteristic;
var serialBLEService = 'ba920001-d666-4d59-b141-05960b3a4ff7';
var txChar = 'ba920002-d666-4d59-b141-05960b3a4ff7';
var rxChar = 'ba920003-d666-4d59-b141-05960b3a4ff7';
$scope.writeToSerial = function (valueToWrite) {
var tmpValue = $('input').val();
valueToWrite = utf8AbFromStr(tmpValue);
writeCharacteristic.writeValue(valueToWrite)
.then( a => {
alert("Written: " + valueToWrite);
})
.catch(function (error) {
// And of course: error handling!
console.error('Something went wrong!', error);
});
}
function handleCharacteristicValueChanged(event) {
var value = event.target.value;
console.log('Received ' + value);
}
$scope.searchForBTDevices = function() {
navigator.bluetooth.requestDevice({
filters: [{ services: [serialBLEService] }],
optionalServices: [
serialBLEService, rxChar, txChar, configChar
]
})
.then(device => {
return device.gatt.connect();
})
.then(server => {
return server.getPrimaryService(serialBLEService);
})
.then(service => {
return service.getCharacteristics();
})
.then(characteristics => {
$scope.btResult += '>> Found Characteristics!\n';
$scope.$apply();
var queue = Promise.resolve();
characteristics.forEach(characteristic => {
switch (characteristic.uuid) {
case rxChar:
readCharacteristic = characteristic;
readCharacteristic.addEventListener('characteristicvaluechanged',
handleCharacteristicValueChanged);
break;
case txChar:
writeCharacteristic = characteristic;
break;
}
});
})
.catch(error => {
$scope.btResult = '>> Error: ' + error;
$scope.$digest();
});
};
答案 0 :(得分:3)
根据https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#receive_gatt_notifications,您似乎需要致电characteristic.startNotifications()
让浏览器知道您想要接收GATT通知:
navigator.bluetooth.requestDevice({ filters: [{ services: ['heart_rate'] }] })
.then(device => device.gatt.connect())
.then(server => server.getPrimaryService('heart_rate'))
.then(service => service.getCharacteristic('heart_rate_measurement'))
.then(characteristic => characteristic.startNotifications())
.then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged',
handleCharacteristicValueChanged);
console.log('Notifications have been started.');
})
.catch(error => { console.log(error); });
function handleCharacteristicValueChanged(event) {
var value = event.target.value;
console.log('Received ' + value);
// TODO: Parse Heart Rate Measurement value.
// See https://github.com/WebBluetoothCG/demos/blob/gh-pages/heart-rate-sensor/heartRateSensor.js
}
答案 1 :(得分:0)
是的,你是绝对正确的。对我的串口到BT适配器的波特率首先出现了2个错误,这使得我没有发送我认为的那个。另一个是没有调用startnotifications。
由于 安德斯