Skip to content

Instantly share code, notes, and snippets.

@oliverturner
Created February 28, 2018 16:24
Show Gist options
  • Save oliverturner/a72e7589abff74ec4308556bd8bae385 to your computer and use it in GitHub Desktop.
Save oliverturner/a72e7589abff74ec4308556bd8bae385 to your computer and use it in GitHub Desktop.
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