-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbau-scanner.js
246 lines (203 loc) · 5.12 KB
/
bau-scanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const template = document.createElement('template');
template.innerHTML = `
<style>
#scanner {
display: block;
position: relative;
box-sizing: border-box;
min-width: 5.14em;
height: auto;
margin: 0;
background: gray;
text-align: center;
}
video {
width: 100%;
height: auto;
}
#info {
position: absolute;
font: monospace;
top: 10px;
left: 10px;
width: calc(100% - 20px);
height: calc(100% - 20px);
background: rgba(255,255,255,0.7);
color: black;
opacity: 0;
transition: opacity 0.5s;
text-align: left;
}
table {
margin: 10px;
}
#info.show {
opacity: 1;
}
</style>
<div id="scanner">
<video id="camera" muted autoplay="autoplay" playsinline="playsinline"></video>
<div id="info"></div>
</div>
`;
const base64ToBytes = (base64) => {
const binString = atob(base64);
return Uint8Array.from(binString, (m) => m.codePointAt(0));
}
const addressTypeName = (value) => {
switch (value) {
case '0': return 'Public';
case '1': return 'Random';
}
}
const BROADCAST_AUDIO_URI_SCHEME = 'BLUETOOTH:';
const parseBroadcastURI = (str) => {
// This function is a PoC, parsing an incomplete list of the possible fields
// listed in the Broadcast Audio URI spec:
// https://www.bluetooth.com/specifications/specs/broadcast-audio-uri-2/
// Check that the string starts with "BLUETOOTH:"
if (!str.startsWith(BROADCAST_AUDIO_URI_SCHEME)) {
return [];
}
const result = [];
// split sections (;)
const sections = str.substring(BROADCAST_AUDIO_URI_SCHEME.length).split(';');
sections.forEach(section => {
const [key, value] = section.split(':');
switch (key) {
case 'AT': // Address type
result.push({
type: key,
name: 'Address type',
value: addressTypeName(value)
});
break;
case 'BC': // Broadcast code
case 'BN': // Broadcast name
result.push({
type: key,
name: key == 'BC' ? 'Broadcast Code' : 'Broadcast Name',
value: new TextDecoder().decode(base64ToBytes(value))
});
break;
case 'AD':
result.push({
type: key,
name: 'Address',
value: value.match(/.{1,2}/g).join(':')
});
break;
case 'UUID':
result.push({
type: key,
name: 'UUID',
value: `0x${value}`
});
break;
case 'BI': // Broadcast ID
result.push({
type: key,
name: 'Broadcast ID',
value: `0x${value.padStart(6,0)}`
});
break;
case 'PI': // PA interval
result.push({
type: key,
name: 'PA interval',
value: `0x${value.padStart(4,0)}`
});
break;
case 'AS': // Advertising SID
case 'NS': // Number of subgroups
result.push({
type: key,
name: key == 'AS' ? 'Advertising SID' : 'No. of subgroups',
value: `0x${value.padStart(2,0)}`
});
break;
}
});
return result;
}
export class BauScanner extends HTMLElement {
#videoStream
#decoderActive
#camera
#barcodeDetector
#lastCode
#info
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(template.content.cloneNode(true));
this.startCamera = this.startCamera.bind(this);
this.stopCamera = this.stopCamera.bind(this);
this.decodeQr = this.decodeQr.bind(this);
this.#lastCode = "";
}
connectedCallback() {
this.#camera = this.shadowRoot?.querySelector('#camera');
this.#info = this.shadowRoot?.querySelector('#info');
this.#barcodeDetector = new window.BarcodeDetector();
}
async startCamera() {
const constraints = {video: true, audio: false};
let stream = await navigator.mediaDevices.getUserMedia(constraints);
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(d => d.kind == 'videoinput');
// Try to find the back camera if using a phone
const backDevice = videoDevices.find(d => d.label.toLowerCase().includes('back'));
if (backDevice) {
constraints.video = {deviceId: backDevice.deviceId};
}
stream = await navigator.mediaDevices.getUserMedia(constraints);
this.#videoStream = stream;
this.#camera.srcObject = stream;
this.#decoderActive = true;
setTimeout(this.decodeQr, 500);
}
stopCamera (){
this.#decoderActive = false;
try {
if (this.#videoStream){
this.#videoStream.getTracks().forEach(t => t.stop());
}
} catch (e){
alert(e.message);
}
}
async decodeQr(){
const barcodes = await this.#barcodeDetector.detect(this.#camera);
if (barcodes?.length) {
for (const barcode of barcodes) {
const decoded = parseBroadcastURI(barcode.rawValue);
if (decoded?.length) {
// Avoid repainting if the data is already shown
if (this.#lastCode == barcode.rawValue) {
break;
}
this.#lastCode = barcode.rawValue;
this.showBroadcastInfo(decoded);
break; // Only use the first code found
}
}
} else {
this.#lastCode = "";
this.hideInfo();
}
if (this.#decoderActive) {
// Try again in 100ms
setTimeout(this.decodeQr, 100);
}
}
showBroadcastInfo(data) {
const dataStrs = data.map(i => `<tr><td>${i.name}: </td><td>${i.value}</td></tr>`);
this.#info.innerHTML = `<table>${dataStrs.join('')}</table>`;
this.#info.classList.add('show');
}
hideInfo() {
this.#info.classList.remove('show');
}
}
customElements.define('bau-scanner', BauScanner);