()
| 1 | async function onButtonClick() { |
| 2 | let filters = []; |
| 3 | |
| 4 | let filterName = document.querySelector('#name').value; |
| 5 | if (filterName) { |
| 6 | filters.push({name: filterName}); |
| 7 | } |
| 8 | |
| 9 | let filterNamePrefix = document.querySelector('#namePrefix').value; |
| 10 | if (filterNamePrefix) { |
| 11 | filters.push({namePrefix: filterNamePrefix}); |
| 12 | } |
| 13 | |
| 14 | let options = {}; |
| 15 | if (document.querySelector('#allAdvertisements').checked) { |
| 16 | options.acceptAllAdvertisements = true; |
| 17 | } else { |
| 18 | options.filters = filters; |
| 19 | } |
| 20 | |
| 21 | try { |
| 22 | log('Requesting Bluetooth Scan with options: ' + JSON.stringify(options)); |
| 23 | const scan = await navigator.bluetooth.requestLEScan(options); |
| 24 | |
| 25 | log('Scan started with:'); |
| 26 | log(' acceptAllAdvertisements: ' + scan.acceptAllAdvertisements); |
| 27 | log(' active: ' + scan.active); |
| 28 | log(' keepRepeatedDevices: ' + scan.keepRepeatedDevices); |
| 29 | log(' filters: ' + JSON.stringify(scan.filters)); |
| 30 | |
| 31 | navigator.bluetooth.addEventListener('advertisementreceived', event => { |
| 32 | log('Advertisement received.'); |
| 33 | log(' Device Name: ' + event.device.name); |
| 34 | log(' Device ID: ' + event.device.id); |
| 35 | log(' RSSI: ' + event.rssi); |
| 36 | log(' TX Power: ' + event.txPower); |
| 37 | log(' UUIDs: ' + event.uuids); |
| 38 | event.manufacturerData.forEach((valueDataView, key) => { |
| 39 | logDataView('Manufacturer', key, valueDataView); |
| 40 | }); |
| 41 | event.serviceData.forEach((valueDataView, key) => { |
| 42 | logDataView('Service', key, valueDataView); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | setTimeout(stopScan, 10000); |
| 47 | function stopScan() { |
| 48 | log('Stopping scan...'); |
| 49 | scan.stop(); |
| 50 | log('Stopped. scan.active = ' + scan.active); |
| 51 | } |
| 52 | } catch(error) { |
| 53 | log('Argh! ' + error); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* Utils */ |
| 58 |
nothing calls this directly
no test coverage detected