-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-devices.js
More file actions
57 lines (54 loc) · 1.62 KB
/
get-devices.js
File metadata and controls
57 lines (54 loc) · 1.62 KB
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
function populateBluetoothDevices() {
const devicesSelect = document.querySelector('#devicesSelect');
log('Getting existing permitted Bluetooth devices...');
navigator.bluetooth.getDevices()
.then(devices => {
log('> Got ' + devices.length + ' Bluetooth devices.');
devicesSelect.textContent = '';
for (const device of devices) {
const option = document.createElement('option');
option.value = device.id;
option.textContent = device.name;
devicesSelect.appendChild(option);
}
})
.catch(error => {
log('Argh! ' + error);
});
}
function onRequestBluetoothDeviceButtonClick() {
log('Requesting any Bluetooth device...');
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true
})
.then(device => {
log('> Requested ' + device.name + ' (' + device.id + ')');
populateBluetoothDevices();
})
.catch(error => {
log('Argh! ' + error);
});
}
function onForgetBluetoothDeviceButtonClick() {
navigator.bluetooth.getDevices()
.then(devices => {
const deviceIdToForget = document.querySelector('#devicesSelect').value;
const device = devices.find((device) => device.id == deviceIdToForget);
if (!device) {
throw new Error('No Bluetooth device to forget');
}
log('Forgetting ' + device.name + 'Bluetooth device...');
return device.forget();
})
.then(() => {
log(' > Bluetooth device has been forgotten.');
populateBluetoothDevices();
})
.catch(error => {
log('Argh! ' + error);
});
}
window.onload = () => {
populateBluetoothDevices();
};