我正在開發一個軟件,他將使用Web藍牙API連接到BT到串行適配器。它似乎支持寫入和通知。但我不能工作。無法通知在Web藍牙API中工作
事件永遠不會被解僱。現在我正在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();
});
};