mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Refactoring the WebBluetooth html tests.
This commit is contained in:
parent
9c0e7b1cf2
commit
5d3556a99d
23 changed files with 1112 additions and 119 deletions
87
tests/html/bluetooth/bluetooth_device_disconnect.html
Normal file
87
tests/html/bluetooth/bluetooth_device_disconnect.html
Normal file
|
@ -0,0 +1,87 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<title>Device Disconnect</title>
|
||||
<body>
|
||||
<input id="service" type="text" autofocus placeholder="Bluetooth Service">
|
||||
<input id="name" type="text" placeholder="Device Name">
|
||||
<input id="namePrefix" type="text" placeholder="Device Name Prefix">
|
||||
<button type="button" onclick="onScanButtonClick()">Scan()</button>
|
||||
<button type="button" onclick="onDisconnectButtonClick()">Disconnect()</button>
|
||||
<button type="button" onclick="onReconnectButtonClick()">Reconnect()</button>
|
||||
<pre id="log"></pre>
|
||||
<script src="bluetooth_functions.js"></script>
|
||||
<script>
|
||||
var bluetoothDevice;
|
||||
|
||||
function onScanButtonClick() {
|
||||
clear();
|
||||
var options = {filters: [{}]};
|
||||
|
||||
var filterService = document.getElementById('service').value;
|
||||
if (filterService) {
|
||||
if (filterService.startsWith('0x'))
|
||||
filterService = parseInt(filterService, 16);
|
||||
options.filters[0].services = [filterService];
|
||||
}
|
||||
|
||||
var filterName = document.getElementById('name').value;
|
||||
if (filterName)
|
||||
options.filters[0].name = filterName;
|
||||
|
||||
var filterNamePrefix = document.getElementById('namePrefix').value;
|
||||
if (filterNamePrefix)
|
||||
options.filters[0].namePrefix = filterNamePrefix;
|
||||
|
||||
try {
|
||||
clear();
|
||||
log('Requesting Bluetooth Device...');
|
||||
bluetoothDevice = window.navigator.bluetooth.requestDevice(options);
|
||||
log('Connecting to Bluetooth Device...');
|
||||
connect();
|
||||
} catch(err) {
|
||||
log(err);
|
||||
}
|
||||
}
|
||||
|
||||
function onDisconnectButtonClick() {
|
||||
clear();
|
||||
if (!bluetoothDevice)
|
||||
return log('> There is no connected Bluetooth Device instance, from which we can disconnect');
|
||||
|
||||
try {
|
||||
log('Disconnecting from Bluetooth Device...');
|
||||
if (bluetoothDevice.gatt.connected) {
|
||||
bluetoothDevice.gatt.disconnect();
|
||||
log('> Bluetooth Device connected: ' + bluetoothDevice.gatt.connected);
|
||||
} else {
|
||||
log('> Bluetooth Device is already disconnected');
|
||||
}
|
||||
} catch(err) {
|
||||
log(err);
|
||||
}
|
||||
}
|
||||
|
||||
function onReconnectButtonClick() {
|
||||
clear();
|
||||
if (!bluetoothDevice)
|
||||
log('> There is no connected Bluetooth Device instance, so we cannot reconnect')
|
||||
if (bluetoothDevice.gatt.connected) {
|
||||
log('> Bluetooth Device is already connected');
|
||||
return;
|
||||
} else {
|
||||
log('Connecting to Bluetooth Device...');
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
function connect() {
|
||||
try {
|
||||
log('Result of the connect() method of the GATT Server: ' + bluetoothDevice.gatt.connect());
|
||||
log('> Bluetooth Device connected: ' + bluetoothDevice.gatt.connected);
|
||||
} catch(err) {
|
||||
log(err);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue