Skip to content

Instantly share code, notes, and snippets.

@loginov-rocks
Created August 27, 2018 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loginov-rocks/140ee65772f87ab775cb94cae850eb3a to your computer and use it in GitHub Desktop.
Save loginov-rocks/140ee65772f87ab775cb94cae850eb3a to your computer and use it in GitHub Desktop.
How to make a web app for your own Bluetooth Low Energy device? Step 12
// Enable the characteristic changes notification
function startNotifications(characteristic) {
log('Starting notifications...');
return characteristic.startNotifications().
then(() => {
log('Notifications started');
// Added line
characteristic.addEventListener('characteristicvaluechanged',
handleCharacteristicValueChanged);
});
}
function disconnect() {
if (deviceCache) {
log('Disconnecting from "' + deviceCache.name + '" bluetooth device...');
deviceCache.removeEventListener('gattserverdisconnected',
handleDisconnection);
if (deviceCache.gatt.connected) {
deviceCache.gatt.disconnect();
log('"' + deviceCache.name + '" bluetooth device disconnected');
}
else {
log('"' + deviceCache.name +
'" bluetooth device is already disconnected');
}
}
// Added condition
if (characteristicCache) {
characteristicCache.removeEventListener('characteristicvaluechanged',
handleCharacteristicValueChanged);
characteristicCache = null;
}
deviceCache = null;
}
// Data receiving
function handleCharacteristicValueChanged(event) {
let value = new TextDecoder().decode(event.target.value);
log(value, 'in');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment