Created
March 3, 2018 13:25
-
-
Save BinaryBrain/b84f8f3f4c3b28a23e1cd971a26ea874 to your computer and use it in GitHub Desktop.
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
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