Files
servo/tests/html/bluetooth/bluetooth_descriptor_info.html
2016-09-26 20:02:42 +02:00

64 lines
2.4 KiB
HTML

<!DOCTYPE html>
<html>
<title>Descriptor info</title>
<body>
<input id="service" type="text" autofocus placeholder="Bluetooth Service">
<input id="characteristic" type="text" autofocus placeholder="Bluetooth Characteristic">
<input id="descriptor" type="text" autofocus placeholder="Bluetooth Descriptor">
<button type="button" onclick="onButtonClick()">Get Descriptor Info</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var serviceUuid = document.getElementById('service').value;
var characteristicUuid = document.getElementById('characteristic').value;
var descriptorUuid = document.getElementById('descriptor').value;
if (!serviceUuid || !characteristicUuid || !descriptorUuid) {
return log('All input field must be filled!');
}
if (serviceUuid.startsWith('0x'))
serviceUuid = parseInt(serviceUuid, 16);
if (characteristicUuid.startsWith('0x'))
characteristicUuid = parseInt(characteristicUuid, 16);
if (descriptorUuid.startsWith('0x'))
descriptorUuid = parseInt(descriptorUuid, 16);
log('Requesting Bluetooth Device...');
window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => {
log('Connecting to GATT Server on device...');
return device.gatt.connect();
})
.then(server => {
log('Getting Primary Service...');
return server.getPrimaryService(serviceUuid);
})
.then(service => {
log('Getting Characteristic...');
return service.getCharacteristic(characteristicUuid);
})
.then(characteristic => {
log('Getting Descriptor...');
return characteristic.getDescriptor(descriptorUuid);
})
.then(descriptor => {
log('Descriptor found!');
log('> Descriptor characteristic: ' + descriptor.characteristic.uuid);
log('> Descriptor UUID: ' + descriptor.uuid);
return descriptor.readValue();
})
.then(value => {
log('> Descriptor value: ' + asciiToDecimal(value));
})
.catch(err => {
log(err);
});
}
</script>
</body>
</html>