-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
63 lines (55 loc) · 1.7 KB
/
scripts.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
(function () {
function installSW() {
}
if (navigator.serviceWorker) {
installSW();
}
if (!navigator.bluetooth) {
//show notification
return;
}
window.utils = {
async exponentialBackoff(max, delay, connect) {
if (max > 0) {
try {
return await connect();
} catch (error) {
await new Promise(resolve => setTimeout(resolve, delay * 1000))
delay *= 2;
max--;
return await window.utils.exponentialBackoff(max, delay, connect);
}
}
// window.utils.disableNoSleep();
throw new Error('device unavailable');
},
async connect(device, serviceUuid, characteristicUuid) {
const server = await device.gatt.connect();
const service = await server.getPrimaryService(serviceUuid);
const heartMonitorCharacteristic = await service.getCharacteristic(characteristicUuid);
// window.utils.enableNoSleep();
return heartMonitorCharacteristic;
},
loadScript(url, async) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script');
script.src = url;
if (typeof async !== 'undefined') {
script.async = async;
}
script.onerror = reject;
script.onload = resolve;
document.head.appendChild(script);
});
}
}
// window.utils.loadScript('noSleep.js', true)
// .then(() => {
// const noSleep = new NoSleep();
// window.utils = Object.assign({}, window.utils, {
// enableNoSleep: noSleep.enable.bind(noSleep),
// disableNoSleep: noSleep.disable.bind(noSleep),
// });
// });
window.utils.loadScript('heartMonitor.js', true);
}())