Skip to content

Commit 156da29

Browse files
committed
Test app
1 parent e403fcb commit 156da29

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

get-devices.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function populateBluetoothDevices() {
2+
const devicesSelect = document.querySelector('#devicesSelect');
3+
log('Getting existing permitted Bluetooth devices...');
4+
navigator.bluetooth.getDevices()
5+
.then(devices => {
6+
log('> Got ' + devices.length + ' Bluetooth devices.');
7+
devicesSelect.textContent = '';
8+
for (const device of devices) {
9+
const option = document.createElement('option');
10+
option.value = device.id;
11+
option.textContent = device.name;
12+
devicesSelect.appendChild(option);
13+
}
14+
})
15+
.catch(error => {
16+
log('Argh! ' + error);
17+
});
18+
}
19+
20+
function onRequestBluetoothDeviceButtonClick() {
21+
log('Requesting any Bluetooth device...');
22+
navigator.bluetooth.requestDevice({
23+
// filters: [...] <- Prefer filters to save energy & show relevant devices.
24+
acceptAllDevices: true
25+
})
26+
.then(device => {
27+
log('> Requested ' + device.name + ' (' + device.id + ')');
28+
populateBluetoothDevices();
29+
})
30+
.catch(error => {
31+
log('Argh! ' + error);
32+
});
33+
}
34+
35+
function onForgetBluetoothDeviceButtonClick() {
36+
navigator.bluetooth.getDevices()
37+
.then(devices => {
38+
const deviceIdToForget = document.querySelector('#devicesSelect').value;
39+
const device = devices.find((device) => device.id == deviceIdToForget);
40+
if (!device) {
41+
throw new Error('No Bluetooth device to forget');
42+
}
43+
log('Forgetting ' + device.name + 'Bluetooth device...');
44+
return device.forget();
45+
})
46+
.then(() => {
47+
log(' > Bluetooth device has been forgotten.');
48+
populateBluetoothDevices();
49+
})
50+
.catch(error => {
51+
log('Argh! ' + error);
52+
});
53+
}
54+
55+
window.onload = () => {
56+
populateBluetoothDevices();
57+
};

t5m4r.html

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,78 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>T5M4R HUD</title>
7+
<script src="get-devices.js"></script>
78
</head>
89
<body>
910
<h1>T5M4R HUP App</h1>
10-
<p>This document provides an overview of the T5M4R model, its architecture
11-
and usage guidelines.</p>
11+
<p>Chrome advanced <a href="chrome://flags/#enable-web-bluetooth-new-permissions-backend">new permissions backend for Web Bluetooth</a> and <a href="chrome://flags/#enable-experimental-web-platform-features">Experimental Web Platform flag</a> are ENABLED.</p>
1212
<h2>Model Overview</h2>
13+
14+
<p>
15+
<button id="requestBluetoothDevice">Request Bluetooth Device</button>
16+
</p>
17+
<p>
18+
<select id="devicesSelect"></select>
19+
<button id="forgetBluetoothDevice">Forget Bluetooth Device</button>
20+
</p>
21+
22+
<script>
23+
var ChromeSamples = {
24+
log: function() {
25+
var line = Array.prototype.slice.call(arguments).map(function(argument) {
26+
return typeof argument === 'string' ? argument : JSON.stringify(argument);
27+
}).join(' ');
28+
29+
document.querySelector('#log').textContent += line + '\n';
30+
},
31+
32+
clearLog: function() {
33+
document.querySelector('#log').textContent = '';
34+
},
35+
36+
setStatus: function(status) {
37+
document.querySelector('#status').textContent = status;
38+
},
39+
40+
setContent: function(newContent) {
41+
var content = document.querySelector('#content');
42+
while(content.hasChildNodes()) {
43+
content.removeChild(content.lastChild);
44+
}
45+
content.appendChild(newContent);
46+
}
47+
};
48+
</script>
49+
50+
<h3>Live Output</h3>
51+
<div id="output" class="output">
52+
<div id="content">Output area</div>
53+
<div id="status"></div>
54+
<pre id="log"></pre>
55+
</div>
56+
57+
<script>
58+
log = ChromeSamples.log;
59+
60+
function isWebBluetoothEnabled() {
61+
if (navigator.bluetooth) {
62+
return true;
63+
} else {
64+
ChromeSamples.setStatus('Web Bluetooth API is not available.\n' +
65+
'Please make sure the "Experimental Web Platform features" flag is enabled.');
66+
return false;
67+
}
68+
}
69+
</script>
70+
71+
<script>
72+
if (isWebBluetoothEnabled()) {
73+
document.querySelector('#requestBluetoothDevice').addEventListener('click', function() {
74+
onRequestBluetoothDeviceButtonClick();
75+
});
76+
document.querySelector('#forgetBluetoothDevice').addEventListener('click', function() {
77+
onForgetBluetoothDeviceButtonClick();
78+
});
79+
}
80+
</script>
1381
</body>

0 commit comments

Comments
 (0)