Created
August 27, 2018 00:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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