-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbluetooth.js
More file actions
73 lines (68 loc) · 2.12 KB
/
bluetooth.js
File metadata and controls
73 lines (68 loc) · 2.12 KB
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
import {
primaryServiceUuidVolcano1,
primaryServiceUuidVolcano2,
primaryServiceUuidVolcano3,
primaryServiceUuidVolcano4,
primaryServiceUuidVolcano5,
} from "../constants/uuids";
import { buildCacheFromBleDevice } from "../services/BleCharacteristicCache";
const bluetoothConnectFunction = async (onConnected, onDisconnected) => {
const iSiOSdevice =
window.navigator.userAgent.includes("iPhone") ||
window.navigator.userAgent.includes("WebBLE") ||
window.navigator.userAgent.includes("iPad");
if (!navigator.bluetooth) {
const bleNotSupported = `WEB BLE not supported. ${
iSiOSdevice
? 'Download "WebBLE" or "Bluefy" from the app store to use Project Onyx on this device.'
: ""
}`;
alert(bleNotSupported);
throw new Error(bleNotSupported);
}
const filters = [];
const options = {};
const filterNamePrefixVolcano = "S&B";
filters.push({ namePrefix: filterNamePrefixVolcano });
if (iSiOSdevice) {
options.filters = filters;
options.acceptAllDevices = false;
} else {
filters.push({
services: [
primaryServiceUuidVolcano1,
primaryServiceUuidVolcano2,
primaryServiceUuidVolcano3,
primaryServiceUuidVolcano4,
primaryServiceUuidVolcano5,
],
});
options.filters = filters;
options.acceptAllDevices = false;
}
try {
const device = await navigator.bluetooth.requestDevice(options);
if (device.name.includes("S&B VOLCANO")) {
onConnected();
await device.addEventListener("gattserverdisconnected", onDisconnected);
await buildCacheFromBleDevice(device);
}
} catch (error) {
const errorMessage = error.toString();
if (
errorMessage.includes("User cancelled") ||
errorMessage.includes("a user gesture") ||
errorMessage === "2" //The things you do for 3rd party support
) {
return;
}
const alertMessage =
"Bluetooth connection error. Please refresh the page and try again.\n" +
errorMessage +
"\n" +
error.stack;
alert(alertMessage);
throw new Error(alertMessage);
}
};
export default bluetoothConnectFunction;