-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbattery.js
23 lines (21 loc) · 826 Bytes
/
battery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function battery_connect() {
let serviceUuid = "battery_service";
let characteristicUuid = "battery_level"
navigator.bluetooth.requestDevice
({
filters: [{ services: [serviceUuid] }]
, optionalServices: [serviceUuid]
})
.then(device => { return device.gatt.connect(); })
.then(server => { return server.getPrimaryService(serviceUuid); })
.then(service => { return service.getCharacteristic(characteristicUuid); })
.then(characteristic => { return characteristic.readValue(); })
.then(value => {
var percentage = value.getUint8(0);
setBatteryPercentage(percentage);
log('Battery percentage is ' + percentage);
})
.catch(error => {
log('Error! ' + error);
});
}