/
bluetooth_descriptor_info.html
57 lines (48 loc) · 2.22 KB
/
bluetooth_descriptor_info.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!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);
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]});
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
log('Getting Primary Service...');
var primaryService = server.getPrimaryService(serviceUuid);
log('Getting Characteristic...');
var characteristic = primaryService.getCharacteristic(characteristicUuid);
log('Getting Descriptor...');
var descriptor = characteristic.getDescriptor(descriptorUuid);
log('Descriptor found!');
log('> Descriptor characteristic: ' + descriptor.characteristic.uuid);
log('> Descriptor UUID: ' + descriptor.uuid);
descriptor.readValue();
log('> Descriptor value: ' + asciiToDecimal(descriptor.value));
} catch(err) {
log(err);
}
}
</script>
</body>
</html>