/
write-descriptor-async-await.js
61 lines (48 loc) · 1.87 KB
/
write-descriptor-async-await.js
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
var myDescriptor;
async function onReadButtonClick() {
let serviceUuid = document.querySelector('#service').value;
if (serviceUuid.startsWith('0x')) {
serviceUuid = parseInt(serviceUuid);
}
let characteristicUuid = document.querySelector('#characteristic').value;
if (characteristicUuid.startsWith('0x')) {
characteristicUuid = parseInt(characteristicUuid);
}
try {
log('Requesting any Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true, optionalServices: [serviceUuid]});
log('Connecting to GATT Server...');
const server = await device.gatt.connect();
log('Getting Service...');
const service = await server.getPrimaryService(serviceUuid);
log('Getting Characteristic...');
const characteristic = await service.getCharacteristic(characteristicUuid);
log('Getting Descriptor...');
myDescriptor = await characteristic.getDescriptor('gatt.characteristic_user_description');
document.querySelector('#writeButton').disabled =
!characteristic.properties.write;
log('Reading Descriptor...');
const value = await myDescriptor.readValue();
let decoder = new TextDecoder('utf-8');
log('> Characteristic User Description: ' + decoder.decode(value));
} catch(error) {
document.querySelector('#writeButton').disabled = true;
log('Argh! ' + error);
}
}
async function onWriteButtonClick() {
if (!myDescriptor) {
return;
}
let encoder = new TextEncoder('utf-8');
let value = document.querySelector('#description').value;
try {
log('Setting Characteristic User Description...');
await myDescriptor.writeValue(encoder.encode(value));
log('> Characteristic User Description changed to: ' + value);
} catch(error) {
log('Argh! ' + error);
}
}