@neurosity/sdk 6.2.1-next.2 → 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/neurosity.iife.js +48717 -0
- package/dist/browser/neurosity.js +666 -0
- package/dist/browser/neurosity.js.map +1 -0
- package/dist/{Neurosity.d.ts → cjs/Neurosity.d.ts} +2 -116
- package/dist/cjs/Neurosity.js +1244 -0
- package/dist/{api → cjs/api}/bluetooth/BluetoothClient.d.ts +6 -6
- package/dist/cjs/api/bluetooth/BluetoothClient.js +310 -0
- package/dist/cjs/api/bluetooth/BluetoothTransport.d.ts +33 -0
- package/dist/cjs/api/bluetooth/BluetoothTransport.js +2 -0
- package/dist/{api → cjs/api}/bluetooth/constants.d.ts +0 -0
- package/dist/cjs/api/bluetooth/constants.js +9 -0
- package/dist/{api → cjs/api}/bluetooth/index.d.ts +0 -0
- package/dist/cjs/api/bluetooth/index.js +21 -0
- package/dist/{api → cjs/api}/bluetooth/react-native/ReactNativeTransport.d.ts +4 -4
- package/dist/cjs/api/bluetooth/react-native/ReactNativeTransport.js +411 -0
- package/dist/cjs/api/bluetooth/react-native/types/BleManagerTypes.d.ts +63 -0
- package/dist/cjs/api/bluetooth/react-native/types/BleManagerTypes.js +11 -0
- package/dist/cjs/api/bluetooth/react-native/types/ReactNativeTypes.d.ts +180 -0
- package/dist/cjs/api/bluetooth/react-native/types/ReactNativeTypes.js +10 -0
- package/dist/{api → cjs/api}/bluetooth/types/index.d.ts +2 -2
- package/dist/cjs/api/bluetooth/types/index.js +22 -0
- package/dist/{api → cjs/api}/bluetooth/utils/create6DigitPin.d.ts +0 -0
- package/dist/cjs/api/bluetooth/utils/create6DigitPin.js +8 -0
- package/dist/{api → cjs/api}/bluetooth/utils/csvBufferToEpoch.d.ts +0 -0
- package/dist/cjs/api/bluetooth/utils/csvBufferToEpoch.js +36 -0
- package/dist/{api → cjs/api}/bluetooth/utils/encoding.d.ts +0 -0
- package/dist/cjs/api/bluetooth/utils/encoding.js +22 -0
- package/dist/{api → cjs/api}/bluetooth/utils/osHasBluetoothSupport.d.ts +0 -0
- package/dist/cjs/api/bluetooth/utils/osHasBluetoothSupport.js +23 -0
- package/dist/{api → cjs/api}/bluetooth/utils/stitch.d.ts +1 -1
- package/dist/cjs/api/bluetooth/utils/stitch.js +22 -0
- package/dist/{api → cjs/api}/bluetooth/web/WebBluetoothTransport.d.ts +1 -1
- package/dist/cjs/api/bluetooth/web/WebBluetoothTransport.js +430 -0
- package/dist/{api → cjs/api}/bluetooth/web/isMaybeWebWorkerContext.d.ts +0 -0
- package/dist/cjs/api/bluetooth/web/isMaybeWebWorkerContext.js +8 -0
- package/dist/{api → cjs/api}/bluetooth/web/isWebBluetoothSupported.d.ts +0 -0
- package/dist/cjs/api/bluetooth/web/isWebBluetoothSupported.js +11 -0
- package/dist/{api → cjs/api}/firebase/FirebaseApp.d.ts +0 -0
- package/dist/cjs/api/firebase/FirebaseApp.js +70 -0
- package/dist/{api → cjs/api}/firebase/FirebaseDevice.d.ts +1 -1
- package/dist/cjs/api/firebase/FirebaseDevice.js +146 -0
- package/dist/{api → cjs/api}/firebase/FirebaseUser.d.ts +0 -0
- package/dist/cjs/api/firebase/FirebaseUser.js +434 -0
- package/dist/{api → cjs/api}/firebase/config.d.ts +0 -0
- package/dist/cjs/api/firebase/config.js +11 -0
- package/dist/{api → cjs/api}/firebase/deviceStore.d.ts +0 -0
- package/dist/cjs/api/firebase/deviceStore.js +191 -0
- package/dist/{api → cjs/api}/firebase/index.d.ts +0 -0
- package/dist/cjs/api/firebase/index.js +19 -0
- package/dist/{api → cjs/api}/https/config.d.ts +0 -0
- package/dist/cjs/api/https/config.js +4 -0
- package/dist/{api → cjs/api}/https/createOAuthURL.d.ts +0 -0
- package/dist/cjs/api/https/createOAuthURL.js +18 -0
- package/dist/{api → cjs/api}/https/getOAuthToken.d.ts +0 -0
- package/dist/cjs/api/https/getOAuthToken.js +34 -0
- package/dist/{api → cjs/api}/https/utils.d.ts +0 -0
- package/dist/cjs/api/https/utils.js +13 -0
- package/dist/{api → cjs/api}/index.d.ts +0 -8
- package/dist/cjs/api/index.js +307 -0
- package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- package/dist/cjs/index.js +18 -0
- package/dist/{subscriptions → cjs/subscriptions}/SubscriptionManager.d.ts +0 -0
- package/dist/cjs/subscriptions/SubscriptionManager.js +27 -0
- package/dist/{timesync → cjs/timesync}/Timesync.d.ts +1 -1
- package/dist/cjs/timesync/Timesync.js +78 -0
- package/dist/{timesync → cjs/timesync}/index.d.ts +0 -0
- package/dist/cjs/timesync/index.js +17 -0
- package/dist/cjs/types/accelerometer.d.ts +10 -0
- package/dist/cjs/types/accelerometer.js +2 -0
- package/dist/cjs/types/actions.d.ts +16 -0
- package/dist/cjs/types/actions.js +2 -0
- package/dist/cjs/types/awareness.d.ts +1 -0
- package/dist/cjs/types/awareness.js +2 -0
- package/dist/cjs/types/brainwaves.d.ts +26 -0
- package/dist/cjs/types/brainwaves.js +2 -0
- package/dist/cjs/types/calm.d.ts +6 -0
- package/dist/cjs/types/calm.js +2 -0
- package/dist/cjs/types/client.d.ts +22 -0
- package/dist/cjs/types/client.js +2 -0
- package/dist/cjs/types/credentials.d.ts +12 -0
- package/dist/cjs/types/credentials.js +2 -0
- package/dist/cjs/types/deviceInfo.d.ts +18 -0
- package/dist/cjs/types/deviceInfo.js +2 -0
- package/dist/cjs/types/epoch.d.ts +12 -0
- package/dist/cjs/types/epoch.js +2 -0
- package/dist/cjs/types/experiment.d.ts +9 -0
- package/dist/cjs/types/experiment.js +2 -0
- package/dist/cjs/types/focus.d.ts +6 -0
- package/dist/cjs/types/focus.js +2 -0
- package/dist/cjs/types/hapticEffects.d.ts +125 -0
- package/dist/cjs/types/hapticEffects.js +2 -0
- package/dist/cjs/types/kinesis.d.ts +6 -0
- package/dist/cjs/types/kinesis.js +2 -0
- package/dist/cjs/types/marker.d.ts +7 -0
- package/dist/cjs/types/marker.js +2 -0
- package/dist/cjs/types/metrics.d.ts +21 -0
- package/dist/cjs/types/metrics.js +2 -0
- package/dist/cjs/types/oauth.d.ts +20 -0
- package/dist/cjs/types/oauth.js +2 -0
- package/dist/cjs/types/options.d.ts +51 -0
- package/dist/cjs/types/options.js +2 -0
- package/dist/cjs/types/sample.d.ts +15 -0
- package/dist/cjs/types/sample.js +2 -0
- package/dist/cjs/types/settings.d.ts +13 -0
- package/dist/cjs/types/settings.js +2 -0
- package/dist/cjs/types/signalQuality.d.ts +13 -0
- package/dist/cjs/types/signalQuality.js +2 -0
- package/dist/cjs/types/skill.d.ts +68 -0
- package/dist/cjs/types/skill.js +2 -0
- package/dist/{types → cjs/types}/status.d.ts +0 -0
- package/dist/cjs/types/status.js +22 -0
- package/dist/{types → cjs/types}/streaming.d.ts +0 -0
- package/dist/cjs/types/streaming.js +20 -0
- package/dist/cjs/types/subscriptions.d.ts +23 -0
- package/dist/cjs/types/subscriptions.js +2 -0
- package/dist/cjs/types/training.d.ts +19 -0
- package/dist/cjs/types/training.js +2 -0
- package/dist/cjs/types/user.d.ts +12 -0
- package/dist/cjs/types/user.js +2 -0
- package/dist/{utils → cjs/utils}/errors.d.ts +0 -0
- package/dist/cjs/utils/errors.js +17 -0
- package/dist/{utils → cjs/utils}/filterInternalKeys.d.ts +0 -0
- package/dist/cjs/utils/filterInternalKeys.js +21 -0
- package/dist/{utils → cjs/utils}/hapticEffects.d.ts +0 -0
- package/dist/cjs/utils/hapticEffects.js +130 -0
- package/dist/{utils → cjs/utils}/heartbeat.d.ts +0 -0
- package/dist/cjs/utils/heartbeat.js +41 -0
- package/dist/{utils → cjs/utils}/is-node.d.ts +0 -0
- package/dist/cjs/utils/is-node.js +9 -0
- package/dist/cjs/utils/metrics.d.ts +1 -0
- package/dist/cjs/utils/metrics.js +56 -0
- package/dist/{utils → cjs/utils}/oauth.d.ts +1 -1
- package/dist/cjs/utils/oauth.js +92 -0
- package/dist/cjs/utils/pick.d.ts +1 -0
- package/dist/cjs/utils/pick.js +5 -0
- package/dist/{utils → cjs/utils}/pipes.d.ts +0 -0
- package/dist/cjs/utils/pipes.js +71 -0
- package/dist/{utils → cjs/utils}/platform.d.ts +0 -0
- package/dist/cjs/utils/platform.js +47 -0
- package/dist/{utils → cjs/utils}/subscription.d.ts +0 -0
- package/dist/cjs/utils/subscription.js +55 -0
- package/dist/cjs/utils/transferDevice.d.ts +9 -0
- package/dist/cjs/utils/transferDevice.js +2 -0
- package/dist/{utils → cjs/utils}/whileOnline.d.ts +1 -1
- package/dist/cjs/utils/whileOnline.js +15 -0
- package/dist/electron/index.js +86 -0
- package/dist/electron/index.js.map +1 -0
- package/dist/esm/Neurosity.d.ts +619 -0
- package/dist/esm/Neurosity.js +1213 -0
- package/dist/esm/api/bluetooth/BluetoothClient.d.ts +72 -0
- package/dist/esm/api/bluetooth/BluetoothClient.js +306 -0
- package/dist/esm/api/bluetooth/BluetoothTransport.d.ts +33 -0
- package/dist/esm/api/bluetooth/BluetoothTransport.js +1 -0
- package/dist/esm/api/bluetooth/constants.d.ts +4 -0
- package/dist/esm/api/bluetooth/constants.js +6 -0
- package/dist/esm/api/bluetooth/index.d.ts +5 -0
- package/dist/esm/api/bluetooth/index.js +5 -0
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.d.ts +70 -0
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.js +407 -0
- package/dist/esm/api/bluetooth/react-native/types/BleManagerTypes.d.ts +63 -0
- package/dist/esm/api/bluetooth/react-native/types/BleManagerTypes.js +8 -0
- package/dist/esm/api/bluetooth/react-native/types/ReactNativeTypes.d.ts +180 -0
- package/dist/esm/api/bluetooth/react-native/types/ReactNativeTypes.js +9 -0
- package/dist/esm/api/bluetooth/types/index.d.ts +31 -0
- package/dist/esm/api/bluetooth/types/index.js +19 -0
- package/dist/esm/api/bluetooth/utils/create6DigitPin.d.ts +1 -0
- package/dist/esm/api/bluetooth/utils/create6DigitPin.js +4 -0
- package/dist/esm/api/bluetooth/utils/csvBufferToEpoch.d.ts +11 -0
- package/dist/esm/api/bluetooth/utils/csvBufferToEpoch.js +31 -0
- package/dist/esm/api/bluetooth/utils/encoding.d.ts +3 -0
- package/dist/esm/api/bluetooth/utils/encoding.js +17 -0
- package/dist/esm/api/bluetooth/utils/osHasBluetoothSupport.d.ts +2 -0
- package/dist/esm/api/bluetooth/utils/osHasBluetoothSupport.js +16 -0
- package/dist/esm/api/bluetooth/utils/stitch.d.ts +5 -0
- package/dist/esm/api/bluetooth/utils/stitch.js +18 -0
- package/dist/esm/api/bluetooth/web/WebBluetoothTransport.d.ts +54 -0
- package/dist/esm/api/bluetooth/web/WebBluetoothTransport.js +426 -0
- package/dist/esm/api/bluetooth/web/isMaybeWebWorkerContext.d.ts +1 -0
- package/dist/esm/api/bluetooth/web/isMaybeWebWorkerContext.js +4 -0
- package/dist/esm/api/bluetooth/web/isWebBluetoothSupported.d.ts +1 -0
- package/dist/esm/api/bluetooth/web/isWebBluetoothSupported.js +7 -0
- package/dist/esm/api/firebase/FirebaseApp.d.ts +20 -0
- package/dist/esm/api/firebase/FirebaseApp.js +63 -0
- package/dist/esm/api/firebase/FirebaseDevice.d.ts +68 -0
- package/dist/esm/api/firebase/FirebaseDevice.js +139 -0
- package/dist/esm/api/firebase/FirebaseUser.d.ts +53 -0
- package/dist/esm/api/firebase/FirebaseUser.js +426 -0
- package/dist/esm/api/firebase/config.d.ts +8 -0
- package/dist/esm/api/firebase/config.js +8 -0
- package/dist/esm/api/firebase/deviceStore.d.ts +26 -0
- package/dist/esm/api/firebase/deviceStore.js +184 -0
- package/dist/esm/api/firebase/index.d.ts +3 -0
- package/dist/esm/api/firebase/index.js +3 -0
- package/dist/esm/api/https/config.d.ts +1 -0
- package/dist/esm/api/https/config.js +1 -0
- package/dist/esm/api/https/createOAuthURL.d.ts +3 -0
- package/dist/esm/api/https/createOAuthURL.js +11 -0
- package/dist/esm/api/https/getOAuthToken.d.ts +3 -0
- package/dist/esm/api/https/getOAuthToken.js +27 -0
- package/dist/esm/api/https/utils.d.ts +2 -0
- package/dist/esm/api/https/utils.js +9 -0
- package/dist/esm/api/index.d.ts +109 -0
- package/dist/esm/api/index.js +300 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/neurosity.mjs +48705 -0
- package/dist/esm/subscriptions/SubscriptionManager.d.ts +11 -0
- package/dist/esm/subscriptions/SubscriptionManager.js +23 -0
- package/dist/esm/timesync/Timesync.d.ts +20 -0
- package/dist/esm/timesync/Timesync.js +71 -0
- package/dist/esm/timesync/index.d.ts +1 -0
- package/dist/esm/timesync/index.js +1 -0
- package/dist/esm/types/accelerometer.d.ts +10 -0
- package/dist/esm/types/accelerometer.js +1 -0
- package/dist/esm/types/actions.d.ts +16 -0
- package/dist/esm/types/actions.js +1 -0
- package/dist/esm/types/awareness.d.ts +1 -0
- package/dist/esm/types/awareness.js +1 -0
- package/dist/esm/types/brainwaves.d.ts +26 -0
- package/dist/esm/types/brainwaves.js +1 -0
- package/dist/esm/types/calm.d.ts +6 -0
- package/dist/esm/types/calm.js +1 -0
- package/dist/esm/types/client.d.ts +22 -0
- package/dist/esm/types/client.js +1 -0
- package/dist/esm/types/credentials.d.ts +12 -0
- package/dist/esm/types/credentials.js +1 -0
- package/dist/esm/types/deviceInfo.d.ts +18 -0
- package/dist/esm/types/deviceInfo.js +1 -0
- package/dist/esm/types/epoch.d.ts +12 -0
- package/dist/esm/types/epoch.js +1 -0
- package/dist/esm/types/experiment.d.ts +9 -0
- package/dist/esm/types/experiment.js +1 -0
- package/dist/esm/types/focus.d.ts +6 -0
- package/dist/esm/types/focus.js +1 -0
- package/dist/esm/types/hapticEffects.d.ts +125 -0
- package/dist/esm/types/hapticEffects.js +1 -0
- package/dist/esm/types/kinesis.d.ts +6 -0
- package/dist/esm/types/kinesis.js +1 -0
- package/dist/esm/types/marker.d.ts +7 -0
- package/dist/esm/types/marker.js +1 -0
- package/dist/esm/types/metrics.d.ts +21 -0
- package/dist/esm/types/metrics.js +1 -0
- package/dist/esm/types/oauth.d.ts +20 -0
- package/dist/esm/types/oauth.js +1 -0
- package/dist/esm/types/options.d.ts +51 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/sample.d.ts +15 -0
- package/dist/esm/types/sample.js +1 -0
- package/dist/esm/types/settings.d.ts +13 -0
- package/dist/esm/types/settings.js +1 -0
- package/dist/esm/types/signalQuality.d.ts +13 -0
- package/dist/esm/types/signalQuality.js +1 -0
- package/dist/esm/types/skill.d.ts +68 -0
- package/dist/esm/types/skill.js +1 -0
- package/dist/esm/types/status.d.ts +26 -0
- package/dist/esm/types/status.js +19 -0
- package/dist/esm/types/streaming.d.ts +15 -0
- package/dist/esm/types/streaming.js +17 -0
- package/dist/esm/types/subscriptions.d.ts +23 -0
- package/dist/esm/types/subscriptions.js +1 -0
- package/dist/esm/types/training.d.ts +19 -0
- package/dist/esm/types/training.js +1 -0
- package/dist/esm/types/user.d.ts +12 -0
- package/dist/esm/types/user.js +1 -0
- package/dist/esm/utils/errors.d.ts +5 -0
- package/dist/esm/utils/errors.js +11 -0
- package/dist/esm/utils/filterInternalKeys.d.ts +3 -0
- package/dist/esm/utils/filterInternalKeys.js +17 -0
- package/dist/esm/utils/hapticEffects.d.ts +123 -0
- package/dist/esm/utils/hapticEffects.js +125 -0
- package/dist/esm/utils/heartbeat.d.ts +4 -0
- package/dist/esm/utils/heartbeat.js +33 -0
- package/dist/esm/utils/is-node.d.ts +1 -0
- package/dist/esm/utils/is-node.js +5 -0
- package/dist/esm/utils/metrics.d.ts +1 -0
- package/dist/esm/utils/metrics.js +52 -0
- package/dist/esm/utils/oauth.d.ts +9 -0
- package/dist/esm/utils/oauth.js +64 -0
- package/dist/esm/utils/pick.d.ts +1 -0
- package/dist/esm/utils/pick.js +1 -0
- package/dist/esm/utils/pipes.d.ts +46 -0
- package/dist/esm/utils/pipes.js +65 -0
- package/dist/esm/utils/platform.d.ts +30 -0
- package/dist/esm/utils/platform.js +41 -0
- package/dist/esm/utils/subscription.d.ts +6 -0
- package/dist/esm/utils/subscription.js +24 -0
- package/dist/esm/utils/transferDevice.d.ts +9 -0
- package/dist/esm/utils/transferDevice.js +1 -0
- package/dist/esm/utils/whileOnline.d.ts +8 -0
- package/dist/esm/utils/whileOnline.js +11 -0
- package/dist/examples/auth.html +32 -0
- package/dist/examples/cloud.html +17 -0
- package/dist/examples/index.html +1 -0
- package/dist/examples/neurosity.iife.js +48717 -0
- package/dist/examples/neurosity.js +666 -0
- package/dist/examples/neurosity.mjs +48705 -0
- package/dist/examples/training.html +49 -0
- package/package.json +23 -18
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.m.js +0 -2
- package/dist/index.m.js.map +0 -1
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.map +0 -1
- package/dist/utils/metrics.d.ts +0 -6
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { BLUETOOTH_PRIMARY_SERVICE_UUID_STRING } from "@neurosity/ipk";
|
|
11
|
+
import { BLUETOOTH_CHUNK_DELIMITER } from "@neurosity/ipk";
|
|
12
|
+
import { BLUETOOTH_DEVICE_NAME_PREFIXES } from "@neurosity/ipk";
|
|
13
|
+
import { BehaviorSubject, defer, merge, of, ReplaySubject, timer } from "rxjs";
|
|
14
|
+
import { fromEventPattern, Observable, NEVER, EMPTY } from "rxjs";
|
|
15
|
+
import { switchMap, map, filter, takeUntil, tap } from "rxjs/operators";
|
|
16
|
+
import { shareReplay, distinctUntilChanged, finalize } from "rxjs/operators";
|
|
17
|
+
import { take, share, scan, distinct } from "rxjs/operators";
|
|
18
|
+
import { create6DigitPin } from "../utils/create6DigitPin";
|
|
19
|
+
import { stitchChunks } from "../utils/stitch";
|
|
20
|
+
import { encode, decode } from "../utils/encoding";
|
|
21
|
+
import { TRANSPORT_TYPE, BLUETOOTH_CONNECTION } from "../types";
|
|
22
|
+
import { DEFAULT_ACTION_RESPONSE_TIMEOUT } from "../constants";
|
|
23
|
+
import { CHARACTERISTIC_UUIDS_TO_NAMES } from "../constants";
|
|
24
|
+
import { ANDROID_MAX_MTU } from "../constants";
|
|
25
|
+
import { REACT_NATIVE_MAX_BYTE_SIZE } from "../constants";
|
|
26
|
+
import { osHasBluetoothSupport } from "../utils/osHasBluetoothSupport";
|
|
27
|
+
const defaultOptions = {
|
|
28
|
+
autoConnect: true
|
|
29
|
+
};
|
|
30
|
+
export class ReactNativeTransport {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.type = TRANSPORT_TYPE.REACT_NATIVE;
|
|
33
|
+
this.characteristicsByName = {};
|
|
34
|
+
this.connection$ = new BehaviorSubject(BLUETOOTH_CONNECTION.DISCONNECTED);
|
|
35
|
+
this.pendingActions$ = new BehaviorSubject([]);
|
|
36
|
+
this.logs$ = new ReplaySubject(10);
|
|
37
|
+
this.connectionStream$ = this.connection$
|
|
38
|
+
.asObservable()
|
|
39
|
+
.pipe(filter((connection) => !!connection), distinctUntilChanged(), shareReplay(1));
|
|
40
|
+
this._isAutoConnectEnabled$ = new ReplaySubject(1);
|
|
41
|
+
if (!options) {
|
|
42
|
+
const errorMessage = "React Native transport: missing options.";
|
|
43
|
+
this.addLog(errorMessage);
|
|
44
|
+
throw new Error(errorMessage);
|
|
45
|
+
}
|
|
46
|
+
this.options = Object.assign(Object.assign({}, defaultOptions), options);
|
|
47
|
+
const { BleManager, bleManagerEmitter, platform, autoConnect } = this.options;
|
|
48
|
+
if (!BleManager) {
|
|
49
|
+
const errorMessage = "React Native option: BleManager not provided.";
|
|
50
|
+
this.addLog(errorMessage);
|
|
51
|
+
throw new Error(errorMessage);
|
|
52
|
+
}
|
|
53
|
+
if (!bleManagerEmitter) {
|
|
54
|
+
const errorMessage = "React Native option: bleManagerEmitter not provided.";
|
|
55
|
+
this.addLog(errorMessage);
|
|
56
|
+
throw new Error(errorMessage);
|
|
57
|
+
}
|
|
58
|
+
if (!platform) {
|
|
59
|
+
const errorMessage = "React Native option: platform not provided.";
|
|
60
|
+
this.addLog(errorMessage);
|
|
61
|
+
throw new Error(errorMessage);
|
|
62
|
+
}
|
|
63
|
+
this.BleManager = BleManager;
|
|
64
|
+
this.bleManagerEmitter = bleManagerEmitter;
|
|
65
|
+
this.platform = platform;
|
|
66
|
+
this._isAutoConnectEnabled$.next(autoConnect);
|
|
67
|
+
this._isAutoConnectEnabled$.subscribe((autoConnect) => {
|
|
68
|
+
this.addLog(`Auto connect: ${autoConnect ? "enabled" : "disabled"}`);
|
|
69
|
+
});
|
|
70
|
+
// We create a single listener per event type to
|
|
71
|
+
// avoid missing events when multiple listeners are attached.
|
|
72
|
+
this.bleEvents = {
|
|
73
|
+
stopScan$: this._fromEvent("BleManagerStopScan"),
|
|
74
|
+
discoverPeripheral$: this._fromEvent("BleManagerDiscoverPeripheral"),
|
|
75
|
+
connectPeripheral$: this._fromEvent("BleManagerConnectPeripheral"),
|
|
76
|
+
disconnectPeripheral$: this._fromEvent("BleManagerDisconnectPeripheral"),
|
|
77
|
+
didUpdateValueForCharacteristic$: this._fromEvent("BleManagerDidUpdateValueForCharacteristic"),
|
|
78
|
+
didUpdateState$: this._fromEvent("BleManagerDidUpdateState")
|
|
79
|
+
};
|
|
80
|
+
this.onDisconnected$ = this.bleEvents.disconnectPeripheral$.pipe(share());
|
|
81
|
+
// Initializes the module. This can only be called once.
|
|
82
|
+
this.BleManager.start({ showAlert: false })
|
|
83
|
+
.then(() => {
|
|
84
|
+
this.addLog(`BleManger started`);
|
|
85
|
+
})
|
|
86
|
+
.catch((error) => {
|
|
87
|
+
var _a;
|
|
88
|
+
this.addLog(`BleManger failed to start. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
89
|
+
});
|
|
90
|
+
this.connection$.asObservable().subscribe((connection) => {
|
|
91
|
+
this.addLog(`connection status is ${connection}`);
|
|
92
|
+
});
|
|
93
|
+
this.onDisconnected$.subscribe(() => {
|
|
94
|
+
this.connection$.next(BLUETOOTH_CONNECTION.DISCONNECTED);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
addLog(log) {
|
|
98
|
+
this.logs$.next(log);
|
|
99
|
+
}
|
|
100
|
+
isConnected() {
|
|
101
|
+
const connection = this.connection$.getValue();
|
|
102
|
+
return connection === BLUETOOTH_CONNECTION.CONNECTED;
|
|
103
|
+
}
|
|
104
|
+
_autoConnect(selectedDevice$) {
|
|
105
|
+
const selectedDeviceAfterDisconnect$ = this.onDisconnected$.pipe(switchMap(() => selectedDevice$));
|
|
106
|
+
return this._isAutoConnectEnabled$.pipe(switchMap((isAutoConnectEnabled) => isAutoConnectEnabled
|
|
107
|
+
? merge(selectedDevice$, selectedDeviceAfterDisconnect$)
|
|
108
|
+
: NEVER), switchMap((selectedDevice) => !osHasBluetoothSupport(selectedDevice)
|
|
109
|
+
? NEVER
|
|
110
|
+
: this.scan().pipe(switchMap((peripherals) => {
|
|
111
|
+
const peripheralMatch = peripherals.find((peripheral) => peripheral.name === (selectedDevice === null || selectedDevice === void 0 ? void 0 : selectedDevice.deviceNickname));
|
|
112
|
+
return peripheralMatch ? of(peripheralMatch) : NEVER;
|
|
113
|
+
}), distinct((peripheral) => peripheral.id), take(1))), switchMap((peripheral) => __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
return yield this.connect(peripheral);
|
|
115
|
+
})));
|
|
116
|
+
}
|
|
117
|
+
enableAutoConnect(autoConnect) {
|
|
118
|
+
this._isAutoConnectEnabled$.next(autoConnect);
|
|
119
|
+
}
|
|
120
|
+
connection() {
|
|
121
|
+
return this.connectionStream$;
|
|
122
|
+
}
|
|
123
|
+
_fromEvent(eventName) {
|
|
124
|
+
return fromEventPattern((addHandler) => {
|
|
125
|
+
this.bleManagerEmitter.addListener(eventName, addHandler);
|
|
126
|
+
}, () => {
|
|
127
|
+
this.bleManagerEmitter.removeAllListeners(eventName);
|
|
128
|
+
}).pipe(
|
|
129
|
+
// @important: we need to share the subscription
|
|
130
|
+
// to avoid missing events
|
|
131
|
+
share());
|
|
132
|
+
}
|
|
133
|
+
scan(options) {
|
|
134
|
+
var _a, _b;
|
|
135
|
+
const RESCAN_INTERVAL = 10000; // 10 seconds
|
|
136
|
+
const seconds = (_a = options === null || options === void 0 ? void 0 : options.seconds) !== null && _a !== void 0 ? _a : RESCAN_INTERVAL / 1000;
|
|
137
|
+
const once = (_b = options === null || options === void 0 ? void 0 : options.once) !== null && _b !== void 0 ? _b : false;
|
|
138
|
+
const serviceUUIDs = [BLUETOOTH_PRIMARY_SERVICE_UUID_STRING];
|
|
139
|
+
const allowDuplicates = true;
|
|
140
|
+
const scanOptions = {};
|
|
141
|
+
const scanOnce$ = new Observable((subscriber) => {
|
|
142
|
+
var _a;
|
|
143
|
+
try {
|
|
144
|
+
this.BleManager.scan(serviceUUIDs, seconds, allowDuplicates, scanOptions).then(() => {
|
|
145
|
+
this.addLog(`BleManger scanning ${once ? "once" : "indefintely"}`);
|
|
146
|
+
subscriber.next();
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
this.addLog(`BleManger scanning ${once ? "once" : "indefintely"} failed. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
151
|
+
subscriber.error(error);
|
|
152
|
+
}
|
|
153
|
+
return () => {
|
|
154
|
+
this.BleManager.stopScan();
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
const scan$ = once
|
|
158
|
+
? scanOnce$
|
|
159
|
+
: timer(0, RESCAN_INTERVAL).pipe(switchMap(() => scanOnce$));
|
|
160
|
+
const peripherals$ = scan$.pipe(tap(() => {
|
|
161
|
+
this.connection$.next(BLUETOOTH_CONNECTION.SCANNING);
|
|
162
|
+
}), takeUntil(this.onDisconnected$), switchMap(() => this.bleEvents.discoverPeripheral$),
|
|
163
|
+
// Filter out devices that are not Neurosity devices
|
|
164
|
+
filter((peripheral) => {
|
|
165
|
+
var _a, _b, _c;
|
|
166
|
+
const peripheralName = (_c = (_b = (_a = peripheral === null || peripheral === void 0 ? void 0 : peripheral.advertising) === null || _a === void 0 ? void 0 : _a.localName) !== null && _b !== void 0 ? _b : peripheral.name) !== null && _c !== void 0 ? _c : "";
|
|
167
|
+
if (!peripheralName) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
const startsWithPrefix = BLUETOOTH_DEVICE_NAME_PREFIXES.findIndex((prefix) => peripheralName.startsWith(prefix)) !== -1;
|
|
171
|
+
return startsWithPrefix;
|
|
172
|
+
}), scan((acc, peripheral) => {
|
|
173
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
174
|
+
// normalized peripheral name for backwards compatibility
|
|
175
|
+
// Neurosity OS v15 doesn't have peripheral.name as deviceNickname
|
|
176
|
+
// it only has peripheral.advertising.localName as deviceNickname
|
|
177
|
+
// and OS v16 has both as deviceNickname
|
|
178
|
+
const peripheralName = (_c = (_b = (_a = peripheral === null || peripheral === void 0 ? void 0 : peripheral.advertising) === null || _a === void 0 ? void 0 : _a.localName) !== null && _b !== void 0 ? _b : peripheral.name) !== null && _c !== void 0 ? _c : "";
|
|
179
|
+
const manufactureDataString = (_h = (_g = decode(this.type, (_f = (_e = (_d = peripheral === null || peripheral === void 0 ? void 0 : peripheral.advertising) === null || _d === void 0 ? void 0 : _d.manufacturerData) === null || _e === void 0 ? void 0 : _e.bytes) !== null && _f !== void 0 ? _f : [])) === null || _g === void 0 ? void 0 : _g.slice) === null || _h === void 0 ? void 0 : _h.call(_g, 2); // First 2 bytes are reserved for the Neurosity company code
|
|
180
|
+
return Object.assign(Object.assign({}, acc), { [peripheral.id]: Object.assign(Object.assign({}, peripheral), { name: peripheralName, manufactureDataString }) });
|
|
181
|
+
}, {}), distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)), map((peripheralMap) => Object.values(peripheralMap)), share());
|
|
182
|
+
return peripherals$;
|
|
183
|
+
}
|
|
184
|
+
connect(peripheral) {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
187
|
+
try {
|
|
188
|
+
if (!peripheral) {
|
|
189
|
+
this.addLog("Peripheral not found");
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
this.connection$.next(BLUETOOTH_CONNECTION.CONNECTING);
|
|
193
|
+
yield this.BleManager.connect(peripheral.id);
|
|
194
|
+
this.addLog(`Getting service...`);
|
|
195
|
+
const peripheralInfo = yield this.BleManager.retrieveServices(peripheral.id, [
|
|
196
|
+
BLUETOOTH_PRIMARY_SERVICE_UUID_STRING
|
|
197
|
+
]);
|
|
198
|
+
if (!peripheralInfo) {
|
|
199
|
+
this.addLog("Could not retreive services");
|
|
200
|
+
reject(new Error(`Could not retreive services`));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this.addLog(`Got service ${BLUETOOTH_PRIMARY_SERVICE_UUID_STRING}, getting characteristics...`);
|
|
204
|
+
this.device = peripheral;
|
|
205
|
+
this.characteristicsByName = Object.fromEntries(peripheralInfo.characteristics.map((characteristic) => [
|
|
206
|
+
CHARACTERISTIC_UUIDS_TO_NAMES[characteristic.characteristic.toLowerCase() // react native uses uppercase
|
|
207
|
+
],
|
|
208
|
+
{
|
|
209
|
+
characteristicUUID: characteristic.characteristic,
|
|
210
|
+
serviceUUID: characteristic.service,
|
|
211
|
+
peripheralId: peripheral.id
|
|
212
|
+
}
|
|
213
|
+
]));
|
|
214
|
+
this.addLog(`Got characteristics.`);
|
|
215
|
+
if (this.platform === "android") {
|
|
216
|
+
yield this.BleManager.requestMTU(peripheral.id, ANDROID_MAX_MTU)
|
|
217
|
+
.then((updatedMTU) => {
|
|
218
|
+
this.addLog(`Successfully updated Android MTU to ${updatedMTU} bytes. Requested MTU: ${ANDROID_MAX_MTU} bytes.`);
|
|
219
|
+
})
|
|
220
|
+
.catch((error) => {
|
|
221
|
+
this.addLog(`Failed to set Android MTU of ${ANDROID_MAX_MTU} bytes. Error: ${error}`);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
this.addLog(`Successfully connected to peripheral ${peripheral.id}`);
|
|
225
|
+
this.connection$.next(BLUETOOTH_CONNECTION.CONNECTED);
|
|
226
|
+
resolve();
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
reject(error);
|
|
230
|
+
}
|
|
231
|
+
}));
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
disconnect() {
|
|
235
|
+
var _a;
|
|
236
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
237
|
+
try {
|
|
238
|
+
if (this.isConnected() && ((_a = this === null || this === void 0 ? void 0 : this.device) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
239
|
+
yield this.BleManager.disconnect(this.device.id);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
return Promise.reject(error);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
getCharacteristicByName(characteristicName) {
|
|
248
|
+
var _a;
|
|
249
|
+
if (!(characteristicName in this.characteristicsByName)) {
|
|
250
|
+
throw new Error(`Characteristic by name ${characteristicName} is not found`);
|
|
251
|
+
}
|
|
252
|
+
return (_a = this.characteristicsByName) === null || _a === void 0 ? void 0 : _a[characteristicName];
|
|
253
|
+
}
|
|
254
|
+
subscribeToCharacteristic({ characteristicName, manageNotifications = true }) {
|
|
255
|
+
const getData = ({ peripheralId, serviceUUID, characteristicUUID }) => defer(() => __awaiter(this, void 0, void 0, function* () {
|
|
256
|
+
var _a;
|
|
257
|
+
if (manageNotifications) {
|
|
258
|
+
try {
|
|
259
|
+
yield this.BleManager.startNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
260
|
+
this.addLog(`Started notifications for ${characteristicName} characteristic`);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
this.addLog(`Attemped to stop notifications for ${characteristicName} characteristic: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
})).pipe(switchMap(() => this.bleEvents.didUpdateValueForCharacteristic$), finalize(() => __awaiter(this, void 0, void 0, function* () {
|
|
267
|
+
var _b;
|
|
268
|
+
if (manageNotifications) {
|
|
269
|
+
try {
|
|
270
|
+
yield this.BleManager.stopNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
271
|
+
this.addLog(`Stopped notifications for ${characteristicName} characteristic`);
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
this.addLog(`Attemped to stop notifications for ${characteristicName} characteristic: ${(_b = error === null || error === void 0 ? void 0 : error.message) !== null && _b !== void 0 ? _b : error}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
})), filter(({ characteristic }) => characteristic === characteristicUUID), map(({ value }) => decode(this.type, value)), stitchChunks({ delimiter: BLUETOOTH_CHUNK_DELIMITER }), map((payload) => {
|
|
278
|
+
var _a;
|
|
279
|
+
try {
|
|
280
|
+
return JSON.parse(payload);
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
this.addLog(`Failed to parse JSON for ${characteristicName} characteristic. Falling back to unparsed string. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
284
|
+
return payload;
|
|
285
|
+
}
|
|
286
|
+
}));
|
|
287
|
+
return this.connection$.pipe(switchMap((connection) => connection === BLUETOOTH_CONNECTION.CONNECTED
|
|
288
|
+
? getData(this.getCharacteristicByName(characteristicName))
|
|
289
|
+
: NEVER));
|
|
290
|
+
}
|
|
291
|
+
readCharacteristic(characteristicName, parse = false) {
|
|
292
|
+
var _a;
|
|
293
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
294
|
+
this.addLog(`Reading characteristic: ${characteristicName}`);
|
|
295
|
+
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
296
|
+
if (!characteristicUUID) {
|
|
297
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
const value = yield this.BleManager.read(peripheralId, serviceUUID, characteristicUUID);
|
|
301
|
+
const decodedValue = decode(this.type, value);
|
|
302
|
+
const data = parse ? JSON.parse(decodedValue) : decodedValue;
|
|
303
|
+
this.addLog(`Received read data from ${characteristicName} characteristic: \n${data}`);
|
|
304
|
+
return data;
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
return Promise.reject(new Error(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`));
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
writeCharacteristic(characteristicName, data) {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
313
|
+
this.addLog(`Writing characteristic: ${characteristicName}`);
|
|
314
|
+
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
315
|
+
if (!characteristicUUID) {
|
|
316
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
317
|
+
}
|
|
318
|
+
const encoded = encode(this.type, data);
|
|
319
|
+
yield this.BleManager.write(peripheralId, serviceUUID, characteristicUUID, encoded, REACT_NATIVE_MAX_BYTE_SIZE);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
_addPendingAction(actionId) {
|
|
323
|
+
const actions = this.pendingActions$.getValue();
|
|
324
|
+
this.pendingActions$.next([...actions, actionId]);
|
|
325
|
+
}
|
|
326
|
+
_removePendingAction(actionId) {
|
|
327
|
+
const actions = this.pendingActions$.getValue();
|
|
328
|
+
this.pendingActions$.next(actions.filter((id) => id !== actionId));
|
|
329
|
+
}
|
|
330
|
+
_autoToggleActionNotifications(selectedDevice$) {
|
|
331
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
332
|
+
let started = false;
|
|
333
|
+
const sideEffects$ = this.connection$.asObservable().pipe(switchMap((connection) => connection === BLUETOOTH_CONNECTION.CONNECTED
|
|
334
|
+
? this.pendingActions$
|
|
335
|
+
: NEVER), tap((pendingActions) => __awaiter(this, void 0, void 0, function* () {
|
|
336
|
+
var _a, _b;
|
|
337
|
+
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName("actions");
|
|
338
|
+
const hasPendingActions = !!pendingActions.length;
|
|
339
|
+
if (hasPendingActions && !started) {
|
|
340
|
+
started = true;
|
|
341
|
+
try {
|
|
342
|
+
yield this.BleManager.startNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
343
|
+
this.addLog(`Started notifications for [actions] characteristic`);
|
|
344
|
+
}
|
|
345
|
+
catch (error) {
|
|
346
|
+
this.addLog(`Attemped to start notifications for [actions] characteristic: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (!hasPendingActions && started) {
|
|
350
|
+
started = false;
|
|
351
|
+
try {
|
|
352
|
+
yield this.BleManager.stopNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
353
|
+
this.addLog(`Stopped notifications for actions characteristic`);
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
this.addLog(`Attemped to stop notifications for [actions] characteristic: ${(_b = error === null || error === void 0 ? void 0 : error.message) !== null && _b !== void 0 ? _b : error}`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
})));
|
|
360
|
+
selectedDevice$
|
|
361
|
+
.pipe(switchMap((selectedDevice) => !osHasBluetoothSupport(selectedDevice) ? EMPTY : sideEffects$))
|
|
362
|
+
.subscribe();
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
dispatchAction({ characteristicName, action }) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
const { responseRequired = false, responseTimeout = DEFAULT_ACTION_RESPONSE_TIMEOUT } = action;
|
|
368
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
369
|
+
const actionId = create6DigitPin(); // use to later identify and filter response
|
|
370
|
+
const payload = JSON.stringify(Object.assign({ actionId }, action)); // add the response id to the action
|
|
371
|
+
this.addLog(`Dispatched action with id ${actionId}`);
|
|
372
|
+
if (responseRequired && responseTimeout) {
|
|
373
|
+
this._addPendingAction(actionId);
|
|
374
|
+
const timeout = timer(responseTimeout).subscribe(() => {
|
|
375
|
+
this._removePendingAction(actionId);
|
|
376
|
+
reject(new Error(`Action with id ${actionId} timed out after ${responseTimeout}ms`));
|
|
377
|
+
});
|
|
378
|
+
// listen for a response before writing
|
|
379
|
+
this.subscribeToCharacteristic({
|
|
380
|
+
characteristicName,
|
|
381
|
+
manageNotifications: false
|
|
382
|
+
})
|
|
383
|
+
.pipe(filter((response) => (response === null || response === void 0 ? void 0 : response.actionId) === actionId), take(1))
|
|
384
|
+
.subscribe((response) => {
|
|
385
|
+
timeout.unsubscribe();
|
|
386
|
+
this._removePendingAction(actionId);
|
|
387
|
+
resolve(response);
|
|
388
|
+
});
|
|
389
|
+
// register action by writing
|
|
390
|
+
this.writeCharacteristic(characteristicName, payload).catch((error) => {
|
|
391
|
+
this._removePendingAction(actionId);
|
|
392
|
+
reject(error);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
this.writeCharacteristic(characteristicName, payload)
|
|
397
|
+
.then(() => {
|
|
398
|
+
resolve(null);
|
|
399
|
+
})
|
|
400
|
+
.catch((error) => {
|
|
401
|
+
reject(error);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}));
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export declare class BleManager {
|
|
2
|
+
start(options?: StartOptions): Promise<void>;
|
|
3
|
+
scan(serviceUUIDs: string[], seconds: number, allowDuplicates?: boolean, options?: ScanOptions): Promise<void>;
|
|
4
|
+
stopScan(): Promise<void>;
|
|
5
|
+
connect(peripheralID: string): Promise<void>;
|
|
6
|
+
disconnect(peripheralID: string, force?: boolean): Promise<void>;
|
|
7
|
+
checkState(): void;
|
|
8
|
+
startNotification(peripheralID: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
|
|
9
|
+
startNotificationUseBuffer(peripheralID: string, serviceUUID: string, characteristicUUID: string, buffer: number): Promise<void>;
|
|
10
|
+
stopNotification(peripheralID: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
|
|
11
|
+
read(peripheralID: string, serviceUUID: string, characteristicUUID: string): Promise<any>;
|
|
12
|
+
write(peripheralID: string, serviceUUID: string, characteristicUUID: string, data: any, maxByteSize?: number): Promise<void>;
|
|
13
|
+
writeWithoutResponse(peripheralID: string, serviceUUID: string, characteristicUUID: string, data: any, maxByteSize?: number, queueSleepTime?: number): Promise<void>;
|
|
14
|
+
readRSSI(peripheralID: string): Promise<void>;
|
|
15
|
+
getConnectedPeripherals(serviceUUIDs: string[]): Promise<Peripheral[]>;
|
|
16
|
+
getDiscoveredPeripherals(): Promise<Peripheral[]>;
|
|
17
|
+
isPeripheralConnected(peripheralID: string, serviceUUIDs: string[]): Promise<boolean>;
|
|
18
|
+
requestConnectionPriority(peripheralID: string, connectionPriority: ConnectionPriority): Promise<void>;
|
|
19
|
+
enableBluetooth(): Promise<void>;
|
|
20
|
+
refreshCache(peripheralID: string): Promise<void>;
|
|
21
|
+
requestMTU(peripheralID: string, mtu: number): Promise<void>;
|
|
22
|
+
createBond(peripheralID: string, peripheralPin?: string): Promise<void>;
|
|
23
|
+
removeBond(peripheralID: string): Promise<void>;
|
|
24
|
+
getBondedPeripherals(): Promise<Peripheral[]>;
|
|
25
|
+
removePeripheral(peripheralID: string): Promise<void>;
|
|
26
|
+
retrieveServices(peripheralID: string, serviceUUIDs?: string[]): Promise<PeripheralInfo>;
|
|
27
|
+
}
|
|
28
|
+
export interface Peripheral {
|
|
29
|
+
id: string;
|
|
30
|
+
rssi: number;
|
|
31
|
+
name?: string;
|
|
32
|
+
advertising: AdvertisingData;
|
|
33
|
+
}
|
|
34
|
+
export interface AdvertisingData {
|
|
35
|
+
isConnectable?: boolean;
|
|
36
|
+
localName?: string;
|
|
37
|
+
manufacturerData?: any;
|
|
38
|
+
serviceUUIDs?: string[];
|
|
39
|
+
txPowerLevel?: number;
|
|
40
|
+
}
|
|
41
|
+
export interface StartOptions {
|
|
42
|
+
showAlert?: boolean;
|
|
43
|
+
restoreIdentifierKey?: string;
|
|
44
|
+
queueIdentifierKey?: string;
|
|
45
|
+
forceLegacy?: boolean;
|
|
46
|
+
}
|
|
47
|
+
export interface ScanOptions {
|
|
48
|
+
numberOfMatches?: number;
|
|
49
|
+
matchMode?: number;
|
|
50
|
+
scanMode?: number;
|
|
51
|
+
reportDelay?: number;
|
|
52
|
+
}
|
|
53
|
+
export declare enum ConnectionPriority {
|
|
54
|
+
balanced = 0,
|
|
55
|
+
high = 1,
|
|
56
|
+
low = 2
|
|
57
|
+
}
|
|
58
|
+
export interface PeripheralInfo {
|
|
59
|
+
characteristics: Characteristic[];
|
|
60
|
+
}
|
|
61
|
+
export interface Characteristic {
|
|
62
|
+
characteristic: string;
|
|
63
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Taken from react-native-ble-manager project
|
|
2
|
+
// [Android only API 21+]
|
|
3
|
+
export var ConnectionPriority;
|
|
4
|
+
(function (ConnectionPriority) {
|
|
5
|
+
ConnectionPriority[ConnectionPriority["balanced"] = 0] = "balanced";
|
|
6
|
+
ConnectionPriority[ConnectionPriority["high"] = 1] = "high";
|
|
7
|
+
ConnectionPriority[ConnectionPriority["low"] = 2] = "low";
|
|
8
|
+
})(ConnectionPriority || (ConnectionPriority = {}));
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EmitterSubscription represents a subscription with listener and context data.
|
|
3
|
+
*/
|
|
4
|
+
interface EmitterSubscription extends EventSubscription {
|
|
5
|
+
emitter: EventEmitter;
|
|
6
|
+
listener: () => any;
|
|
7
|
+
context: any;
|
|
8
|
+
/**
|
|
9
|
+
* @param emitter - The event emitter that registered this
|
|
10
|
+
* subscription
|
|
11
|
+
* @param subscriber - The subscriber that controls
|
|
12
|
+
* this subscription
|
|
13
|
+
* @param listener - Function to invoke when the specified event is
|
|
14
|
+
* emitted
|
|
15
|
+
* @param context - Optional context object to use when invoking the
|
|
16
|
+
* listener
|
|
17
|
+
*/
|
|
18
|
+
new (emitter: EventEmitter, subscriber: EventSubscriptionVendor, listener: () => any, context: any): EmitterSubscription;
|
|
19
|
+
/**
|
|
20
|
+
* Removes this subscription from the emitter that registered it.
|
|
21
|
+
* Note: we're overriding the `remove()` method of EventSubscription here
|
|
22
|
+
* but deliberately not calling `super.remove()` as the responsibility
|
|
23
|
+
* for removing the subscription lies with the EventEmitter.
|
|
24
|
+
*/
|
|
25
|
+
remove(): void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* EventSubscription represents a subscription to a particular event. It can
|
|
29
|
+
* remove its own subscription.
|
|
30
|
+
*/
|
|
31
|
+
interface EventSubscription {
|
|
32
|
+
eventType: string;
|
|
33
|
+
key: number;
|
|
34
|
+
subscriber: EventSubscriptionVendor;
|
|
35
|
+
/**
|
|
36
|
+
* @param subscriber the subscriber that controls
|
|
37
|
+
* this subscription.
|
|
38
|
+
*/
|
|
39
|
+
new (subscriber: EventSubscriptionVendor): EventSubscription;
|
|
40
|
+
/**
|
|
41
|
+
* Removes this subscription from the subscriber that controls it.
|
|
42
|
+
*/
|
|
43
|
+
remove(): void;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* EventSubscriptionVendor stores a set of EventSubscriptions that are
|
|
47
|
+
* subscribed to a particular event type.
|
|
48
|
+
*/
|
|
49
|
+
declare class EventSubscriptionVendor {
|
|
50
|
+
constructor();
|
|
51
|
+
/**
|
|
52
|
+
* Adds a subscription keyed by an event type.
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
addSubscription(eventType: string, subscription: EventSubscription): EventSubscription;
|
|
56
|
+
/**
|
|
57
|
+
* Removes a bulk set of the subscriptions.
|
|
58
|
+
*
|
|
59
|
+
* @param eventType - Optional name of the event type whose
|
|
60
|
+
* registered supscriptions to remove, if null remove all subscriptions.
|
|
61
|
+
*/
|
|
62
|
+
removeAllSubscriptions(eventType?: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Removes a specific subscription. Instead of calling this function, call
|
|
65
|
+
* `subscription.remove()` directly.
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
removeSubscription(subscription: any): void;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the array of subscriptions that are currently registered for the
|
|
71
|
+
* given event type.
|
|
72
|
+
*
|
|
73
|
+
* Note: This array can be potentially sparse as subscriptions are deleted
|
|
74
|
+
* from it when they are removed.
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
getSubscriptionsForType(eventType: string): EventSubscription[];
|
|
78
|
+
}
|
|
79
|
+
declare class EventEmitter {
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
* @param subscriber - Optional subscriber instance
|
|
83
|
+
* to use. If omitted, a new subscriber will be created for the emitter.
|
|
84
|
+
*/
|
|
85
|
+
constructor(subscriber?: EventSubscriptionVendor | null);
|
|
86
|
+
/**
|
|
87
|
+
* Adds a listener to be invoked when events of the specified type are
|
|
88
|
+
* emitted. An optional calling context may be provided. The data arguments
|
|
89
|
+
* emitted will be passed to the listener function.
|
|
90
|
+
*
|
|
91
|
+
* @param eventType - Name of the event to listen to
|
|
92
|
+
* @param listener - Function to invoke when the specified event is
|
|
93
|
+
* emitted
|
|
94
|
+
* @param context - Optional context object to use when invoking the
|
|
95
|
+
* listener
|
|
96
|
+
*/
|
|
97
|
+
addListener(eventType: string, listener: (...args: any[]) => any, context?: any): EmitterSubscription;
|
|
98
|
+
/**
|
|
99
|
+
* Removes all of the registered listeners, including those registered as
|
|
100
|
+
* listener maps.
|
|
101
|
+
*
|
|
102
|
+
* @param eventType - Optional name of the event whose registered
|
|
103
|
+
* listeners to remove
|
|
104
|
+
*/
|
|
105
|
+
removeAllListeners(eventType?: string): void;
|
|
106
|
+
/**
|
|
107
|
+
* Returns the number of listeners that are currently registered for the given
|
|
108
|
+
* event.
|
|
109
|
+
*
|
|
110
|
+
* @param eventType - Name of the event to query
|
|
111
|
+
*/
|
|
112
|
+
listenerCount(eventType: string): number;
|
|
113
|
+
/**
|
|
114
|
+
* Emits an event of the given type with the given data. All handlers of that
|
|
115
|
+
* particular type will be notified.
|
|
116
|
+
*
|
|
117
|
+
* @param eventType - Name of the event to emit
|
|
118
|
+
* @param Arbitrary arguments to be passed to each registered listener
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* emitter.addListener('someEvent', function(message) {
|
|
122
|
+
* console.log(message);
|
|
123
|
+
* });
|
|
124
|
+
*
|
|
125
|
+
* emitter.emit('someEvent', 'abc'); // logs 'abc'
|
|
126
|
+
*/
|
|
127
|
+
emit(eventType: string, ...params: any[]): void;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The React Native implementation of the IOS RCTEventEmitter which is required when creating
|
|
131
|
+
* a module that communicates with IOS
|
|
132
|
+
*/
|
|
133
|
+
declare type NativeModule = {
|
|
134
|
+
/**
|
|
135
|
+
* Add the provided eventType as an active listener
|
|
136
|
+
* @param eventType name of the event for which we are registering listener
|
|
137
|
+
*/
|
|
138
|
+
addListener: (eventType: string) => void;
|
|
139
|
+
/**
|
|
140
|
+
* Remove a specified number of events. There are no eventTypes in this case, as
|
|
141
|
+
* the native side doesn't remove the name, but only manages a counter of total
|
|
142
|
+
* listeners
|
|
143
|
+
* @param count number of listeners to remove (of any type)
|
|
144
|
+
*/
|
|
145
|
+
removeListeners: (count: number) => void;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Abstract base class for implementing event-emitting modules. This implements
|
|
149
|
+
* a subset of the standard EventEmitter node module API.
|
|
150
|
+
*/
|
|
151
|
+
export declare class NativeEventEmitter extends EventEmitter {
|
|
152
|
+
/**
|
|
153
|
+
* @param nativeModule the NativeModule implementation. This is required on IOS and will throw
|
|
154
|
+
* an invariant error if undefined.
|
|
155
|
+
*/
|
|
156
|
+
constructor(nativeModule?: NativeModule);
|
|
157
|
+
/**
|
|
158
|
+
* Add the specified listener, this call passes through to the NativeModule
|
|
159
|
+
* addListener
|
|
160
|
+
*
|
|
161
|
+
* @param eventType name of the event for which we are registering listener
|
|
162
|
+
* @param listener the listener function
|
|
163
|
+
* @param context context of the listener
|
|
164
|
+
*/
|
|
165
|
+
addListener(eventType: string, listener: (event: any) => void, context?: Object): EmitterSubscription;
|
|
166
|
+
/**
|
|
167
|
+
* @param eventType name of the event whose registered listeners to remove
|
|
168
|
+
*/
|
|
169
|
+
removeAllListeners(eventType: string): void;
|
|
170
|
+
/**
|
|
171
|
+
* Removes a subscription created by the addListener, the EventSubscription#remove()
|
|
172
|
+
* function actually calls through to this.
|
|
173
|
+
*/
|
|
174
|
+
removeSubscription(subscription: EmitterSubscription): void;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @see https://reactnative.dev/docs/platform-specific-code#content
|
|
178
|
+
*/
|
|
179
|
+
export declare type PlatformOSType = "ios" | "android" | "macos" | "windows" | "web" | "native";
|
|
180
|
+
export {};
|