servo/tests/html/bluetooth/bluetooth_device_disconnect.html
2016-07-28 14:26:23 +02:00

87 lines
2.9 KiB
HTML

<!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>