Skip to content

Instantly share code, notes, and snippets.

@thangman22
Last active October 30, 2016 04:23
Show Gist options
  • Save thangman22/44594efa1658901d1dbdca50fc5f47c7 to your computer and use it in GitHub Desktop.
Save thangman22/44594efa1658901d1dbdca50fc5f47c7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function onButtonClick() {
let filters = [];
filters.push({services: ['battery_service']});
navigator.bluetooth.requestDevice({filters: filters})
.then(device => {
return device.gatt.connect();
})
.then(server => {
console.log('Getting Battery Service...');
return server.getPrimaryService('battery_service');
})
.then(service => {
console.log('Getting Battery Level Characteristic...');
return service.getCharacteristic('battery_level');
})
.then(characteristic => {
console.log('Reading Battery Level...');
return characteristic.readValue();
})
.then(value => {
let batteryLevel = value.getUint8(0);
console.log('> Battery Level is ' + batteryLevel + '%');
})
.catch(error => {
console.log('Argh! ' + error);
});
}
</script>
</head>
<body>
<button onclick="onButtonClick()">Connect</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment