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

57 lines
2.1 KiB
HTML

<!DOCTYPE html>
<html>
<title>Primary Services info</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="onButtonClick()">Get Primary Services Info</button>
<pre id="log"></pre>
<script src="bluetooth_functions.js"></script>
<script>
function onButtonClick() {
clear();
var options = {filters: [], optionalServices: []};
var filterService = document.getElementById('service').value;
if (filterService) {
if (filterService.startsWith('0x'))
filterService = parseInt(filterService, 16);
options.filters.push({services: [filterService]});
}
var filterName = document.getElementById('name').value;
if (filterName)
options.filters.push({name: filterName});
var filterNamePrefix = document.getElementById('namePrefix').value;
if (filterNamePrefix)
options.filters.push({namePrefix: filterNamePrefix});
try {
log('Requesting Bluetooth Device...');
var device = window.navigator.bluetooth.requestDevice(options);
log('Connecting to GATTserver on device...');
var server = device.gatt.connect();
log('Getting Primary Service...');
var primaryServices;
if (filterService) {
primaryServices = server.getPrimaryServices(filterService);
} else {
primaryServices = server.getPrimaryServices();
}
log('> List of Services on the current device:');
for(i = 0; i < primaryServices.length; ++i) {
log('> #' + (i+1));
log('> UUID: ' + primaryServices[i].uuid);
log('> Is primary: ' + primaryServices[i].isPrimary);
}
} catch(err) {
log(err);
}
}
</script>
</body>
</html>