Created
March 20, 2020 12:45
-
-
Save paramaggarwal/fdd344aad7f55c29ac1e59d9dfea56bd to your computer and use it in GitHub Desktop.
Trying Web Bluetooth
This file contains 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
<html> | |
<script> | |
function onButtonClick() { | |
const serviceUuid = "body_composition"; | |
console.log("Requesting Bluetooth Device..."); | |
navigator.bluetooth | |
.requestDevice({ filters: [{ services: [serviceUuid] }] }) | |
.then(device => { | |
console.log("Connecting to GATT Server..."); | |
return device.gatt.connect(); | |
}) | |
.then(server => { | |
console.log("Getting Service..."); | |
return server.getPrimaryService(serviceUuid); | |
}) | |
.then(service => { | |
console.log("Getting Characteristic..."); | |
return service.getCharacteristic("body_composition_measurement"); | |
}) | |
.then(characteristic => { | |
console.log(characteristic); | |
characteristic.addEventListener("characteristicvaluechanged", e => { | |
console.log(e); | |
}); | |
characteristic.startNotifications(); | |
return characteristic.readValue(); | |
}) | |
.then(value => { | |
console.log(value); | |
}) | |
.catch(error => { | |
console.log("Argh! " + error); | |
}); | |
} | |
</script> | |
<body> | |
<button onclick="onButtonClick()">Start</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment