Created
February 28, 2018 16:24
-
-
Save oliverturner/a72e7589abff74ec4308556bd8bae385 to your computer and use it in GitHub Desktop.
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
function getBatteryLevel() { | |
navigator.bluetooth | |
.requestDevice({ filters: [{ services: ["battery_service"] }] }) | |
.then(device => device.gatt.connect()) | |
.then(server => server.getPrimaryService("battery_service")) | |
.then(service => service.getCharacteristic("battery_level")) | |
.then(characteristic => characteristic.readValue()) | |
.then(value => { | |
console.log(`> Battery Level is ${value.getUint8(0)}%`); | |
}) | |
.catch(error => { | |
console.log("Something went wrong", error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment