Files
servo/tests/html/bluetooth/bluetooth_battery_level.html
2016-09-26 20:02:42 +02:00

41 lines
1.2 KiB
HTML

<!DOCTYPE html>
<html>
<title>Battery Level</title>
<body>
<button type="button" onclick="onButtonClick()">Get Bluetooth Device's Battery Level</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var options = {filters: [{services: ['battery_service']}], optionalServices: []};
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice(options)
.then(device => {
log('Connecting to GATT Server on device...');
return device.gatt.connect();
})
.then(server => {
log('Getting Battery Service...');
return server.getPrimaryService('battery_service');
})
.then(service => {
log('Getting Battery Level Characteristic...');
return service.getCharacteristic('battery_level');
})
.then(characteristic => {
log('Reading Battery Level...');
return characteristic.readValue();
})
.then(value => {
log('> Battery Level is ' + asciiToDecimal(value) + '%');
})
.catch(err => {
log(err);
});
}
</script>
</body>
</html>