Created
January 30, 2018 18:55
-
-
Save spacelatte/4e94d69ce3d9b06613f737e4ab4277c3 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
module.exports = { | |
"0000fee0-0000-1000-8000-00805f9b34fb": { | |
"0000ff05-0000-1000-8000-00805f9b34fb": function(char) { | |
return char.writeValue(new Uint8Array([ | |
0x12, | |
0x01, | |
])).then(console.log).catch(console.log) | |
}, | |
"0000ff0e-0000-1000-8000-00805f9b34fb": function(char) { | |
char.addEventListener("characteristicvaluechanged", function(event) { | |
let self = new Int16Array(this.value.buffer) | |
console.log(JSON.stringify(self)) | |
}) | |
char.startNotifications().then(function(result) { | |
console.log(result) | |
}) | |
char.writeValue(new Uint8Array([ 1, ])).then(function(res) { | |
return res | |
}) | |
}, | |
}, | |
} |
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
function ble(map, cleanup) { | |
return navigator.bluetooth.requestDevice({ | |
optionalServices: Object.keys(map), | |
acceptAllDevices: true, | |
}).then(function(device) { | |
device.gatt.connect().then(function(response) { | |
response.device.ongattserverdisconnected = function(e) { | |
if(typeof cleanup === "function") { | |
cleanup(e) | |
} | |
} | |
device.gatt.getPrimaryServices().then(function(services) { | |
services.forEach(function(service) { | |
service.getCharacteristics().then(function(characteristics) { | |
characteristics.forEach(function(characteristic) { | |
let id = { | |
c: characteristic.uuid, | |
s: service.uuid, | |
} | |
if(id.s in map && id.c in map[id.s]) { | |
map[id.s][id.c](characteristic) | |
} | |
}) | |
}).catch(console.log) | |
}) | |
}).catch(console.log) | |
}).catch(console.log) | |
}).catch(console.log) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment