Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BinaryBrain/b84f8f3f4c3b28a23e1cd971a26ea874 to your computer and use it in GitHub Desktop.
Save BinaryBrain/b84f8f3f4c3b28a23e1cd971a26ea874 to your computer and use it in GitHub Desktop.
function log(s) { console.log(s) }
var myDescriptor;
function onReadButtonClick() {
let serviceUuid = 0xff40;
let characteristicUuid = 0xff42;
log('Requesting any Bluetooth Device...');
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: [serviceUuid]})
.then(device => {
log('Connecting to GATT Server...');
return device.gatt.connect();
})
.then(server => {
log('Getting Service...');
return server.getPrimaryService(serviceUuid);
})
.then(service => {
log('Getting Characteristic...');
return service.getCharacteristic(characteristicUuid);
})
.then(characteristic => {
log('Getting Descriptor...');
var buffer = new ArrayBuffer(2);
var view = new DataView(buffer);
view.setInt8(0, 21);
console.log(view.getInt8(0));
setTimeout(() => {
characteristic.writeValue(buffer);
console.log("Writing...");
}, 4000);
})
.catch(error => {
log('Argh! ' + error);
});
}
onReadButtonClick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment