Skip to content

Instantly share code, notes, and snippets.

@paramaggarwal
Created March 20, 2020 12:45
Show Gist options
  • Save paramaggarwal/fdd344aad7f55c29ac1e59d9dfea56bd to your computer and use it in GitHub Desktop.
Save paramaggarwal/fdd344aad7f55c29ac1e59d9dfea56bd to your computer and use it in GitHub Desktop.
Trying Web Bluetooth
<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