Last active
October 30, 2016 04:23
-
-
Save thangman22/44594efa1658901d1dbdca50fc5f47c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!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