forked from servo/servo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_characteristic_info.html
69 lines (60 loc) · 2.82 KB
/
bluetooth_characteristic_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
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<title>Characteristic info</title>
<body>
<input id="service" type="text" autofocus placeholder="Bluetooth Service">
<input id="characteristic" type="text" autofocus placeholder="Bluetooth Characteristic">
<button type="button" onclick="onButtonClick()">Get Characteristic Info</button>
<pre id="log"></pre>
<script>
function onButtonClick() {
clear();
var serviceUuid = document.getElementById('service').value;
if (serviceUuid.startsWith('0x'))
serviceUuid = parseInt(serviceUuid, 16);
var characteristicUuid = document.getElementById('characteristic').value;
if (characteristicUuid.startsWith('0x'))
characteristicUuid = parseInt(characteristicUuid, 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('Characteristic found!');
log('> Characteristic service: ' + characteristic.service.uuid);
log('> Characteristic UUID: ' + characteristic.uuid);
log('> Broadcast: ' + characteristic.properties.broadcast);
log('> Read: ' + characteristic.properties.read);
log('> Write w/o response: ' + characteristic.properties.writeWithoutResponse);
log('> Write: ' + characteristic.properties.write);
log('> Notify: ' + characteristic.properties.notify);
log('> Indicate: ' + characteristic.properties.indicate);
log('> Signed Write: ' + characteristic.properties.authenticatedSignedWrites);
log('> Queued Write: ' + characteristic.properties.reliableWrite);
log('> Writable Auxiliaries: ' + characteristic.properties.writableAuxiliaries);
characteristic.readValue();
log('> Characteristic value: ' + AsciiToDecimal(characteristic.value));
} catch(err) {
log(err);
}
}
function clear() {
document.getElementById("log").textContent = "";
}
function log(line) {
document.getElementById("log").textContent += line + '\n';
}
function AsciiToDecimal(bytestr) {
var result = [];
for(i = 0; i < bytestr.length; i++) {
result[i] = bytestr[i].charCodeAt(0) ;
}
return result;
}
</script>
</body>
</html>