@neurosity/sdk 6.4.0-next.0 → 6.4.0
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 +777 -806
- package/dist/browser/neurosity.js +13 -13
- package/dist/browser/neurosity.js.map +1 -1
- package/dist/cjs/Neurosity.d.ts +5 -0
- package/dist/cjs/Neurosity.js +18 -6
- package/dist/cjs/api/bluetooth/BluetoothClient.d.ts +3 -3
- package/dist/cjs/api/bluetooth/BluetoothClient.js +34 -33
- package/dist/cjs/api/bluetooth/react-native/ReactNativeTransport.d.ts +4 -5
- package/dist/cjs/api/bluetooth/react-native/ReactNativeTransport.js +38 -48
- package/dist/cjs/api/bluetooth/web/WebBluetoothTransport.d.ts +4 -5
- package/dist/cjs/api/bluetooth/web/WebBluetoothTransport.js +36 -46
- package/dist/electron/index.js +9 -9
- package/dist/electron/index.js.map +1 -1
- package/dist/esm/Neurosity.d.ts +5 -0
- package/dist/esm/Neurosity.js +19 -7
- package/dist/esm/api/bluetooth/BluetoothClient.d.ts +3 -3
- package/dist/esm/api/bluetooth/BluetoothClient.js +35 -34
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.d.ts +4 -5
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.js +35 -45
- package/dist/esm/api/bluetooth/web/WebBluetoothTransport.d.ts +4 -5
- package/dist/esm/api/bluetooth/web/WebBluetoothTransport.js +35 -45
- package/dist/esm/neurosity.mjs +777 -806
- package/dist/examples/neurosity.iife.js +777 -806
- package/dist/examples/neurosity.js +13 -13
- package/dist/examples/neurosity.mjs +777 -806
- package/package.json +1 -1
package/dist/cjs/Neurosity.d.ts
CHANGED
|
@@ -80,6 +80,11 @@ export declare class Neurosity {
|
|
|
80
80
|
* @hidden
|
|
81
81
|
*/
|
|
82
82
|
_initStreamingMode(streamingMode: STREAMING_MODE, hasBluetoothTransport: boolean): void;
|
|
83
|
+
/**
|
|
84
|
+
*
|
|
85
|
+
* @hidden
|
|
86
|
+
*/
|
|
87
|
+
_osHasBluetoothSupport(): Observable<any>;
|
|
83
88
|
/**
|
|
84
89
|
* Subscribe to the device's streaming state changes and the current strategy
|
|
85
90
|
*
|
package/dist/cjs/Neurosity.js
CHANGED
|
@@ -100,7 +100,7 @@ class Neurosity {
|
|
|
100
100
|
if (!!bluetoothTransport) {
|
|
101
101
|
this.bluetoothClient = new bluetooth_1.BluetoothClient({
|
|
102
102
|
selectedDevice$: this.onDeviceChange(),
|
|
103
|
-
|
|
103
|
+
osHasBluetoothSupport$: this._osHasBluetoothSupport(),
|
|
104
104
|
createBluetoothToken: this.createBluetoothToken.bind(this),
|
|
105
105
|
transport: bluetoothTransport
|
|
106
106
|
});
|
|
@@ -131,6 +131,16 @@ class Neurosity {
|
|
|
131
131
|
this.streamingMode$.next(streamingMode);
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
*
|
|
136
|
+
* @hidden
|
|
137
|
+
*/
|
|
138
|
+
_osHasBluetoothSupport() {
|
|
139
|
+
return (0, rxjs_1.combineLatest)({
|
|
140
|
+
selectedDevice: this.onDeviceChange(),
|
|
141
|
+
osVersion: this.osVersion().pipe((0, operators_1.startWith)(null))
|
|
142
|
+
}).pipe((0, operators_1.map)(({ selectedDevice, osVersion }) => (0, bluetooth_1.osHasBluetoothSupport)(selectedDevice, osVersion)));
|
|
143
|
+
}
|
|
134
144
|
/**
|
|
135
145
|
* Subscribe to the device's streaming state changes and the current strategy
|
|
136
146
|
*
|
|
@@ -146,12 +156,14 @@ class Neurosity {
|
|
|
146
156
|
streamingState() {
|
|
147
157
|
const isWifiOnline = (state) => [status_1.STATUS.ONLINE, status_1.STATUS.UPDATING].includes(state);
|
|
148
158
|
return this.streamingMode$.pipe((0, operators_1.switchMap)((streamingMode) => {
|
|
149
|
-
return
|
|
150
|
-
|
|
159
|
+
return (0, rxjs_1.combineLatest)({
|
|
160
|
+
selectedDevice: this.onDeviceChange(),
|
|
161
|
+
osHasBluetoothSupport: this._osHasBluetoothSupport()
|
|
162
|
+
}).pipe((0, operators_1.switchMap)(({ selectedDevice, osHasBluetoothSupport }) => {
|
|
163
|
+
if (!selectedDevice) {
|
|
151
164
|
return rxjs_2.EMPTY;
|
|
152
165
|
}
|
|
153
|
-
const isUnableToUseBluetooth = this.isMissingBluetoothTransport ||
|
|
154
|
-
!(0, bluetooth_1.osHasBluetoothSupport)(selectDevice, osVersion);
|
|
166
|
+
const isUnableToUseBluetooth = this.isMissingBluetoothTransport || !osHasBluetoothSupport;
|
|
155
167
|
if (isUnableToUseBluetooth) {
|
|
156
168
|
return this.cloudClient.status().pipe((0, operators_1.map)(({ state }) => ({
|
|
157
169
|
connected: isWifiOnline(state),
|
|
@@ -195,7 +207,7 @@ class Neurosity {
|
|
|
195
207
|
: streaming_1.STREAMING_TYPE.WIFI
|
|
196
208
|
};
|
|
197
209
|
}
|
|
198
|
-
}), (0,
|
|
210
|
+
}), (0, operators_2.distinctUntilChanged)((a, b) => (0, fast_deep_equal_1.default)(a, b)));
|
|
199
211
|
}));
|
|
200
212
|
}));
|
|
201
213
|
}
|
|
@@ -2,7 +2,7 @@ import { Observable } from "rxjs";
|
|
|
2
2
|
import { ReplaySubject } from "rxjs";
|
|
3
3
|
import { WebBluetoothTransport } from "./web/WebBluetoothTransport";
|
|
4
4
|
import { ReactNativeTransport } from "./react-native/ReactNativeTransport";
|
|
5
|
-
import { DeviceInfo
|
|
5
|
+
import { DeviceInfo } from "../../types/deviceInfo";
|
|
6
6
|
import { Action } from "../../types/actions";
|
|
7
7
|
import { Epoch } from "../../types/epoch";
|
|
8
8
|
import { BLUETOOTH_CONNECTION } from "./types";
|
|
@@ -16,14 +16,14 @@ declare type CreateBluetoothToken = () => Promise<string>;
|
|
|
16
16
|
declare type Options = {
|
|
17
17
|
transport: BluetoothTransport;
|
|
18
18
|
selectedDevice$: Observable<DeviceInfo>;
|
|
19
|
-
|
|
19
|
+
osHasBluetoothSupport$: Observable<boolean>;
|
|
20
20
|
createBluetoothToken: CreateBluetoothToken;
|
|
21
21
|
};
|
|
22
22
|
export declare class BluetoothClient {
|
|
23
23
|
transport: BluetoothTransport;
|
|
24
24
|
deviceInfo: DeviceInfo;
|
|
25
25
|
selectedDevice$: ReplaySubject<DeviceInfo>;
|
|
26
|
-
|
|
26
|
+
osHasBluetoothSupport$: ReplaySubject<boolean>;
|
|
27
27
|
isAuthenticated$: ReplaySubject<boolean>;
|
|
28
28
|
_focus$: Observable<any>;
|
|
29
29
|
_calm$: Observable<any>;
|
|
@@ -13,18 +13,16 @@ exports.BluetoothClient = void 0;
|
|
|
13
13
|
const rxjs_1 = require("rxjs");
|
|
14
14
|
const rxjs_2 = require("rxjs");
|
|
15
15
|
const operators_1 = require("rxjs/operators");
|
|
16
|
-
const operators_2 = require("rxjs/operators");
|
|
17
16
|
const WebBluetoothTransport_1 = require("./web/WebBluetoothTransport");
|
|
18
17
|
const ReactNativeTransport_1 = require("./react-native/ReactNativeTransport");
|
|
19
18
|
const csvBufferToEpoch_1 = require("./utils/csvBufferToEpoch");
|
|
20
19
|
const types_1 = require("./types");
|
|
21
|
-
const osHasBluetoothSupport_1 = require("./utils/osHasBluetoothSupport");
|
|
22
20
|
class BluetoothClient {
|
|
23
21
|
constructor(options) {
|
|
24
22
|
this.selectedDevice$ = new rxjs_2.ReplaySubject(1);
|
|
25
|
-
this.
|
|
23
|
+
this.osHasBluetoothSupport$ = new rxjs_2.ReplaySubject(1);
|
|
26
24
|
this.isAuthenticated$ = new rxjs_2.ReplaySubject(1);
|
|
27
|
-
const { transport, selectedDevice$, createBluetoothToken } = options !== null && options !== void 0 ? options : {};
|
|
25
|
+
const { transport, selectedDevice$, osHasBluetoothSupport$, createBluetoothToken } = options !== null && options !== void 0 ? options : {};
|
|
28
26
|
if (!transport) {
|
|
29
27
|
throw new Error(`No bluetooth transport provided.`);
|
|
30
28
|
}
|
|
@@ -33,9 +31,14 @@ class BluetoothClient {
|
|
|
33
31
|
if (selectedDevice$) {
|
|
34
32
|
selectedDevice$.subscribe(this.selectedDevice$);
|
|
35
33
|
}
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
// Pass events to the internal osHasBluetoothSupport$ if osHasBluetoothSupport$ is passed via options
|
|
35
|
+
if (osHasBluetoothSupport$) {
|
|
36
|
+
osHasBluetoothSupport$.subscribe(this.osHasBluetoothSupport$);
|
|
37
|
+
}
|
|
38
|
+
this.osHasBluetoothSupport$
|
|
39
|
+
.pipe((0, operators_1.switchMap)((osHasBluetoothSupport) => osHasBluetoothSupport
|
|
40
|
+
? this.transport._autoConnect(this.selectedDevice$)
|
|
41
|
+
: rxjs_2.EMPTY))
|
|
39
42
|
.subscribe({
|
|
40
43
|
error: (error) => {
|
|
41
44
|
var _a;
|
|
@@ -51,7 +54,11 @@ class BluetoothClient {
|
|
|
51
54
|
this.transport.addLog("Auto authentication not enabled");
|
|
52
55
|
}
|
|
53
56
|
// Auto manage action notifications
|
|
54
|
-
this.
|
|
57
|
+
this.osHasBluetoothSupport$
|
|
58
|
+
.pipe((0, operators_1.switchMap)((osHasBluetoothSupport) => osHasBluetoothSupport
|
|
59
|
+
? this.transport._autoToggleActionNotifications()
|
|
60
|
+
: rxjs_2.EMPTY))
|
|
61
|
+
.subscribe();
|
|
55
62
|
// Multicast metrics (share)
|
|
56
63
|
this._focus$ = this._subscribeWhileAuthenticated("focus");
|
|
57
64
|
this._calm$ = this._subscribeWhileAuthenticated("calm");
|
|
@@ -75,28 +82,24 @@ class BluetoothClient {
|
|
|
75
82
|
const reauthenticateInterval$ = (0, rxjs_1.timer)(0, REAUTHENTICATE_INTERVAL).pipe((0, operators_1.tap)(() => {
|
|
76
83
|
this.transport.addLog(`Auto authentication in progress...`);
|
|
77
84
|
}));
|
|
78
|
-
return this.
|
|
79
|
-
?
|
|
80
|
-
:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
})))));
|
|
85
|
+
return this.osHasBluetoothSupport$.pipe((0, operators_1.switchMap)((osHasBluetoothSupport) => osHasBluetoothSupport ? this.connection() : rxjs_2.EMPTY), (0, operators_1.switchMap)((connection) => connection === types_1.BLUETOOTH_CONNECTION.CONNECTED
|
|
86
|
+
? reauthenticateInterval$
|
|
87
|
+
: rxjs_2.EMPTY), (0, operators_1.switchMap)(() => __awaiter(this, void 0, void 0, function* () { return yield this.isAuthenticated(); })), (0, operators_1.tap)(([isAuthenticated]) => __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
if (!isAuthenticated) {
|
|
89
|
+
const token = yield createBluetoothToken();
|
|
90
|
+
yield this.authenticate(token);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
this.transport.addLog(`Already authenticated`);
|
|
94
|
+
}
|
|
95
|
+
})));
|
|
91
96
|
}
|
|
92
97
|
enableAutoConnect(autoConnect) {
|
|
93
98
|
this.transport.enableAutoConnect(autoConnect);
|
|
94
99
|
}
|
|
95
100
|
_hasBluetoothSupport() {
|
|
96
101
|
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
-
|
|
98
|
-
const osVersion = yield (0, rxjs_1.firstValueFrom)(this.osVersion$);
|
|
99
|
-
return (0, osHasBluetoothSupport_1.osHasBluetoothSupport)(selectedDevice, osVersion);
|
|
102
|
+
return yield (0, rxjs_2.firstValueFrom)(this.osHasBluetoothSupport$);
|
|
100
103
|
});
|
|
101
104
|
}
|
|
102
105
|
authenticate(token) {
|
|
@@ -167,7 +170,7 @@ class BluetoothClient {
|
|
|
167
170
|
this.transport.addLog(errorMessage);
|
|
168
171
|
return Promise.reject(errorMessage);
|
|
169
172
|
}
|
|
170
|
-
const isAuthenticated = yield (0,
|
|
173
|
+
const isAuthenticated = yield (0, rxjs_2.firstValueFrom)(this.isAuthenticated$);
|
|
171
174
|
if (!isAuthenticated) {
|
|
172
175
|
const errorMessage = `Authentication required.`;
|
|
173
176
|
this.transport.addLog(errorMessage);
|
|
@@ -177,13 +180,11 @@ class BluetoothClient {
|
|
|
177
180
|
});
|
|
178
181
|
}
|
|
179
182
|
_subscribeWhileAuthenticated(characteristicName) {
|
|
180
|
-
return this.
|
|
181
|
-
?
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
})
|
|
186
|
-
: rxjs_2.EMPTY))), (0, operators_1.share)());
|
|
183
|
+
return this.osHasBluetoothSupport$.pipe((0, operators_1.switchMap)((osHasBluetoothSupport) => osHasBluetoothSupport ? this.isAuthenticated$ : rxjs_2.EMPTY), (0, operators_1.distinctUntilChanged)(), (0, operators_1.switchMap)((isAuthenticated) => isAuthenticated
|
|
184
|
+
? this.transport.subscribeToCharacteristic({
|
|
185
|
+
characteristicName
|
|
186
|
+
})
|
|
187
|
+
: rxjs_2.EMPTY), (0, operators_1.share)());
|
|
187
188
|
}
|
|
188
189
|
focus() {
|
|
189
190
|
return this._focus$;
|
|
@@ -224,7 +225,7 @@ class BluetoothClient {
|
|
|
224
225
|
}
|
|
225
226
|
getInfo() {
|
|
226
227
|
return __awaiter(this, void 0, void 0, function* () {
|
|
227
|
-
return yield this._withAuthentication(() => (0,
|
|
228
|
+
return yield this._withAuthentication(() => (0, rxjs_2.firstValueFrom)(this.transport.subscribeToCharacteristic({
|
|
228
229
|
characteristicName: "deviceInfo"
|
|
229
230
|
})));
|
|
230
231
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { BehaviorSubject, ReplaySubject } from "rxjs";
|
|
2
|
-
import { Observable } from "rxjs";
|
|
1
|
+
import { Observable, BehaviorSubject, ReplaySubject } from "rxjs";
|
|
3
2
|
import { BluetoothTransport } from "../BluetoothTransport";
|
|
4
3
|
import { ActionOptions, SubscribeOptions } from "../types";
|
|
5
4
|
import { TRANSPORT_TYPE, BLUETOOTH_CONNECTION } from "../types";
|
|
@@ -7,7 +6,7 @@ import { BleManager } from "./types/BleManagerTypes";
|
|
|
7
6
|
import { Peripheral } from "./types/BleManagerTypes";
|
|
8
7
|
import { NativeEventEmitter } from "./types/ReactNativeTypes";
|
|
9
8
|
import { PlatformOSType } from "./types/ReactNativeTypes";
|
|
10
|
-
import { DeviceInfo
|
|
9
|
+
import { DeviceInfo } from "../../../types/deviceInfo";
|
|
11
10
|
declare type Characteristic = {
|
|
12
11
|
characteristicUUID: string;
|
|
13
12
|
serviceUUID: string;
|
|
@@ -48,7 +47,7 @@ export declare class ReactNativeTransport implements BluetoothTransport {
|
|
|
48
47
|
constructor(options: Options);
|
|
49
48
|
addLog(log: string): void;
|
|
50
49
|
isConnected(): boolean;
|
|
51
|
-
_autoConnect(selectedDevice$: Observable<DeviceInfo
|
|
50
|
+
_autoConnect(selectedDevice$: Observable<DeviceInfo>): Observable<void>;
|
|
52
51
|
enableAutoConnect(autoConnect: boolean): void;
|
|
53
52
|
connection(): Observable<BLUETOOTH_CONNECTION>;
|
|
54
53
|
_fromEvent(eventName: string): Observable<any>;
|
|
@@ -65,7 +64,7 @@ export declare class ReactNativeTransport implements BluetoothTransport {
|
|
|
65
64
|
writeCharacteristic(characteristicName: string, data: string): Promise<void>;
|
|
66
65
|
_addPendingAction(actionId: number): void;
|
|
67
66
|
_removePendingAction(actionId: number): void;
|
|
68
|
-
_autoToggleActionNotifications(
|
|
67
|
+
_autoToggleActionNotifications(): Observable<any>;
|
|
69
68
|
dispatchAction({ characteristicName, action }: ActionOptions): Promise<any>;
|
|
70
69
|
}
|
|
71
70
|
export {};
|
|
@@ -26,7 +26,6 @@ const constants_1 = require("../constants");
|
|
|
26
26
|
const constants_2 = require("../constants");
|
|
27
27
|
const constants_3 = require("../constants");
|
|
28
28
|
const constants_4 = require("../constants");
|
|
29
|
-
const osHasBluetoothSupport_1 = require("../utils/osHasBluetoothSupport");
|
|
30
29
|
const defaultOptions = {
|
|
31
30
|
autoConnect: true
|
|
32
31
|
};
|
|
@@ -104,16 +103,14 @@ class ReactNativeTransport {
|
|
|
104
103
|
const connection = this.connection$.getValue();
|
|
105
104
|
return connection === types_1.BLUETOOTH_CONNECTION.CONNECTED;
|
|
106
105
|
}
|
|
107
|
-
_autoConnect(selectedDevice
|
|
106
|
+
_autoConnect(selectedDevice$) {
|
|
108
107
|
const selectedDeviceAfterDisconnect$ = this.onDisconnected$.pipe((0, operators_1.switchMap)(() => selectedDevice$));
|
|
109
108
|
return this._isAutoConnectEnabled$.pipe((0, operators_1.switchMap)((isAutoConnectEnabled) => isAutoConnectEnabled
|
|
110
|
-
? (0,
|
|
111
|
-
:
|
|
112
|
-
?
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return peripheralMatch ? (0, rxjs_1.of)(peripheralMatch) : rxjs_2.NEVER;
|
|
116
|
-
}), (0, operators_3.distinct)((peripheral) => peripheral.id), (0, operators_3.take)(1))), (0, operators_1.switchMap)((peripheral) => __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
? (0, rxjs_2.merge)(selectedDevice$, selectedDeviceAfterDisconnect$)
|
|
110
|
+
: rxjs_1.NEVER), (0, operators_1.switchMap)((selectedDevice) => this.scan().pipe((0, operators_1.switchMap)((peripherals) => {
|
|
111
|
+
const peripheralMatch = peripherals.find((peripheral) => peripheral.name === (selectedDevice === null || selectedDevice === void 0 ? void 0 : selectedDevice.deviceNickname));
|
|
112
|
+
return peripheralMatch ? (0, rxjs_2.of)(peripheralMatch) : rxjs_1.NEVER;
|
|
113
|
+
}), (0, operators_3.distinct)((peripheral) => peripheral.id), (0, operators_3.take)(1))), (0, operators_1.switchMap)((peripheral) => __awaiter(this, void 0, void 0, function* () {
|
|
117
114
|
return yield this.connect(peripheral);
|
|
118
115
|
})));
|
|
119
116
|
}
|
|
@@ -145,7 +142,7 @@ class ReactNativeTransport {
|
|
|
145
142
|
const serviceUUIDs = [ipk_1.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING];
|
|
146
143
|
const allowDuplicates = true;
|
|
147
144
|
const scanOptions = {};
|
|
148
|
-
const scanOnce$ = new
|
|
145
|
+
const scanOnce$ = new rxjs_1.Observable((subscriber) => {
|
|
149
146
|
var _a;
|
|
150
147
|
try {
|
|
151
148
|
this.BleManager.scan(serviceUUIDs, seconds, allowDuplicates, scanOptions).then(() => {
|
|
@@ -163,7 +160,7 @@ class ReactNativeTransport {
|
|
|
163
160
|
});
|
|
164
161
|
const scan$ = once
|
|
165
162
|
? scanOnce$
|
|
166
|
-
: (0,
|
|
163
|
+
: (0, rxjs_2.timer)(0, RESCAN_INTERVAL).pipe((0, operators_1.switchMap)(() => scanOnce$));
|
|
167
164
|
const peripherals$ = scan$.pipe((0, operators_1.tap)(() => {
|
|
168
165
|
if (!skipConnectionUpdate) {
|
|
169
166
|
this.connection$.next(types_1.BLUETOOTH_CONNECTION.SCANNING);
|
|
@@ -261,7 +258,7 @@ class ReactNativeTransport {
|
|
|
261
258
|
return (_a = this.characteristicsByName) === null || _a === void 0 ? void 0 : _a[characteristicName];
|
|
262
259
|
}
|
|
263
260
|
subscribeToCharacteristic({ characteristicName, manageNotifications = true }) {
|
|
264
|
-
const getData = ({ peripheralId, serviceUUID, characteristicUUID }) => (0,
|
|
261
|
+
const getData = ({ peripheralId, serviceUUID, characteristicUUID }) => (0, rxjs_2.defer)(() => __awaiter(this, void 0, void 0, function* () {
|
|
265
262
|
var _a;
|
|
266
263
|
if (manageNotifications) {
|
|
267
264
|
try {
|
|
@@ -295,7 +292,7 @@ class ReactNativeTransport {
|
|
|
295
292
|
}));
|
|
296
293
|
return this.connection$.pipe((0, operators_1.switchMap)((connection) => connection === types_1.BLUETOOTH_CONNECTION.CONNECTED
|
|
297
294
|
? getData(this.getCharacteristicByName(characteristicName))
|
|
298
|
-
:
|
|
295
|
+
: rxjs_1.NEVER));
|
|
299
296
|
}
|
|
300
297
|
readCharacteristic(characteristicName, parse = false) {
|
|
301
298
|
var _a;
|
|
@@ -336,42 +333,35 @@ class ReactNativeTransport {
|
|
|
336
333
|
const actions = this.pendingActions$.getValue();
|
|
337
334
|
this.pendingActions$.next(actions.filter((id) => id !== actionId));
|
|
338
335
|
}
|
|
339
|
-
_autoToggleActionNotifications(
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
this.addLog(`Started notifications for [actions] characteristic`);
|
|
353
|
-
}
|
|
354
|
-
catch (error) {
|
|
355
|
-
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}`);
|
|
356
|
-
}
|
|
336
|
+
_autoToggleActionNotifications() {
|
|
337
|
+
let started = false;
|
|
338
|
+
return this.connection$.asObservable().pipe((0, operators_1.switchMap)((connection) => connection === types_1.BLUETOOTH_CONNECTION.CONNECTED
|
|
339
|
+
? this.pendingActions$
|
|
340
|
+
: rxjs_1.NEVER), (0, operators_1.tap)((pendingActions) => __awaiter(this, void 0, void 0, function* () {
|
|
341
|
+
var _a, _b;
|
|
342
|
+
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName("actions");
|
|
343
|
+
const hasPendingActions = !!pendingActions.length;
|
|
344
|
+
if (hasPendingActions && !started) {
|
|
345
|
+
started = true;
|
|
346
|
+
try {
|
|
347
|
+
yield this.BleManager.startNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
348
|
+
this.addLog(`Started notifications for [actions] characteristic`);
|
|
357
349
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
try {
|
|
361
|
-
yield this.BleManager.stopNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
362
|
-
this.addLog(`Stopped notifications for actions characteristic`);
|
|
363
|
-
}
|
|
364
|
-
catch (error) {
|
|
365
|
-
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}`);
|
|
366
|
-
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
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}`);
|
|
367
352
|
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
353
|
+
}
|
|
354
|
+
if (!hasPendingActions && started) {
|
|
355
|
+
started = false;
|
|
356
|
+
try {
|
|
357
|
+
yield this.BleManager.stopNotification(peripheralId, serviceUUID, characteristicUUID);
|
|
358
|
+
this.addLog(`Stopped notifications for actions characteristic`);
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
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}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
})));
|
|
375
365
|
}
|
|
376
366
|
dispatchAction({ characteristicName, action }) {
|
|
377
367
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -382,7 +372,7 @@ class ReactNativeTransport {
|
|
|
382
372
|
this.addLog(`Dispatched action with id ${actionId}`);
|
|
383
373
|
if (responseRequired && responseTimeout) {
|
|
384
374
|
this._addPendingAction(actionId);
|
|
385
|
-
const timeout = (0,
|
|
375
|
+
const timeout = (0, rxjs_2.timer)(responseTimeout).subscribe(() => {
|
|
386
376
|
this._removePendingAction(actionId);
|
|
387
377
|
reject(new Error(`Action with id ${actionId} timed out after ${responseTimeout}ms`));
|
|
388
378
|
});
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/// <reference types="web-bluetooth" />
|
|
2
|
-
import { BehaviorSubject, ReplaySubject } from "rxjs";
|
|
3
|
-
import { Observable } from "rxjs";
|
|
2
|
+
import { Observable, BehaviorSubject, ReplaySubject } from "rxjs";
|
|
4
3
|
import { BluetoothTransport } from "../BluetoothTransport";
|
|
5
4
|
import { ActionOptions, SubscribeOptions } from "../types";
|
|
6
5
|
import { TRANSPORT_TYPE, BLUETOOTH_CONNECTION } from "../types";
|
|
7
|
-
import { DeviceInfo
|
|
6
|
+
import { DeviceInfo } from "../../../types/deviceInfo";
|
|
8
7
|
declare type Options = {
|
|
9
8
|
autoConnect?: boolean;
|
|
10
9
|
};
|
|
@@ -25,7 +24,7 @@ export declare class WebBluetoothTransport implements BluetoothTransport {
|
|
|
25
24
|
_isAutoConnectEnabled$: ReplaySubject<boolean>;
|
|
26
25
|
constructor(options?: Options);
|
|
27
26
|
_getPairedDevices(): Promise<BluetoothDevice[]>;
|
|
28
|
-
_autoConnect(selectedDevice$: Observable<DeviceInfo
|
|
27
|
+
_autoConnect(selectedDevice$: Observable<DeviceInfo>): Observable<void>;
|
|
29
28
|
enableAutoConnect(autoConnect: boolean): void;
|
|
30
29
|
addLog(log: string): void;
|
|
31
30
|
isConnected(): boolean;
|
|
@@ -48,7 +47,7 @@ export declare class WebBluetoothTransport implements BluetoothTransport {
|
|
|
48
47
|
writeCharacteristic(characteristicName: string, data: string): Promise<void>;
|
|
49
48
|
_addPendingAction(actionId: number): void;
|
|
50
49
|
_removePendingAction(actionId: number): void;
|
|
51
|
-
_autoToggleActionNotifications(
|
|
50
|
+
_autoToggleActionNotifications(): Observable<any>;
|
|
52
51
|
dispatchAction({ characteristicName, action }: ActionOptions): Promise<any>;
|
|
53
52
|
}
|
|
54
53
|
export {};
|
|
@@ -26,7 +26,6 @@ const encoding_1 = require("../utils/encoding");
|
|
|
26
26
|
const types_1 = require("../types");
|
|
27
27
|
const constants_1 = require("../constants");
|
|
28
28
|
const constants_2 = require("../constants");
|
|
29
|
-
const osHasBluetoothSupport_1 = require("../utils/osHasBluetoothSupport");
|
|
30
29
|
const defaultOptions = {
|
|
31
30
|
autoConnect: true
|
|
32
31
|
};
|
|
@@ -64,12 +63,10 @@ class WebBluetoothTransport {
|
|
|
64
63
|
return yield navigator.bluetooth.getDevices();
|
|
65
64
|
});
|
|
66
65
|
}
|
|
67
|
-
_autoConnect(selectedDevice
|
|
66
|
+
_autoConnect(selectedDevice$) {
|
|
68
67
|
return this._isAutoConnectEnabled$.pipe((0, operators_1.switchMap)((isAutoConnectEnabled) => isAutoConnectEnabled
|
|
69
|
-
? (0,
|
|
70
|
-
: rxjs_2.NEVER), (0, operators_1.
|
|
71
|
-
? (0, rxjs_1.of)(selectedDevice)
|
|
72
|
-
: rxjs_2.EMPTY), (0, operators_1.switchMap)((selectedDevice) => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
? (0, rxjs_2.merge)(selectedDevice$, this.onDisconnected$.pipe((0, operators_1.switchMap)(() => selectedDevice$)))
|
|
69
|
+
: rxjs_2.NEVER), (0, operators_1.switchMap)((selectedDevice) => __awaiter(this, void 0, void 0, function* () {
|
|
73
70
|
var _a;
|
|
74
71
|
const { deviceNickname } = selectedDevice;
|
|
75
72
|
if (this.isConnected()) {
|
|
@@ -215,7 +212,7 @@ class WebBluetoothTransport {
|
|
|
215
212
|
});
|
|
216
213
|
}
|
|
217
214
|
subscribeToCharacteristic({ characteristicName, manageNotifications = true }) {
|
|
218
|
-
const data$ = (0,
|
|
215
|
+
const data$ = (0, rxjs_2.defer)(() => this.getCharacteristicByName(characteristicName)).pipe((0, operators_1.switchMap)((characteristic) => __awaiter(this, void 0, void 0, function* () {
|
|
219
216
|
var _a;
|
|
220
217
|
if (this.isConnected() && manageNotifications) {
|
|
221
218
|
try {
|
|
@@ -309,45 +306,38 @@ class WebBluetoothTransport {
|
|
|
309
306
|
const actions = this.pendingActions$.getValue();
|
|
310
307
|
this.pendingActions$.next(actions.filter((id) => id !== actionId));
|
|
311
308
|
}
|
|
312
|
-
_autoToggleActionNotifications(
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
this.addLog(`Started notifications for [actions] characteristic`);
|
|
329
|
-
}
|
|
330
|
-
catch (error) {
|
|
331
|
-
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}`);
|
|
332
|
-
}
|
|
309
|
+
_autoToggleActionNotifications() {
|
|
310
|
+
let actionsCharacteristic;
|
|
311
|
+
let started = false;
|
|
312
|
+
return this.connection$.asObservable().pipe((0, operators_1.switchMap)((connection) => connection === types_1.BLUETOOTH_CONNECTION.CONNECTED
|
|
313
|
+
? (0, rxjs_2.defer)(() => this.getCharacteristicByName("actions")).pipe((0, operators_1.switchMap)((characteristic) => {
|
|
314
|
+
actionsCharacteristic = characteristic;
|
|
315
|
+
return this.pendingActions$;
|
|
316
|
+
}))
|
|
317
|
+
: rxjs_2.NEVER), (0, operators_1.tap)((pendingActions) => __awaiter(this, void 0, void 0, function* () {
|
|
318
|
+
var _a, _b;
|
|
319
|
+
const hasPendingActions = !!pendingActions.length;
|
|
320
|
+
if (hasPendingActions && !started) {
|
|
321
|
+
started = true;
|
|
322
|
+
try {
|
|
323
|
+
yield actionsCharacteristic.startNotifications();
|
|
324
|
+
this.addLog(`Started notifications for [actions] characteristic`);
|
|
333
325
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
try {
|
|
337
|
-
yield actionsCharacteristic.stopNotifications();
|
|
338
|
-
this.addLog(`Stopped notifications for actions characteristic`);
|
|
339
|
-
}
|
|
340
|
-
catch (error) {
|
|
341
|
-
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}`);
|
|
342
|
-
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
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}`);
|
|
343
328
|
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
329
|
+
}
|
|
330
|
+
if (!hasPendingActions && started) {
|
|
331
|
+
started = false;
|
|
332
|
+
try {
|
|
333
|
+
yield actionsCharacteristic.stopNotifications();
|
|
334
|
+
this.addLog(`Stopped notifications for actions characteristic`);
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
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}`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
})));
|
|
351
341
|
}
|
|
352
342
|
dispatchAction({ characteristicName, action }) {
|
|
353
343
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -364,7 +354,7 @@ class WebBluetoothTransport {
|
|
|
364
354
|
this.addLog(`Dispatched action with id ${actionId}`);
|
|
365
355
|
if (responseRequired && responseTimeout) {
|
|
366
356
|
this._addPendingAction(actionId);
|
|
367
|
-
const timeout = (0,
|
|
357
|
+
const timeout = (0, rxjs_2.timer)(responseTimeout).subscribe(() => {
|
|
368
358
|
this._removePendingAction(actionId);
|
|
369
359
|
reject(`Action with id ${actionId} timed out after ${responseTimeout}ms`);
|
|
370
360
|
});
|
|
@@ -410,7 +400,7 @@ function fromDOMEvent(target, eventName, beforeRemove) {
|
|
|
410
400
|
}));
|
|
411
401
|
}
|
|
412
402
|
function onAdvertisementReceived(device) {
|
|
413
|
-
return new
|
|
403
|
+
return new rxjs_1.Observable((subscriber) => {
|
|
414
404
|
const abortController = new AbortController();
|
|
415
405
|
const { signal } = abortController;
|
|
416
406
|
const listener = device.addEventListener("advertisementreceived", (advertisement) => {
|