Reputation: 11
I'm developing a software who will use Web Bluetooth API to connect to a BT to Serial Adapter. It seems to have support for Write and Notify. But I can't get nofity to work.
The event is never fired. Right now I'm testing in Canary on Mac.
Thanks Anders
My code for search/pair and add events:
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();
});
};
Upvotes: 0
Views: 2267
Reputation: 11
Yes, you're absolutely right. There was 2 errors first in baud rate against my serial to BT adapter which made that I didn't send what I thought I was. And the other was that startnotifications was not called.
Thanks Anders
Upvotes: 0
Reputation: 5629
According to https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#receive_gatt_notifications, it looks like you need to call characteristic.startNotifications()
to let the browser know you want to receive GATT notifications:
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
}
Upvotes: 5