|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset="UTF-8" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>Bluetooth Device Scanning App</title> |
| 8 | + <style> |
| 9 | + /* CSS to style the background image and text */ |
| 10 | + html, |
| 11 | + body { |
| 12 | + height: 100%; |
| 13 | + margin: 0; |
| 14 | + font-family: Arial, sans-serif; |
| 15 | + background: rgb(16, 9, 34); |
| 16 | + background: linear-gradient(90deg, |
| 17 | + rgba(48, 27, 105, 1) 0%, |
| 18 | + rgba(16, 9, 34, 1) 38%); |
| 19 | + color: white; |
| 20 | + padding: 16px; |
| 21 | + } |
| 22 | + </style> |
| 23 | +</head> |
| 24 | + |
| 25 | +<body> |
| 26 | + <div class="background-img"> |
| 27 | + <h1>Success!</h1> |
| 28 | + <p> |
| 29 | + Scanning for Bluetooth devices... |
| 30 | + Found <span id="bt-devices">0</span> unique devices. |
| 31 | + Found <span id="bt-adv-events">0</span> unique advertising reports. |
| 32 | + </p> |
| 33 | + </div> |
| 34 | + |
| 35 | + <script> |
| 36 | + const bt = require('@brightsign/bt'); |
| 37 | + const btc = new bt(); |
| 38 | + let count = 0; |
| 39 | + let advCount = 0; |
| 40 | + const seenAdvReports = new Set(); |
| 41 | + const seenDevices = new Set(); |
| 42 | + |
| 43 | + /** |
| 44 | + * advertising-report event listener for Bluetooth events. |
| 45 | + * Event contains the following parameters: |
| 46 | + * |
| 47 | + * interface BtCentralAdvertisingReportEvent { |
| 48 | + * attribute String adapter_path; |
| 49 | + * attribute String device_address; |
| 50 | + * attribute int rssi; |
| 51 | + * attribute Array<byte> data; |
| 52 | + * }; |
| 53 | + */ |
| 54 | + btc.addEventListener('advertising-report', (event) => { |
| 55 | + // It is NOT recommended to log the whole event for production scenarios |
| 56 | + // since the logs are too verbose and may block the main thread. |
| 57 | + |
| 58 | + // The following logic only logs parameters of unique events. |
| 59 | + if (event.detail) { |
| 60 | + const eventKey = `${event.detail.adapter_path}-${event.detail.device_address}`; |
| 61 | + if (!seenAdvReports.has(eventKey)) { |
| 62 | + seenAdvReports.add(eventKey); |
| 63 | + const msg = 'adapter_path: ' + event.detail.adapter_path + |
| 64 | + ', device_address: ' + event.detail.device_address + |
| 65 | + ', rssi: ' + event.detail.rssi + |
| 66 | + ', data: ' + event.detail.data; |
| 67 | + |
| 68 | + console.log('New event found: ' + msg); |
| 69 | + advCount++; |
| 70 | + document.getElementById('bt-adv-events').textContent = advCount; |
| 71 | + |
| 72 | + } else { |
| 73 | + // Duplicate event ignored |
| 74 | + } |
| 75 | + } else { |
| 76 | + console.log('advertising-report event detail is invalid:', event); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + /** |
| 81 | + * device-found event listener for Bluetooth devices. |
| 82 | + * Event contains the following parameters: |
| 83 | + * |
| 84 | + * interface BtCentralDeviceFoundEvent { |
| 85 | + * attribute String adapter_path; |
| 86 | + * attribute String address; |
| 87 | + * attribute String alias; |
| 88 | + * [optional] attribute String name; |
| 89 | + * [optional] attribute int rssi; |
| 90 | + * [optional] attribute int tx_power; |
| 91 | + * [optional] attribute Array<String> uuids; |
| 92 | + * [optional] attribute Array<byte> advertising_flags; |
| 93 | + * [optional] attribute Array<Array<int, Array<byte>>> manufacturer_data; |
| 94 | + * [optional] attribute Map<String, Array<byte>> service_data; |
| 95 | + * }; |
| 96 | + */ |
| 97 | + btc.addEventListener('device-found', (event) => { |
| 98 | + // Note that "event" may contain duplicate device information. |
| 99 | + // The following code block ensures that only unique devices are logged. |
| 100 | + if (event.detail) { |
| 101 | + const deviceKey = `${event.detail.adapter_path}-${event.detail.device_address}`; |
| 102 | + if (!seenDevices.has(deviceKey)) { |
| 103 | + seenDevices.add(deviceKey); |
| 104 | + console.log('== new device-found: ' + JSON.stringify(event)); |
| 105 | + count++; |
| 106 | + document.getElementById('bt-devices').textContent = count; |
| 107 | + } else { |
| 108 | + // Duplicate device ignored |
| 109 | + } |
| 110 | + } else { |
| 111 | + console.log('device-found event detail is invalid:', event); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + /** |
| 116 | + * device-lost event listener for Bluetooth devices. |
| 117 | + * Event contains the following parameters: |
| 118 | + * |
| 119 | + * interface BtCentralDeviceLostEvent { |
| 120 | + * attribute String adapter_path; |
| 121 | + * attribute String device_address; |
| 122 | + * }; |
| 123 | + */ |
| 124 | + btc.addEventListener('device-lost', (event) => { |
| 125 | + if (event.detail) { |
| 126 | + const deviceKey = `${event.detail.adapter_path}-${event.detail.device_address}`; |
| 127 | + if (seenDevices.has(deviceKey)) { |
| 128 | + seenDevices.delete(deviceKey); |
| 129 | + console.log('== device-lost: ' + JSON.stringify(event)); |
| 130 | + count--; |
| 131 | + document.getElementById('bt-devices').textContent = count; |
| 132 | + } |
| 133 | + } else { |
| 134 | + console.log('device-lost event detail is invalid:', event); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + btc.scan({ advertisingReports: true }); |
| 139 | + </script> |
| 140 | +</body> |
| 141 | + |
| 142 | +</html> |
0 commit comments