forked from pauljt/BleBrowser
-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.js
66 lines (58 loc) · 1.48 KB
/
utils.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
function ab2str(buf) {
"use strict";
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
"use strict";
let buf = new ArrayBuffer(str.length);
let bufView = new Uint8Array(buf);
let i;
let strLen = str.length;
for (i = 0; i < strLen; i += 1) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function setNextAction(action) {
let stepH3 = document.getElementById('next-action');
stepH3.innerHTML = action;
}
function clearNextAction() {
setNextAction('');
}
function setProgressTestAction(action) {
const but = document.getElementById('progress-test');
if (!but) {
throw new Error('No #progress-test button');
}
but.onclick = action;
}
async function progressTest(msg) {
return new Promise(function (resolve) {
setProgressTestAction(() => {
console.log(`User continuing...`);
setNextAction('...');
resolve();
});
setNextAction(msg ? `Hit Progress test for "${msg}"` : 'Hit "Progress test"');
});
}
async function getConnectedDevice(msg, options) {
await progressTest(msg);
if (!options) {
options = {acceptAllDevices: true};
}
setNextAction(msg);
const device = await navigator.bluetooth.requestDevice(options);
setNextAction('...');
await device.gatt.connect();
return device;
}
async function getConnectedPuck(msg) {
return await getConnectedDevice(msg, {
filters: [{
namePrefix: 'Puck.js',
services: [NORDIC_SERVICE]
}]
});
}