MCPcopy Create free account
hub / github.com/GoogleChrome/samples / onButtonClick

Function onButtonClick

web-bluetooth/scan.js:1–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1async 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

Callers

nothing calls this directly

Calls 2

logDataViewFunction · 0.70
logFunction · 0.50

Tested by

no test coverage detected