Skip to content

Instantly share code, notes, and snippets.

@loginov-rocks
Created August 27, 2018 00:38
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/e03b1dd9e038eaf4eb65413d97a34678 to your computer and use it in GitHub Desktop.
Save loginov-rocks/e03b1dd9e038eaf4eb65413d97a34678 to your computer and use it in GitHub Desktop.
How to make a web app for your own Bluetooth Low Energy device? Step 10
function requestBluetoothDevice() {
log('Requesting bluetooth device...');
return navigator.bluetooth.requestDevice({
filters: [{services: [0xFFE0]}],
}).
then(device => {
log('"' + device.name + '" bluetooth device selected');
deviceCache = device;
// Added line
deviceCache.addEventListener('gattserverdisconnected',
handleDisconnection);
return deviceCache;
});
}
function handleDisconnection(event) {
let device = event.target;
log('"' + device.name +
'" bluetooth device disconnected, trying to reconnect...');
connectDeviceAndCacheCharacteristic(device).
then(characteristic => startNotifications(characteristic)).
catch(error => log(error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment