Skip to content

Instantly share code, notes, and snippets.

@loginov-rocks
Created August 27, 2018 00:16
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/4cf8d0d720dafcf52781748d5c975452 to your computer and use it in GitHub Desktop.
Save loginov-rocks/4cf8d0d720dafcf52781748d5c975452 to your computer and use it in GitHub Desktop.
How to make a web app for your own Bluetooth Low Energy device? Step 7
// Characteristic object cache
let characteristicCache = null;
// Connect to the device specified, get service and characteristic
function connectDeviceAndCacheCharacteristic(device) {
if (device.gatt.connected && characteristicCache) {
return Promise.resolve(characteristicCache);
}
log('Connecting to GATT server...');
return device.gatt.connect().
then(server => {
log('GATT server connected, getting service...');
return server.getPrimaryService(0xFFE0);
}).
then(service => {
log('Service found, getting characteristic...');
return service.getCharacteristic(0xFFE1);
}).
then(characteristic => {
log('Characteristic found');
characteristicCache = characteristic;
return characteristicCache;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment