@neurosity/sdk 6.2.1 → 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 +14 -9
- package/dist/browser/neurosity.js +1 -1
- package/dist/browser/neurosity.js.map +1 -1
- package/dist/cjs/api/bluetooth/react-native/ReactNativeTransport.js +14 -9
- package/dist/electron/index.js +1 -1
- package/dist/electron/index.js.map +1 -1
- package/dist/esm/api/bluetooth/react-native/ReactNativeTransport.js +14 -9
- package/dist/esm/neurosity.mjs +14 -9
- package/dist/examples/neurosity.iife.js +14 -9
- package/dist/examples/neurosity.js +1 -1
- package/dist/examples/neurosity.mjs +14 -9
- package/package.json +1 -1
- package/CHANGELOG.md +0 -25
|
@@ -197,7 +197,7 @@ export class ReactNativeTransport {
|
|
|
197
197
|
]);
|
|
198
198
|
if (!peripheralInfo) {
|
|
199
199
|
this.addLog("Could not retreive services");
|
|
200
|
-
reject(`Could not retreive services`);
|
|
200
|
+
reject(new Error(`Could not retreive services`));
|
|
201
201
|
return;
|
|
202
202
|
}
|
|
203
203
|
this.addLog(`Got service ${BLUETOOTH_PRIMARY_SERVICE_UUID_STRING}, getting characteristics...`);
|
|
@@ -213,8 +213,13 @@ export class ReactNativeTransport {
|
|
|
213
213
|
]));
|
|
214
214
|
this.addLog(`Got characteristics.`);
|
|
215
215
|
if (this.platform === "android") {
|
|
216
|
-
this.
|
|
217
|
-
|
|
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
|
+
});
|
|
218
223
|
}
|
|
219
224
|
this.addLog(`Successfully connected to peripheral ${peripheral.id}`);
|
|
220
225
|
this.connection$.next(BLUETOOTH_CONNECTION.CONNECTED);
|
|
@@ -289,7 +294,7 @@ export class ReactNativeTransport {
|
|
|
289
294
|
this.addLog(`Reading characteristic: ${characteristicName}`);
|
|
290
295
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
291
296
|
if (!characteristicUUID) {
|
|
292
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
297
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
293
298
|
}
|
|
294
299
|
try {
|
|
295
300
|
const value = yield this.BleManager.read(peripheralId, serviceUUID, characteristicUUID);
|
|
@@ -299,7 +304,7 @@ export class ReactNativeTransport {
|
|
|
299
304
|
return data;
|
|
300
305
|
}
|
|
301
306
|
catch (error) {
|
|
302
|
-
return Promise.reject(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : 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}`));
|
|
303
308
|
}
|
|
304
309
|
});
|
|
305
310
|
}
|
|
@@ -308,7 +313,7 @@ export class ReactNativeTransport {
|
|
|
308
313
|
this.addLog(`Writing characteristic: ${characteristicName}`);
|
|
309
314
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
310
315
|
if (!characteristicUUID) {
|
|
311
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
316
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
312
317
|
}
|
|
313
318
|
const encoded = encode(this.type, data);
|
|
314
319
|
yield this.BleManager.write(peripheralId, serviceUUID, characteristicUUID, encoded, REACT_NATIVE_MAX_BYTE_SIZE);
|
|
@@ -368,7 +373,7 @@ export class ReactNativeTransport {
|
|
|
368
373
|
this._addPendingAction(actionId);
|
|
369
374
|
const timeout = timer(responseTimeout).subscribe(() => {
|
|
370
375
|
this._removePendingAction(actionId);
|
|
371
|
-
reject(`Action with id ${actionId} timed out after ${responseTimeout}ms`);
|
|
376
|
+
reject(new Error(`Action with id ${actionId} timed out after ${responseTimeout}ms`));
|
|
372
377
|
});
|
|
373
378
|
// listen for a response before writing
|
|
374
379
|
this.subscribeToCharacteristic({
|
|
@@ -384,7 +389,7 @@ export class ReactNativeTransport {
|
|
|
384
389
|
// register action by writing
|
|
385
390
|
this.writeCharacteristic(characteristicName, payload).catch((error) => {
|
|
386
391
|
this._removePendingAction(actionId);
|
|
387
|
-
reject(error
|
|
392
|
+
reject(error);
|
|
388
393
|
});
|
|
389
394
|
}
|
|
390
395
|
else {
|
|
@@ -393,7 +398,7 @@ export class ReactNativeTransport {
|
|
|
393
398
|
resolve(null);
|
|
394
399
|
})
|
|
395
400
|
.catch((error) => {
|
|
396
|
-
reject(error
|
|
401
|
+
reject(error);
|
|
397
402
|
});
|
|
398
403
|
}
|
|
399
404
|
}));
|
package/dist/esm/neurosity.mjs
CHANGED
|
@@ -46908,7 +46908,7 @@ class ReactNativeTransport {
|
|
|
46908
46908
|
]);
|
|
46909
46909
|
if (!peripheralInfo) {
|
|
46910
46910
|
this.addLog("Could not retreive services");
|
|
46911
|
-
reject(`Could not retreive services`);
|
|
46911
|
+
reject(new Error(`Could not retreive services`));
|
|
46912
46912
|
return;
|
|
46913
46913
|
}
|
|
46914
46914
|
this.addLog(`Got service ${BLUETOOTH_PRIMARY_SERVICE_UUID_STRING}, getting characteristics...`);
|
|
@@ -46924,8 +46924,13 @@ class ReactNativeTransport {
|
|
|
46924
46924
|
]));
|
|
46925
46925
|
this.addLog(`Got characteristics.`);
|
|
46926
46926
|
if (this.platform === "android") {
|
|
46927
|
-
this.
|
|
46928
|
-
|
|
46927
|
+
yield this.BleManager.requestMTU(peripheral.id, ANDROID_MAX_MTU)
|
|
46928
|
+
.then((updatedMTU) => {
|
|
46929
|
+
this.addLog(`Successfully updated Android MTU to ${updatedMTU} bytes. Requested MTU: ${ANDROID_MAX_MTU} bytes.`);
|
|
46930
|
+
})
|
|
46931
|
+
.catch((error) => {
|
|
46932
|
+
this.addLog(`Failed to set Android MTU of ${ANDROID_MAX_MTU} bytes. Error: ${error}`);
|
|
46933
|
+
});
|
|
46929
46934
|
}
|
|
46930
46935
|
this.addLog(`Successfully connected to peripheral ${peripheral.id}`);
|
|
46931
46936
|
this.connection$.next(BLUETOOTH_CONNECTION.CONNECTED);
|
|
@@ -47000,7 +47005,7 @@ class ReactNativeTransport {
|
|
|
47000
47005
|
this.addLog(`Reading characteristic: ${characteristicName}`);
|
|
47001
47006
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
47002
47007
|
if (!characteristicUUID) {
|
|
47003
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
47008
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
47004
47009
|
}
|
|
47005
47010
|
try {
|
|
47006
47011
|
const value = yield this.BleManager.read(peripheralId, serviceUUID, characteristicUUID);
|
|
@@ -47010,7 +47015,7 @@ class ReactNativeTransport {
|
|
|
47010
47015
|
return data;
|
|
47011
47016
|
}
|
|
47012
47017
|
catch (error) {
|
|
47013
|
-
return Promise.reject(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
47018
|
+
return Promise.reject(new Error(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`));
|
|
47014
47019
|
}
|
|
47015
47020
|
});
|
|
47016
47021
|
}
|
|
@@ -47019,7 +47024,7 @@ class ReactNativeTransport {
|
|
|
47019
47024
|
this.addLog(`Writing characteristic: ${characteristicName}`);
|
|
47020
47025
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
47021
47026
|
if (!characteristicUUID) {
|
|
47022
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
47027
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
47023
47028
|
}
|
|
47024
47029
|
const encoded = encode$1(this.type, data);
|
|
47025
47030
|
yield this.BleManager.write(peripheralId, serviceUUID, characteristicUUID, encoded, REACT_NATIVE_MAX_BYTE_SIZE);
|
|
@@ -47079,7 +47084,7 @@ class ReactNativeTransport {
|
|
|
47079
47084
|
this._addPendingAction(actionId);
|
|
47080
47085
|
const timeout$$1 = timer(responseTimeout).subscribe(() => {
|
|
47081
47086
|
this._removePendingAction(actionId);
|
|
47082
|
-
reject(`Action with id ${actionId} timed out after ${responseTimeout}ms`);
|
|
47087
|
+
reject(new Error(`Action with id ${actionId} timed out after ${responseTimeout}ms`));
|
|
47083
47088
|
});
|
|
47084
47089
|
// listen for a response before writing
|
|
47085
47090
|
this.subscribeToCharacteristic({
|
|
@@ -47095,7 +47100,7 @@ class ReactNativeTransport {
|
|
|
47095
47100
|
// register action by writing
|
|
47096
47101
|
this.writeCharacteristic(characteristicName, payload).catch((error) => {
|
|
47097
47102
|
this._removePendingAction(actionId);
|
|
47098
|
-
reject(error
|
|
47103
|
+
reject(error);
|
|
47099
47104
|
});
|
|
47100
47105
|
}
|
|
47101
47106
|
else {
|
|
@@ -47104,7 +47109,7 @@ class ReactNativeTransport {
|
|
|
47104
47109
|
resolve(null);
|
|
47105
47110
|
})
|
|
47106
47111
|
.catch((error) => {
|
|
47107
|
-
reject(error
|
|
47112
|
+
reject(error);
|
|
47108
47113
|
});
|
|
47109
47114
|
}
|
|
47110
47115
|
}));
|
|
@@ -46911,7 +46911,7 @@ var Neurosity = (function (exports) {
|
|
|
46911
46911
|
]);
|
|
46912
46912
|
if (!peripheralInfo) {
|
|
46913
46913
|
this.addLog("Could not retreive services");
|
|
46914
|
-
reject(`Could not retreive services`);
|
|
46914
|
+
reject(new Error(`Could not retreive services`));
|
|
46915
46915
|
return;
|
|
46916
46916
|
}
|
|
46917
46917
|
this.addLog(`Got service ${BLUETOOTH_PRIMARY_SERVICE_UUID_STRING}, getting characteristics...`);
|
|
@@ -46927,8 +46927,13 @@ var Neurosity = (function (exports) {
|
|
|
46927
46927
|
]));
|
|
46928
46928
|
this.addLog(`Got characteristics.`);
|
|
46929
46929
|
if (this.platform === "android") {
|
|
46930
|
-
this.
|
|
46931
|
-
|
|
46930
|
+
yield this.BleManager.requestMTU(peripheral.id, ANDROID_MAX_MTU)
|
|
46931
|
+
.then((updatedMTU) => {
|
|
46932
|
+
this.addLog(`Successfully updated Android MTU to ${updatedMTU} bytes. Requested MTU: ${ANDROID_MAX_MTU} bytes.`);
|
|
46933
|
+
})
|
|
46934
|
+
.catch((error) => {
|
|
46935
|
+
this.addLog(`Failed to set Android MTU of ${ANDROID_MAX_MTU} bytes. Error: ${error}`);
|
|
46936
|
+
});
|
|
46932
46937
|
}
|
|
46933
46938
|
this.addLog(`Successfully connected to peripheral ${peripheral.id}`);
|
|
46934
46939
|
this.connection$.next(exports.BLUETOOTH_CONNECTION.CONNECTED);
|
|
@@ -47003,7 +47008,7 @@ var Neurosity = (function (exports) {
|
|
|
47003
47008
|
this.addLog(`Reading characteristic: ${characteristicName}`);
|
|
47004
47009
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
47005
47010
|
if (!characteristicUUID) {
|
|
47006
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
47011
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
47007
47012
|
}
|
|
47008
47013
|
try {
|
|
47009
47014
|
const value = yield this.BleManager.read(peripheralId, serviceUUID, characteristicUUID);
|
|
@@ -47013,7 +47018,7 @@ var Neurosity = (function (exports) {
|
|
|
47013
47018
|
return data;
|
|
47014
47019
|
}
|
|
47015
47020
|
catch (error) {
|
|
47016
|
-
return Promise.reject(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
47021
|
+
return Promise.reject(new Error(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`));
|
|
47017
47022
|
}
|
|
47018
47023
|
});
|
|
47019
47024
|
}
|
|
@@ -47022,7 +47027,7 @@ var Neurosity = (function (exports) {
|
|
|
47022
47027
|
this.addLog(`Writing characteristic: ${characteristicName}`);
|
|
47023
47028
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
47024
47029
|
if (!characteristicUUID) {
|
|
47025
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
47030
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
47026
47031
|
}
|
|
47027
47032
|
const encoded = encode$1(this.type, data);
|
|
47028
47033
|
yield this.BleManager.write(peripheralId, serviceUUID, characteristicUUID, encoded, REACT_NATIVE_MAX_BYTE_SIZE);
|
|
@@ -47082,7 +47087,7 @@ var Neurosity = (function (exports) {
|
|
|
47082
47087
|
this._addPendingAction(actionId);
|
|
47083
47088
|
const timeout$$1 = timer(responseTimeout).subscribe(() => {
|
|
47084
47089
|
this._removePendingAction(actionId);
|
|
47085
|
-
reject(`Action with id ${actionId} timed out after ${responseTimeout}ms`);
|
|
47090
|
+
reject(new Error(`Action with id ${actionId} timed out after ${responseTimeout}ms`));
|
|
47086
47091
|
});
|
|
47087
47092
|
// listen for a response before writing
|
|
47088
47093
|
this.subscribeToCharacteristic({
|
|
@@ -47098,7 +47103,7 @@ var Neurosity = (function (exports) {
|
|
|
47098
47103
|
// register action by writing
|
|
47099
47104
|
this.writeCharacteristic(characteristicName, payload).catch((error) => {
|
|
47100
47105
|
this._removePendingAction(actionId);
|
|
47101
|
-
reject(error
|
|
47106
|
+
reject(error);
|
|
47102
47107
|
});
|
|
47103
47108
|
}
|
|
47104
47109
|
else {
|
|
@@ -47107,7 +47112,7 @@ var Neurosity = (function (exports) {
|
|
|
47107
47112
|
resolve(null);
|
|
47108
47113
|
})
|
|
47109
47114
|
.catch((error) => {
|
|
47110
|
-
reject(error
|
|
47115
|
+
reject(error);
|
|
47111
47116
|
});
|
|
47112
47117
|
}
|
|
47113
47118
|
}));
|
|
@@ -649,7 +649,7 @@ var r=require("./compare"),e=function(e,o,u){return r(e,o,u)>=0};module.exports=
|
|
|
649
649
|
},{"semver/functions/gte":"A2P2"}],"ouKb":[function(require,module,exports) {
|
|
650
650
|
"use strict";function e(e){return r(e)||n(e)||t()}function t(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function n(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function r(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function i(e,t){return o(e)||a(e,t)||c()}function c(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}function a(e,t){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)){var n=[],r=!0,i=!1,c=void 0;try{for(var a,o=e[Symbol.iterator]();!(r=(a=o.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(s){i=!0,c=s}finally{try{r||null==o.return||o.return()}finally{if(i)throw c}}return n}}function o(e){if(Array.isArray(e))return e}function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function d(e,t,n){return t&&u(e.prototype,t),n&&u(e,n),e}var h=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,c){function a(e){try{s(r.next(e))}catch(t){c(t)}}function o(e){try{s(r.throw(e))}catch(t){c(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(a,o)}s((r=r.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.WebBluetoothTransport=void 0;var v=require("@neurosity/ipk"),f=require("@neurosity/ipk"),p=require("@neurosity/ipk"),l=require("@neurosity/ipk"),g=require("rxjs"),m=require("rxjs"),b=require("rxjs/operators"),E=require("rxjs/operators"),N=require("rxjs/operators"),w=require("./isWebBluetoothSupported"),O=require("../utils/create6DigitPin"),C=require("../utils/stitch"),y=require("../utils/encoding"),x=require("../types"),T=require("../constants"),k=require("../constants"),R=require("../utils/osHasBluetoothSupport"),A={autoConnect:!0},_=function(){function t(){var e=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(s(this,t),this.type=x.TRANSPORT_TYPE.WEB,this.characteristicsByName={},this.connection$=new g.BehaviorSubject(x.BLUETOOTH_CONNECTION.DISCONNECTED),this.pendingActions$=new g.BehaviorSubject([]),this.logs$=new g.ReplaySubject(10),this.onDisconnected$=this._onDisconnected().pipe((0,N.share)()),this.connectionStream$=this.connection$.asObservable().pipe((0,b.filter)(function(e){return!!e}),(0,E.distinctUntilChanged)(),(0,E.shareReplay)(1)),this._isAutoConnectEnabled$=new g.ReplaySubject(1),this.options=Object.assign(Object.assign({},A),n),!(0,w.isWebBluetoothSupported)()){throw this.addLog("Web Bluetooth is not supported"),new Error("Web Bluetooth is not supported")}this._isAutoConnectEnabled$.subscribe(function(t){e.addLog("Auto connect: ".concat(t?"enabled":"disabled"))}),this._isAutoConnectEnabled$.next(this.options.autoConnect),this.connection$.asObservable().subscribe(function(t){e.addLog("connection status is ".concat(t))}),this.onDisconnected$.subscribe(function(){e.connection$.next(x.BLUETOOTH_CONNECTION.DISCONNECTED)})}return d(t,[{key:"_getPairedDevices",value:function(){return h(this,void 0,void 0,regeneratorRuntime.mark(function e(){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,navigator.bluetooth.getDevices();case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}},e)}))}},{key:"_autoConnect",value:function(e){var t=this;return this._isAutoConnectEnabled$.pipe((0,b.switchMap)(function(n){return n?(0,g.merge)(e,t.onDisconnected$.pipe((0,b.switchMap)(function(){return e}))):m.NEVER}),(0,b.switchMap)(function(e){return(0,R.osHasBluetoothSupport)(e)?(0,g.of)(e):m.EMPTY}),(0,b.switchMap)(function(e){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,c,a,o,s,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.deviceNickname,!this.isConnected()){t.next=4;break}return this.addLog("Auto connect: ".concat(r," is already connected. Skipping auto connect.")),t.abrupt("return");case 4:return t.next=6,this._getPairedDevices().then(function(e){return[null,e]}).catch(function(e){return[e,null]});case 6:if(c=t.sent,a=i(c,2),o=a[0],s=a[1],!o){t.next=12;break}throw new Error("failed to get devices: ".concat(null!==(n=null==o?void 0:o.message)&&void 0!==n?n:o));case 12:if(this.addLog("Auto connect: found ".concat(s.length," devices ").concat(s.map(function(e){return e.name}).join(", "))),u=s.findLast(function(e){return e.name===r})){t.next=16;break}throw new Error("couldn't find selected device in the list of paired devices.");case 16:return this.addLog("Auto connect: ".concat(r," was detected and previously paired")),t.abrupt("return",u);case 18:case"end":return t.stop()}},t,this)}))}),(0,b.tap)(function(){t.connection$.next(x.BLUETOOTH_CONNECTION.SCANNING)}),(0,b.switchMap)(function(e){return S(e)}),(0,b.switchMap)(function(e){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return this.addLog("Advertisement received for ".concat(e.device.name)),t.next=3,this.getServerServiceAndCharacteristics(e.device);case 3:return t.abrupt("return",t.sent);case 4:case"end":return t.stop()}},t,this)}))}))}},{key:"enableAutoConnect",value:function(e){this._isAutoConnectEnabled$.next(e)}},{key:"addLog",value:function(e){this.logs$.next(e)}},{key:"isConnected",value:function(){return this.connection$.getValue()===x.BLUETOOTH_CONNECTION.CONNECTED}},{key:"connection",value:function(){return this.connectionStream$}},{key:"connect",value:function(e){return h(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,this.requestDevice(e);case 3:return n=t.sent,t.next=6,this.getServerServiceAndCharacteristics(n);case 6:t.next=11;break;case 8:return t.prev=8,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 11:case"end":return t.stop()}},t,this,[[0,8]])}))}},{key:"requestDevice",value:function(t){return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,c;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.prev=0,this.addLog("Requesting Bluetooth Device..."),r=p.BLUETOOTH_DEVICE_NAME_PREFIXES.map(function(e){return{namePrefix:e}}),i=t?[{name:t}]:r,n.next=6,window.navigator.bluetooth.requestDevice({filters:[].concat(e(i),[{manufacturerData:[{companyIdentifier:l.BLUETOOTH_COMPANY_IDENTIFIER_HEX}]}]),optionalServices:[v.BLUETOOTH_PRIMARY_SERVICE_UUID_HEX]});case 6:return c=n.sent,n.abrupt("return",c);case 10:return n.prev=10,n.t0=n.catch(0),n.abrupt("return",Promise.reject(n.t0));case 13:case"end":return n.stop()}},n,this,[[0,10]])}))}},{key:"getServerServiceAndCharacteristics",value:function(e){return h(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,this.device=e,this.connection$.getValue()===x.BLUETOOTH_CONNECTION.CONNECTING||this.connection$.next(x.BLUETOOTH_CONNECTION.CONNECTING),t.next=6,e.gatt.connect();case 6:return this.server=t.sent,this.addLog("Getting service..."),t.next=10,this.server.getPrimaryService(v.BLUETOOTH_PRIMARY_SERVICE_UUID_HEX);case 10:return this.service=t.sent,this.addLog("Got service ".concat(this.service.uuid,", getting characteristics...")),t.next=14,this.service.getCharacteristics();case 14:n=t.sent,this.addLog("Got characteristics"),this.characteristicsByName=Object.fromEntries(n.map(function(e){return[k.CHARACTERISTIC_UUIDS_TO_NAMES[e.uuid],e]})),this.connection$.next(x.BLUETOOTH_CONNECTION.CONNECTED),t.next=23;break;case 20:return t.prev=20,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 23:case"end":return t.stop()}},t,this,[[0,20]])}))}},{key:"_onDisconnected",value:function(){var e=this;return this.connection$.asObservable().pipe((0,b.switchMap)(function(t){return t===x.BLUETOOTH_CONNECTION.CONNECTED?L(e.device,"gattserverdisconnected"):m.NEVER}))}},{key:"disconnect",value:function(){var e,t;return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:(null===(t=null===(e=null==this?void 0:this.device)||void 0===e?void 0:e.gatt)||void 0===t?void 0:t.connected)&&this.device.gatt.disconnect();case 2:case"end":return n.stop()}},n,this)}))}},{key:"getCharacteristicByName",value:function(e){var t;return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.abrupt("return",null===(t=this.characteristicsByName)||void 0===t?void 0:t[e]);case 1:case"end":return n.stop()}},n,this)}))}},{key:"subscribeToCharacteristic",value:function(e){var t=this,n=e.characteristicName,r=e.manageNotifications,i=void 0===r||r,c=(0,g.defer)(function(){return t.getCharacteristicByName(n)}).pipe((0,b.switchMap)(function(e){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isConnected()||!i){t.next=10;break}return t.prev=1,t.next=4,e.startNotifications();case 4:this.addLog("Started notifications for ".concat(n," characteristic")),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 10:return t.abrupt("return",e);case 11:case"end":return t.stop()}},t,this,[[1,7]])}))}),(0,b.switchMap)(function(e){return L(e,"characteristicvaluechanged",function(){return h(t,void 0,void 0,regeneratorRuntime.mark(function t(){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isConnected()||!i){t.next=10;break}return t.prev=1,t.next=4,e.stopNotifications();case 4:this.addLog("Stopped notifications for ".concat(n," characteristic")),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 10:case"end":return t.stop()}},t,this,[[1,7]])}))})}),(0,b.map)(function(e){var r=e.target.value,i=(0,y.decode)(t.type,r);return t.addLog("Received chunk with buffer size of ".concat(r.byteLength," and decoded size ").concat(i.length," for ").concat(n," characteristic: \n").concat(i)),i}),(0,C.stitchChunks)({delimiter:f.BLUETOOTH_CHUNK_DELIMITER}),(0,b.map)(function(e){var r;try{return JSON.parse(e)}catch(i){return t.addLog("Failed to parse JSON for ".concat(n," characteristic. Falling back to unparsed string. ").concat(null!==(r=null==i?void 0:i.message)&&void 0!==r?r:i)),e}}));return this.connection$.pipe((0,b.switchMap)(function(e){return e===x.BLUETOOTH_CONNECTION.CONNECTED?c:m.NEVER}))}},{key:"readCharacteristic",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,c,a,o;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.prev=0,this.addLog("Reading characteristic: ".concat(e)),n.next=4,this.getCharacteristicByName(e);case 4:if(r=n.sent){n.next=8;break}return this.addLog("Did not fund ".concat(e," characteristic")),n.abrupt("return",Promise.reject("Did not find characteristic by the name: ".concat(e)));case 8:return n.next=10,r.readValue();case 10:return i=n.sent,c=i,a=(0,y.decode)(this.type,c),o=t?JSON.parse(a):a,this.addLog("Received read data from ".concat(e," characteristic: \n").concat(o)),n.abrupt("return",o);case 18:return n.prev=18,n.t0=n.catch(0),n.abrupt("return",Promise.reject("Error reading characteristic: ".concat(n.t0.message)));case 21:case"end":return n.stop()}},n,this,[[0,18]])}))}},{key:"writeCharacteristic",value:function(e,t){return h(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return this.addLog("Writing characteristic: ".concat(e)),n.next=3,this.getCharacteristicByName(e);case 3:if(r=n.sent){n.next=7;break}return this.addLog("Did not fund ".concat(e," characteristic")),n.abrupt("return",Promise.reject("Did not find characteristic by the name: ".concat(e)));case 7:return i=(0,y.encode)(this.type,t),n.next=10,r.writeValueWithResponse(i);case 10:case"end":return n.stop()}},n,this)}))}},{key:"_addPendingAction",value:function(t){var n=this.pendingActions$.getValue();this.pendingActions$.next([].concat(e(n),[t]))}},{key:"_removePendingAction",value:function(e){var t=this.pendingActions$.getValue();this.pendingActions$.next(t.filter(function(t){return t!==e}))}},{key:"_autoToggleActionNotifications",value:function(e){return h(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,i,c=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:r=!1,i=this.connection$.asObservable().pipe((0,b.switchMap)(function(e){return e===x.BLUETOOTH_CONNECTION.CONNECTED?(0,g.defer)(function(){return c.getCharacteristicByName("actions")}).pipe((0,b.switchMap)(function(e){return n=e,c.pendingActions$})):m.NEVER}),(0,b.tap)(function(e){return h(c,void 0,void 0,regeneratorRuntime.mark(function t(){var i,c,a;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!(a=!!e.length)||r){t.next=12;break}return r=!0,t.prev=3,t.next=6,n.startNotifications();case 6:this.addLog("Started notifications for [actions] characteristic"),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(3),this.addLog("Attemped to start notifications for [actions] characteristic: ".concat(null!==(i=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==i?i:t.t0));case 12:if(a||!r){t.next=23;break}return r=!1,t.prev=14,t.next=17,n.stopNotifications();case 17:this.addLog("Stopped notifications for actions characteristic"),t.next=23;break;case 20:t.prev=20,t.t1=t.catch(14),this.addLog("Attemped to stop notifications for [actions] characteristic: ".concat(null!==(c=null===t.t1||void 0===t.t1?void 0:t.t1.message)&&void 0!==c?c:t.t1));case 23:case"end":return t.stop()}},t,this,[[3,9],[14,20]])}))})),e.pipe((0,b.switchMap)(function(e){return(0,R.osHasBluetoothSupport)(e)?i:m.EMPTY})).subscribe();case 3:case"end":return t.stop()}},t,this)}))}},{key:"dispatchAction",value:function(e){var t=e.characteristicName,n=e.action;return h(this,void 0,void 0,regeneratorRuntime.mark(function e(){var r,i,c,a,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.responseRequired,i=void 0!==r&&r,c=n.responseTimeout,a=void 0===c?T.DEFAULT_ACTION_RESPONSE_TIMEOUT:c,e.abrupt("return",new Promise(function(e,r){return h(o,void 0,void 0,regeneratorRuntime.mark(function c(){var o,s,u,d=this;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:return c.next=2,this.getCharacteristicByName(t).catch(function(){r("Did not find characteristic by the name: ".concat(t))});case 2:if(c.sent){c.next=5;break}return c.abrupt("return");case 5:o=(0,O.create6DigitPin)(),s=JSON.stringify(Object.assign({actionId:o},n)),this.addLog("Dispatched action with id ".concat(o)),i&&a?(this._addPendingAction(o),u=(0,g.timer)(a).subscribe(function(){d._removePendingAction(o),r("Action with id ".concat(o," timed out after ").concat(a,"ms"))}),this.subscribeToCharacteristic({characteristicName:t,manageNotifications:!1}).pipe((0,b.filter)(function(e){return(null==e?void 0:e.actionId)===o}),(0,N.take)(1)).subscribe(function(t){u.unsubscribe(),d._removePendingAction(o),e(t)}),this.writeCharacteristic(t,s).catch(function(e){d._removePendingAction(o),r(e.message)})):this.writeCharacteristic(t,s).then(function(){e(null)}).catch(function(e){r(e.message)});case 9:case"end":return c.stop()}},c,this)}))}));case 2:case"end":return e.stop()}},e)}))}}]),t}();function L(e,t,n){var r=this;return(0,m.fromEventPattern)(function(n){e.addEventListener(t,n)},function(i){return h(r,void 0,void 0,regeneratorRuntime.mark(function r(){return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(!n){r.next=3;break}return r.next=3,n();case 3:e.removeEventListener(t,i);case 4:case"end":return r.stop()}},r)}))})}function S(e){return new m.Observable(function(t){var n=new AbortController,r=n.signal,i=e.addEventListener("advertisementreceived",function(e){n.abort(),t.next(e),t.complete()},{once:!0});try{e.watchAdvertisements({signal:r})}catch(c){t.error(c)}return function(){n.abort(),e.removeEventListener("advertisementreceived",i)}})}exports.WebBluetoothTransport=_;
|
|
651
651
|
},{"@neurosity/ipk":"ZOGh","rxjs":"Zr8e","rxjs/operators":"v3iE","./isWebBluetoothSupported":"ljun","../utils/create6DigitPin":"UDAB","../utils/stitch":"V73a","../utils/encoding":"jTvL","../types":"iwtf","../constants":"dGLb","../utils/osHasBluetoothSupport":"rH2Y"}],"FtS5":[function(require,module,exports) {
|
|
652
|
-
"use strict";function e(e){return r(e)||n(e)||t()}function t(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function n(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function r(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function o(e,t,n){return t&&c(e.prototype,t),n&&c(e,n),e}var s=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{s(r.next(e))}catch(t){a(t)}}function o(e){try{s(r.throw(e))}catch(t){a(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,o)}s((r=r.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReactNativeTransport=void 0;var u=require("@neurosity/ipk"),d=require("@neurosity/ipk"),h=require("@neurosity/ipk"),l=require("rxjs"),v=require("rxjs"),p=require("rxjs/operators"),f=require("rxjs/operators"),g=require("rxjs/operators"),m=require("../utils/create6DigitPin"),E=require("../utils/stitch"),b=require("../utils/encoding"),N=require("../types"),O=require("../constants"),C=require("../constants"),T=require("../constants"),w=require("../constants"),_=require("../utils/osHasBluetoothSupport"),y={autoConnect:!0},R=function(){function t(e){var n=this;if(a(this,t),this.type=N.TRANSPORT_TYPE.REACT_NATIVE,this.characteristicsByName={},this.connection$=new l.BehaviorSubject(N.BLUETOOTH_CONNECTION.DISCONNECTED),this.pendingActions$=new l.BehaviorSubject([]),this.logs$=new l.ReplaySubject(10),this.connectionStream$=this.connection$.asObservable().pipe((0,p.filter)(function(e){return!!e}),(0,f.distinctUntilChanged)(),(0,f.shareReplay)(1)),this._isAutoConnectEnabled$=new l.ReplaySubject(1),!e){var r="React Native transport: missing options.";throw this.addLog(r),new Error(r)}this.options=Object.assign(Object.assign({},y),e);var i=this.options,c=i.BleManager,o=i.bleManagerEmitter,s=i.platform,u=i.autoConnect;if(!c){var d="React Native option: BleManager not provided.";throw this.addLog(d),new Error(d)}if(!o){var h="React Native option: bleManagerEmitter not provided.";throw this.addLog(h),new Error(h)}if(!s){var v="React Native option: platform not provided.";throw this.addLog(v),new Error(v)}this.BleManager=c,this.bleManagerEmitter=o,this.platform=s,this._isAutoConnectEnabled$.next(u),this._isAutoConnectEnabled$.subscribe(function(e){n.addLog("Auto connect: ".concat(e?"enabled":"disabled"))}),this.bleEvents={stopScan$:this._fromEvent("BleManagerStopScan"),discoverPeripheral$:this._fromEvent("BleManagerDiscoverPeripheral"),connectPeripheral$:this._fromEvent("BleManagerConnectPeripheral"),disconnectPeripheral$:this._fromEvent("BleManagerDisconnectPeripheral"),didUpdateValueForCharacteristic$:this._fromEvent("BleManagerDidUpdateValueForCharacteristic"),didUpdateState$:this._fromEvent("BleManagerDidUpdateState")},this.onDisconnected$=this.bleEvents.disconnectPeripheral$.pipe((0,g.share)()),this.BleManager.start({showAlert:!1}).then(function(){n.addLog("BleManger started")}).catch(function(e){var t;n.addLog("BleManger failed to start. ".concat(null!==(t=null==e?void 0:e.message)&&void 0!==t?t:e))}),this.connection$.asObservable().subscribe(function(e){n.addLog("connection status is ".concat(e))}),this.onDisconnected$.subscribe(function(){n.connection$.next(N.BLUETOOTH_CONNECTION.DISCONNECTED)})}return o(t,[{key:"addLog",value:function(e){this.logs$.next(e)}},{key:"isConnected",value:function(){return this.connection$.getValue()===N.BLUETOOTH_CONNECTION.CONNECTED}},{key:"_autoConnect",value:function(e){var t=this,n=this.onDisconnected$.pipe((0,p.switchMap)(function(){return e}));return this._isAutoConnectEnabled$.pipe((0,p.switchMap)(function(t){return t?(0,l.merge)(e,n):v.NEVER}),(0,p.switchMap)(function(e){return(0,_.osHasBluetoothSupport)(e)?t.scan().pipe((0,p.switchMap)(function(t){var n=t.find(function(t){return t.name===(null==e?void 0:e.deviceNickname)});return n?(0,l.of)(n):v.NEVER}),(0,g.distinct)(function(e){return e.id}),(0,g.take)(1)):v.NEVER}),(0,p.switchMap)(function(e){return s(t,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.connect(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},t,this)}))}))}},{key:"enableAutoConnect",value:function(e){this._isAutoConnectEnabled$.next(e)}},{key:"connection",value:function(){return this.connectionStream$}},{key:"_fromEvent",value:function(e){var t=this;return(0,v.fromEventPattern)(function(n){t.bleManagerEmitter.addListener(e,n)},function(){t.bleManagerEmitter.removeAllListeners(e)}).pipe((0,g.share)())}},{key:"scan",value:function(e){var t,n,r=this,a=null!==(t=null==e?void 0:e.seconds)&&void 0!==t?t:10,c=null!==(n=null==e?void 0:e.once)&&void 0!==n&&n,o=[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING],s={},d=new v.Observable(function(e){var t;try{r.BleManager.scan(o,a,!0,s).then(function(){r.addLog("BleManger scanning ".concat(c?"once":"indefintely")),e.next()})}catch(n){r.addLog("BleManger scanning ".concat(c?"once":"indefintely"," failed. ").concat(null!==(t=null==n?void 0:n.message)&&void 0!==t?t:n)),e.error(n)}return function(){r.BleManager.stopScan()}});return(c?d:(0,l.timer)(0,1e4).pipe((0,p.switchMap)(function(){return d}))).pipe((0,p.tap)(function(){r.connection$.next(N.BLUETOOTH_CONNECTION.SCANNING)}),(0,p.takeUntil)(this.onDisconnected$),(0,p.switchMap)(function(){return r.bleEvents.discoverPeripheral$}),(0,p.filter)(function(e){var t,n,r,i=null!==(r=null!==(n=null===(t=null==e?void 0:e.advertising)||void 0===t?void 0:t.localName)&&void 0!==n?n:e.name)&&void 0!==r?r:"";return!!i&&-1!==h.BLUETOOTH_DEVICE_NAME_PREFIXES.findIndex(function(e){return i.startsWith(e)})}),(0,g.scan)(function(e,t){var n,a,c,o,s,u,d,h,l=null!==(c=null!==(a=null===(n=null==t?void 0:t.advertising)||void 0===n?void 0:n.localName)&&void 0!==a?a:t.name)&&void 0!==c?c:"",v=null===(h=null===(d=(0,b.decode)(r.type,null!==(u=null===(s=null===(o=null==t?void 0:t.advertising)||void 0===o?void 0:o.manufacturerData)||void 0===s?void 0:s.bytes)&&void 0!==u?u:[]))||void 0===d?void 0:d.slice)||void 0===h?void 0:h.call(d,2);return Object.assign(Object.assign({},e),i({},t.id,Object.assign(Object.assign({},t),{name:l,manufactureDataString:v})))},{}),(0,f.distinctUntilChanged)(function(e,t){return JSON.stringify(e)===JSON.stringify(t)}),(0,p.map)(function(e){return Object.values(e)}),(0,g.share)())}},{key:"connect",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",new Promise(function(t,r){return s(n,void 0,void 0,regeneratorRuntime.mark(function n(){var i;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(n.prev=0,e){n.next=4;break}return this.addLog("Peripheral not found"),n.abrupt("return");case 4:return this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTING),n.next=7,this.BleManager.connect(e.id);case 7:return this.addLog("Getting service..."),n.next=10,this.BleManager.retrieveServices(e.id,[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING]);case 10:if(i=n.sent){n.next=15;break}return this.addLog("Could not retreive services"),r("Could not retreive services"),n.abrupt("return");case 15:if(this.addLog("Got service ".concat(u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING,", getting characteristics...")),this.device=e,this.characteristicsByName=Object.fromEntries(i.characteristics.map(function(t){return[C.CHARACTERISTIC_UUIDS_TO_NAMES[t.characteristic.toLowerCase()],{characteristicUUID:t.characteristic,serviceUUID:t.service,peripheralId:e.id}]})),this.addLog("Got characteristics."),"android"!==this.platform){n.next=23;break}return this.addLog("Setting Android MTU to ".concat(T.ANDROID_MAX_MTU)),n.next=23,this.BleManager.requestMTU(e.id,T.ANDROID_MAX_MTU);case 23:this.addLog("Successfully connected to peripheral ".concat(e.id)),this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTED),t(),n.next=31;break;case 28:n.prev=28,n.t0=n.catch(0),r(n.t0);case 31:case"end":return n.stop()}},n,this,[[0,28]])}))}));case 1:case"end":return t.stop()}},t)}))}},{key:"disconnect",value:function(){var e;return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,!(this.isConnected()&&(null===(e=null==this?void 0:this.device)||void 0===e?void 0:e.id))){t.next=4;break}return t.next=4,this.BleManager.disconnect(this.device.id);case 4:t.next=9;break;case 6:return t.prev=6,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 9:case"end":return t.stop()}},t,this,[[0,6]])}))}},{key:"getCharacteristicByName",value:function(e){var t;if(!(e in this.characteristicsByName))throw new Error("Characteristic by name ".concat(e," is not found"));return null===(t=this.characteristicsByName)||void 0===t?void 0:t[e]}},{key:"subscribeToCharacteristic",value:function(e){var t=this,n=e.characteristicName,r=e.manageNotifications,i=void 0===r||r;return this.connection$.pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?(r=t.getCharacteristicByName(n),a=r.peripheralId,c=r.serviceUUID,o=r.characteristicUUID,(0,l.defer)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.startNotification(a,c,o);case 4:this.addLog("Started notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}).pipe((0,p.switchMap)(function(){return t.bleEvents.didUpdateValueForCharacteristic$}),(0,f.finalize)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.stopNotification(a,c,o);case 4:this.addLog("Stopped notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}),(0,p.filter)(function(e){return e.characteristic===o}),(0,p.map)(function(e){var n=e.value;return(0,b.decode)(t.type,n)}),(0,E.stitchChunks)({delimiter:d.BLUETOOTH_CHUNK_DELIMITER}),(0,p.map)(function(e){var r;try{return JSON.parse(e)}catch(i){return t.addLog("Failed to parse JSON for ".concat(n," characteristic. Falling back to unparsed string. ").concat(null!==(r=null==i?void 0:i.message)&&void 0!==r?r:i)),e}}))):v.NEVER;var r,a,c,o}))}},{key:"readCharacteristic",value:function(e){var t,n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return s(this,void 0,void 0,regeneratorRuntime.mark(function r(){var i,a,c,o,s,u,d;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(this.addLog("Reading characteristic: ".concat(e)),i=this.getCharacteristicByName(e),a=i.peripheralId,c=i.serviceUUID,o=i.characteristicUUID){r.next=4;break}return r.abrupt("return",Promise.reject("Did not find characteristic matching ".concat(e)));case 4:return r.prev=4,r.next=7,this.BleManager.read(a,c,o);case 7:return s=r.sent,u=(0,b.decode)(this.type,s),d=n?JSON.parse(u):u,this.addLog("Received read data from ".concat(e," characteristic: \n").concat(d)),r.abrupt("return",d);case 14:return r.prev=14,r.t0=r.catch(4),r.abrupt("return",Promise.reject("readCharacteristic ".concat(e," error. ").concat(null!==(t=null===r.t0||void 0===r.t0?void 0:r.t0.message)&&void 0!==t?t:r.t0)));case 17:case"end":return r.stop()}},r,this,[[4,14]])}))}},{key:"writeCharacteristic",value:function(e,t){return s(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,a,c,o;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(this.addLog("Writing characteristic: ".concat(e)),r=this.getCharacteristicByName(e),i=r.peripheralId,a=r.serviceUUID,c=r.characteristicUUID){n.next=4;break}return n.abrupt("return",Promise.reject("Did not find characteristic matching ".concat(e)));case 4:return o=(0,b.encode)(this.type,t),n.next=7,this.BleManager.write(i,a,c,o,w.REACT_NATIVE_MAX_BYTE_SIZE);case 7:case"end":return n.stop()}},n,this)}))}},{key:"_addPendingAction",value:function(t){var n=this.pendingActions$.getValue();this.pendingActions$.next([].concat(e(n),[t]))}},{key:"_removePendingAction",value:function(e){var t=this.pendingActions$.getValue();this.pendingActions$.next(t.filter(function(t){return t!==e}))}},{key:"_autoToggleActionNotifications",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,i=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:n=!1,r=this.connection$.asObservable().pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?i.pendingActions$:v.NEVER}),(0,p.tap)(function(e){return s(i,void 0,void 0,regeneratorRuntime.mark(function t(){var r,i,a,c,o,s,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(a=this.getCharacteristicByName("actions"),c=a.peripheralId,o=a.serviceUUID,s=a.characteristicUUID,!(u=!!e.length)||n){t.next=13;break}return n=!0,t.prev=4,t.next=7,this.BleManager.startNotification(c,o,s);case 7:this.addLog("Started notifications for [actions] characteristic"),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(4),this.addLog("Attemped to start notifications for [actions] characteristic: ".concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 13:if(u||!n){t.next=24;break}return n=!1,t.prev=15,t.next=18,this.BleManager.stopNotification(c,o,s);case 18:this.addLog("Stopped notifications for actions characteristic"),t.next=24;break;case 21:t.prev=21,t.t1=t.catch(15),this.addLog("Attemped to stop notifications for [actions] characteristic: ".concat(null!==(i=null===t.t1||void 0===t.t1?void 0:t.t1.message)&&void 0!==i?i:t.t1));case 24:case"end":return t.stop()}},t,this,[[4,10],[15,21]])}))})),e.pipe((0,p.switchMap)(function(e){return(0,_.osHasBluetoothSupport)(e)?r:v.EMPTY})).subscribe();case 3:case"end":return t.stop()}},t,this)}))}},{key:"dispatchAction",value:function(e){var t=e.characteristicName,n=e.action;return s(this,void 0,void 0,regeneratorRuntime.mark(function e(){var r,i,a,c,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.responseRequired,i=void 0!==r&&r,a=n.responseTimeout,c=void 0===a?O.DEFAULT_ACTION_RESPONSE_TIMEOUT:a,e.abrupt("return",new Promise(function(e,r){return s(o,void 0,void 0,regeneratorRuntime.mark(function a(){var o,s,u,d=this;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:o=(0,m.create6DigitPin)(),s=JSON.stringify(Object.assign({actionId:o},n)),this.addLog("Dispatched action with id ".concat(o)),i&&c?(this._addPendingAction(o),u=(0,l.timer)(c).subscribe(function(){d._removePendingAction(o),r("Action with id ".concat(o," timed out after ").concat(c,"ms"))}),this.subscribeToCharacteristic({characteristicName:t,manageNotifications:!1}).pipe((0,p.filter)(function(e){return(null==e?void 0:e.actionId)===o}),(0,g.take)(1)).subscribe(function(t){u.unsubscribe(),d._removePendingAction(o),e(t)}),this.writeCharacteristic(t,s).catch(function(e){d._removePendingAction(o),r(e.message)})):this.writeCharacteristic(t,s).then(function(){e(null)}).catch(function(e){r(e.message)});case 4:case"end":return a.stop()}},a,this)}))}));case 2:case"end":return e.stop()}},e)}))}}]),t}();exports.ReactNativeTransport=R;
|
|
652
|
+
"use strict";function e(e){return r(e)||n(e)||t()}function t(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function n(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function r(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function o(e,t,n){return t&&c(e.prototype,t),n&&c(e,n),e}var s=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{s(r.next(e))}catch(t){a(t)}}function o(e){try{s(r.throw(e))}catch(t){a(t)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,o)}s((r=r.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReactNativeTransport=void 0;var u=require("@neurosity/ipk"),d=require("@neurosity/ipk"),h=require("@neurosity/ipk"),l=require("rxjs"),v=require("rxjs"),p=require("rxjs/operators"),f=require("rxjs/operators"),g=require("rxjs/operators"),m=require("../utils/create6DigitPin"),E=require("../utils/stitch"),b=require("../utils/encoding"),N=require("../types"),O=require("../constants"),C=require("../constants"),w=require("../constants"),T=require("../constants"),y=require("../utils/osHasBluetoothSupport"),_={autoConnect:!0},R=function(){function t(e){var n=this;if(a(this,t),this.type=N.TRANSPORT_TYPE.REACT_NATIVE,this.characteristicsByName={},this.connection$=new l.BehaviorSubject(N.BLUETOOTH_CONNECTION.DISCONNECTED),this.pendingActions$=new l.BehaviorSubject([]),this.logs$=new l.ReplaySubject(10),this.connectionStream$=this.connection$.asObservable().pipe((0,p.filter)(function(e){return!!e}),(0,f.distinctUntilChanged)(),(0,f.shareReplay)(1)),this._isAutoConnectEnabled$=new l.ReplaySubject(1),!e){var r="React Native transport: missing options.";throw this.addLog(r),new Error(r)}this.options=Object.assign(Object.assign({},_),e);var i=this.options,c=i.BleManager,o=i.bleManagerEmitter,s=i.platform,u=i.autoConnect;if(!c){var d="React Native option: BleManager not provided.";throw this.addLog(d),new Error(d)}if(!o){var h="React Native option: bleManagerEmitter not provided.";throw this.addLog(h),new Error(h)}if(!s){var v="React Native option: platform not provided.";throw this.addLog(v),new Error(v)}this.BleManager=c,this.bleManagerEmitter=o,this.platform=s,this._isAutoConnectEnabled$.next(u),this._isAutoConnectEnabled$.subscribe(function(e){n.addLog("Auto connect: ".concat(e?"enabled":"disabled"))}),this.bleEvents={stopScan$:this._fromEvent("BleManagerStopScan"),discoverPeripheral$:this._fromEvent("BleManagerDiscoverPeripheral"),connectPeripheral$:this._fromEvent("BleManagerConnectPeripheral"),disconnectPeripheral$:this._fromEvent("BleManagerDisconnectPeripheral"),didUpdateValueForCharacteristic$:this._fromEvent("BleManagerDidUpdateValueForCharacteristic"),didUpdateState$:this._fromEvent("BleManagerDidUpdateState")},this.onDisconnected$=this.bleEvents.disconnectPeripheral$.pipe((0,g.share)()),this.BleManager.start({showAlert:!1}).then(function(){n.addLog("BleManger started")}).catch(function(e){var t;n.addLog("BleManger failed to start. ".concat(null!==(t=null==e?void 0:e.message)&&void 0!==t?t:e))}),this.connection$.asObservable().subscribe(function(e){n.addLog("connection status is ".concat(e))}),this.onDisconnected$.subscribe(function(){n.connection$.next(N.BLUETOOTH_CONNECTION.DISCONNECTED)})}return o(t,[{key:"addLog",value:function(e){this.logs$.next(e)}},{key:"isConnected",value:function(){return this.connection$.getValue()===N.BLUETOOTH_CONNECTION.CONNECTED}},{key:"_autoConnect",value:function(e){var t=this,n=this.onDisconnected$.pipe((0,p.switchMap)(function(){return e}));return this._isAutoConnectEnabled$.pipe((0,p.switchMap)(function(t){return t?(0,l.merge)(e,n):v.NEVER}),(0,p.switchMap)(function(e){return(0,y.osHasBluetoothSupport)(e)?t.scan().pipe((0,p.switchMap)(function(t){var n=t.find(function(t){return t.name===(null==e?void 0:e.deviceNickname)});return n?(0,l.of)(n):v.NEVER}),(0,g.distinct)(function(e){return e.id}),(0,g.take)(1)):v.NEVER}),(0,p.switchMap)(function(e){return s(t,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.connect(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},t,this)}))}))}},{key:"enableAutoConnect",value:function(e){this._isAutoConnectEnabled$.next(e)}},{key:"connection",value:function(){return this.connectionStream$}},{key:"_fromEvent",value:function(e){var t=this;return(0,v.fromEventPattern)(function(n){t.bleManagerEmitter.addListener(e,n)},function(){t.bleManagerEmitter.removeAllListeners(e)}).pipe((0,g.share)())}},{key:"scan",value:function(e){var t,n,r=this,a=null!==(t=null==e?void 0:e.seconds)&&void 0!==t?t:10,c=null!==(n=null==e?void 0:e.once)&&void 0!==n&&n,o=[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING],s={},d=new v.Observable(function(e){var t;try{r.BleManager.scan(o,a,!0,s).then(function(){r.addLog("BleManger scanning ".concat(c?"once":"indefintely")),e.next()})}catch(n){r.addLog("BleManger scanning ".concat(c?"once":"indefintely"," failed. ").concat(null!==(t=null==n?void 0:n.message)&&void 0!==t?t:n)),e.error(n)}return function(){r.BleManager.stopScan()}});return(c?d:(0,l.timer)(0,1e4).pipe((0,p.switchMap)(function(){return d}))).pipe((0,p.tap)(function(){r.connection$.next(N.BLUETOOTH_CONNECTION.SCANNING)}),(0,p.takeUntil)(this.onDisconnected$),(0,p.switchMap)(function(){return r.bleEvents.discoverPeripheral$}),(0,p.filter)(function(e){var t,n,r,i=null!==(r=null!==(n=null===(t=null==e?void 0:e.advertising)||void 0===t?void 0:t.localName)&&void 0!==n?n:e.name)&&void 0!==r?r:"";return!!i&&-1!==h.BLUETOOTH_DEVICE_NAME_PREFIXES.findIndex(function(e){return i.startsWith(e)})}),(0,g.scan)(function(e,t){var n,a,c,o,s,u,d,h,l=null!==(c=null!==(a=null===(n=null==t?void 0:t.advertising)||void 0===n?void 0:n.localName)&&void 0!==a?a:t.name)&&void 0!==c?c:"",v=null===(h=null===(d=(0,b.decode)(r.type,null!==(u=null===(s=null===(o=null==t?void 0:t.advertising)||void 0===o?void 0:o.manufacturerData)||void 0===s?void 0:s.bytes)&&void 0!==u?u:[]))||void 0===d?void 0:d.slice)||void 0===h?void 0:h.call(d,2);return Object.assign(Object.assign({},e),i({},t.id,Object.assign(Object.assign({},t),{name:l,manufactureDataString:v})))},{}),(0,f.distinctUntilChanged)(function(e,t){return JSON.stringify(e)===JSON.stringify(t)}),(0,p.map)(function(e){return Object.values(e)}),(0,g.share)())}},{key:"connect",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",new Promise(function(t,r){return s(n,void 0,void 0,regeneratorRuntime.mark(function n(){var i,a=this;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(n.prev=0,e){n.next=4;break}return this.addLog("Peripheral not found"),n.abrupt("return");case 4:return this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTING),n.next=7,this.BleManager.connect(e.id);case 7:return this.addLog("Getting service..."),n.next=10,this.BleManager.retrieveServices(e.id,[u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING]);case 10:if(i=n.sent){n.next=15;break}return this.addLog("Could not retreive services"),r(new Error("Could not retreive services")),n.abrupt("return");case 15:if(this.addLog("Got service ".concat(u.BLUETOOTH_PRIMARY_SERVICE_UUID_STRING,", getting characteristics...")),this.device=e,this.characteristicsByName=Object.fromEntries(i.characteristics.map(function(t){return[C.CHARACTERISTIC_UUIDS_TO_NAMES[t.characteristic.toLowerCase()],{characteristicUUID:t.characteristic,serviceUUID:t.service,peripheralId:e.id}]})),this.addLog("Got characteristics."),"android"!==this.platform){n.next=22;break}return n.next=22,this.BleManager.requestMTU(e.id,w.ANDROID_MAX_MTU).then(function(e){a.addLog("Successfully updated Android MTU to ".concat(e," bytes. Requested MTU: ").concat(w.ANDROID_MAX_MTU," bytes."))}).catch(function(e){a.addLog("Failed to set Android MTU of ".concat(w.ANDROID_MAX_MTU," bytes. Error: ").concat(e))});case 22:this.addLog("Successfully connected to peripheral ".concat(e.id)),this.connection$.next(N.BLUETOOTH_CONNECTION.CONNECTED),t(),n.next=30;break;case 27:n.prev=27,n.t0=n.catch(0),r(n.t0);case 30:case"end":return n.stop()}},n,this,[[0,27]])}))}));case 1:case"end":return t.stop()}},t)}))}},{key:"disconnect",value:function(){var e;return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,!(this.isConnected()&&(null===(e=null==this?void 0:this.device)||void 0===e?void 0:e.id))){t.next=4;break}return t.next=4,this.BleManager.disconnect(this.device.id);case 4:t.next=9;break;case 6:return t.prev=6,t.t0=t.catch(0),t.abrupt("return",Promise.reject(t.t0));case 9:case"end":return t.stop()}},t,this,[[0,6]])}))}},{key:"getCharacteristicByName",value:function(e){var t;if(!(e in this.characteristicsByName))throw new Error("Characteristic by name ".concat(e," is not found"));return null===(t=this.characteristicsByName)||void 0===t?void 0:t[e]}},{key:"subscribeToCharacteristic",value:function(e){var t=this,n=e.characteristicName,r=e.manageNotifications,i=void 0===r||r;return this.connection$.pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?(r=t.getCharacteristicByName(n),a=r.peripheralId,c=r.serviceUUID,o=r.characteristicUUID,(0,l.defer)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.startNotification(a,c,o);case 4:this.addLog("Started notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}).pipe((0,p.switchMap)(function(){return t.bleEvents.didUpdateValueForCharacteristic$}),(0,f.finalize)(function(){return s(t,void 0,void 0,regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!i){e.next=10;break}return e.prev=1,e.next=4,this.BleManager.stopNotification(a,c,o);case 4:this.addLog("Stopped notifications for ".concat(n," characteristic")),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(1),this.addLog("Attemped to stop notifications for ".concat(n," characteristic: ").concat(null!==(t=null===e.t0||void 0===e.t0?void 0:e.t0.message)&&void 0!==t?t:e.t0));case 10:case"end":return e.stop()}},e,this,[[1,7]])}))}),(0,p.filter)(function(e){return e.characteristic===o}),(0,p.map)(function(e){var n=e.value;return(0,b.decode)(t.type,n)}),(0,E.stitchChunks)({delimiter:d.BLUETOOTH_CHUNK_DELIMITER}),(0,p.map)(function(e){var r;try{return JSON.parse(e)}catch(i){return t.addLog("Failed to parse JSON for ".concat(n," characteristic. Falling back to unparsed string. ").concat(null!==(r=null==i?void 0:i.message)&&void 0!==r?r:i)),e}}))):v.NEVER;var r,a,c,o}))}},{key:"readCharacteristic",value:function(e){var t,n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return s(this,void 0,void 0,regeneratorRuntime.mark(function r(){var i,a,c,o,s,u,d;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:if(this.addLog("Reading characteristic: ".concat(e)),i=this.getCharacteristicByName(e),a=i.peripheralId,c=i.serviceUUID,o=i.characteristicUUID){r.next=4;break}return r.abrupt("return",Promise.reject(new Error("Did not find characteristic matching ".concat(e))));case 4:return r.prev=4,r.next=7,this.BleManager.read(a,c,o);case 7:return s=r.sent,u=(0,b.decode)(this.type,s),d=n?JSON.parse(u):u,this.addLog("Received read data from ".concat(e," characteristic: \n").concat(d)),r.abrupt("return",d);case 14:return r.prev=14,r.t0=r.catch(4),r.abrupt("return",Promise.reject(new Error("readCharacteristic ".concat(e," error. ").concat(null!==(t=null===r.t0||void 0===r.t0?void 0:r.t0.message)&&void 0!==t?t:r.t0))));case 17:case"end":return r.stop()}},r,this,[[4,14]])}))}},{key:"writeCharacteristic",value:function(e,t){return s(this,void 0,void 0,regeneratorRuntime.mark(function n(){var r,i,a,c,o;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:if(this.addLog("Writing characteristic: ".concat(e)),r=this.getCharacteristicByName(e),i=r.peripheralId,a=r.serviceUUID,c=r.characteristicUUID){n.next=4;break}return n.abrupt("return",Promise.reject(new Error("Did not find characteristic matching ".concat(e))));case 4:return o=(0,b.encode)(this.type,t),n.next=7,this.BleManager.write(i,a,c,o,T.REACT_NATIVE_MAX_BYTE_SIZE);case 7:case"end":return n.stop()}},n,this)}))}},{key:"_addPendingAction",value:function(t){var n=this.pendingActions$.getValue();this.pendingActions$.next([].concat(e(n),[t]))}},{key:"_removePendingAction",value:function(e){var t=this.pendingActions$.getValue();this.pendingActions$.next(t.filter(function(t){return t!==e}))}},{key:"_autoToggleActionNotifications",value:function(e){return s(this,void 0,void 0,regeneratorRuntime.mark(function t(){var n,r,i=this;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:n=!1,r=this.connection$.asObservable().pipe((0,p.switchMap)(function(e){return e===N.BLUETOOTH_CONNECTION.CONNECTED?i.pendingActions$:v.NEVER}),(0,p.tap)(function(e){return s(i,void 0,void 0,regeneratorRuntime.mark(function t(){var r,i,a,c,o,s,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(a=this.getCharacteristicByName("actions"),c=a.peripheralId,o=a.serviceUUID,s=a.characteristicUUID,!(u=!!e.length)||n){t.next=13;break}return n=!0,t.prev=4,t.next=7,this.BleManager.startNotification(c,o,s);case 7:this.addLog("Started notifications for [actions] characteristic"),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(4),this.addLog("Attemped to start notifications for [actions] characteristic: ".concat(null!==(r=null===t.t0||void 0===t.t0?void 0:t.t0.message)&&void 0!==r?r:t.t0));case 13:if(u||!n){t.next=24;break}return n=!1,t.prev=15,t.next=18,this.BleManager.stopNotification(c,o,s);case 18:this.addLog("Stopped notifications for actions characteristic"),t.next=24;break;case 21:t.prev=21,t.t1=t.catch(15),this.addLog("Attemped to stop notifications for [actions] characteristic: ".concat(null!==(i=null===t.t1||void 0===t.t1?void 0:t.t1.message)&&void 0!==i?i:t.t1));case 24:case"end":return t.stop()}},t,this,[[4,10],[15,21]])}))})),e.pipe((0,p.switchMap)(function(e){return(0,y.osHasBluetoothSupport)(e)?r:v.EMPTY})).subscribe();case 3:case"end":return t.stop()}},t,this)}))}},{key:"dispatchAction",value:function(e){var t=e.characteristicName,n=e.action;return s(this,void 0,void 0,regeneratorRuntime.mark(function e(){var r,i,a,c,o=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.responseRequired,i=void 0!==r&&r,a=n.responseTimeout,c=void 0===a?O.DEFAULT_ACTION_RESPONSE_TIMEOUT:a,e.abrupt("return",new Promise(function(e,r){return s(o,void 0,void 0,regeneratorRuntime.mark(function a(){var o,s,u,d=this;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:o=(0,m.create6DigitPin)(),s=JSON.stringify(Object.assign({actionId:o},n)),this.addLog("Dispatched action with id ".concat(o)),i&&c?(this._addPendingAction(o),u=(0,l.timer)(c).subscribe(function(){d._removePendingAction(o),r(new Error("Action with id ".concat(o," timed out after ").concat(c,"ms")))}),this.subscribeToCharacteristic({characteristicName:t,manageNotifications:!1}).pipe((0,p.filter)(function(e){return(null==e?void 0:e.actionId)===o}),(0,g.take)(1)).subscribe(function(t){u.unsubscribe(),d._removePendingAction(o),e(t)}),this.writeCharacteristic(t,s).catch(function(e){d._removePendingAction(o),r(e)})):this.writeCharacteristic(t,s).then(function(){e(null)}).catch(function(e){r(e)});case 4:case"end":return a.stop()}},a,this)}))}));case 2:case"end":return e.stop()}},e)}))}}]),t}();exports.ReactNativeTransport=R;
|
|
653
653
|
},{"@neurosity/ipk":"ZOGh","rxjs":"Zr8e","rxjs/operators":"v3iE","../utils/create6DigitPin":"UDAB","../utils/stitch":"V73a","../utils/encoding":"jTvL","../types":"iwtf","../constants":"dGLb","../utils/osHasBluetoothSupport":"rH2Y"}],"Oj1i":[function(require,module,exports) {
|
|
654
654
|
"use strict";function n(n,e,t){return e in n?Object.defineProperty(n,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[e]=t,n}Object.defineProperty(exports,"__esModule",{value:!0}),exports.epoch=exports.bufferToEpoch=exports.addInfo=void 0;var e=require("rxjs"),t=require("rxjs/operators"),r="data",o=256,i=function(n){return n instanceof Object&&n===Object(n)},a=function(n){return"function"==typeof n},u=function(n){return function(e){var t;return Object.assign(Object.assign({},n),{info:Object.assign(Object.assign({},null!==(t=null==n?void 0:n.info)&&void 0!==t?t:{}),e||{})})}},f=function(n){return(0,e.pipe)((0,t.map)(function(e){if(!i(e)||!i(n)&&!a(n))return e;var t=a(n)?n(e):n;return u(e)(t)}))};exports.addInfo=f;var c=function(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:r;return n[0][e].map(function(t,r){return n.map(function(n){return n[e][r]})})},p=function(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},a=i.samplingRate,u=void 0===a?o:a,f=i.dataProp,p=void 0===f?r:f;return(0,e.pipe)((0,t.map)(function(e){var t;return n(t={},p,c(e,p)),n(t,"info",Object.assign(Object.assign({},e[0]&&e[0].info?e[0].info:{}),{startTime:e[0].timestamp,samplingRate:e[0].info&&e[0].info.samplingRate?e[0].info.samplingRate:u})),t}))};exports.bufferToEpoch=p;var s=function(n){var o=n.duration,i=n.interval,a=n.samplingRate,u=n.dataProp,f=void 0===u?r:u;return(0,e.pipe)((0,t.bufferCount)(i),(0,t.scan)(function(n,e){return n.concat(e).slice(n.length<o?0:-o)}),(0,t.filter)(function(n){return n.length===o}),(0,exports.bufferToEpoch)({samplingRate:a,dataProp:f}))};exports.epoch=s;
|
|
655
655
|
},{"rxjs":"Zr8e","rxjs/operators":"v3iE"}],"WTrV":[function(require,module,exports) {
|
|
@@ -46908,7 +46908,7 @@ class ReactNativeTransport {
|
|
|
46908
46908
|
]);
|
|
46909
46909
|
if (!peripheralInfo) {
|
|
46910
46910
|
this.addLog("Could not retreive services");
|
|
46911
|
-
reject(`Could not retreive services`);
|
|
46911
|
+
reject(new Error(`Could not retreive services`));
|
|
46912
46912
|
return;
|
|
46913
46913
|
}
|
|
46914
46914
|
this.addLog(`Got service ${BLUETOOTH_PRIMARY_SERVICE_UUID_STRING}, getting characteristics...`);
|
|
@@ -46924,8 +46924,13 @@ class ReactNativeTransport {
|
|
|
46924
46924
|
]));
|
|
46925
46925
|
this.addLog(`Got characteristics.`);
|
|
46926
46926
|
if (this.platform === "android") {
|
|
46927
|
-
this.
|
|
46928
|
-
|
|
46927
|
+
yield this.BleManager.requestMTU(peripheral.id, ANDROID_MAX_MTU)
|
|
46928
|
+
.then((updatedMTU) => {
|
|
46929
|
+
this.addLog(`Successfully updated Android MTU to ${updatedMTU} bytes. Requested MTU: ${ANDROID_MAX_MTU} bytes.`);
|
|
46930
|
+
})
|
|
46931
|
+
.catch((error) => {
|
|
46932
|
+
this.addLog(`Failed to set Android MTU of ${ANDROID_MAX_MTU} bytes. Error: ${error}`);
|
|
46933
|
+
});
|
|
46929
46934
|
}
|
|
46930
46935
|
this.addLog(`Successfully connected to peripheral ${peripheral.id}`);
|
|
46931
46936
|
this.connection$.next(BLUETOOTH_CONNECTION.CONNECTED);
|
|
@@ -47000,7 +47005,7 @@ class ReactNativeTransport {
|
|
|
47000
47005
|
this.addLog(`Reading characteristic: ${characteristicName}`);
|
|
47001
47006
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
47002
47007
|
if (!characteristicUUID) {
|
|
47003
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
47008
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
47004
47009
|
}
|
|
47005
47010
|
try {
|
|
47006
47011
|
const value = yield this.BleManager.read(peripheralId, serviceUUID, characteristicUUID);
|
|
@@ -47010,7 +47015,7 @@ class ReactNativeTransport {
|
|
|
47010
47015
|
return data;
|
|
47011
47016
|
}
|
|
47012
47017
|
catch (error) {
|
|
47013
|
-
return Promise.reject(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
47018
|
+
return Promise.reject(new Error(`readCharacteristic ${characteristicName} error. ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`));
|
|
47014
47019
|
}
|
|
47015
47020
|
});
|
|
47016
47021
|
}
|
|
@@ -47019,7 +47024,7 @@ class ReactNativeTransport {
|
|
|
47019
47024
|
this.addLog(`Writing characteristic: ${characteristicName}`);
|
|
47020
47025
|
const { peripheralId, serviceUUID, characteristicUUID } = this.getCharacteristicByName(characteristicName);
|
|
47021
47026
|
if (!characteristicUUID) {
|
|
47022
|
-
return Promise.reject(`Did not find characteristic matching ${characteristicName}`);
|
|
47027
|
+
return Promise.reject(new Error(`Did not find characteristic matching ${characteristicName}`));
|
|
47023
47028
|
}
|
|
47024
47029
|
const encoded = encode$1(this.type, data);
|
|
47025
47030
|
yield this.BleManager.write(peripheralId, serviceUUID, characteristicUUID, encoded, REACT_NATIVE_MAX_BYTE_SIZE);
|
|
@@ -47079,7 +47084,7 @@ class ReactNativeTransport {
|
|
|
47079
47084
|
this._addPendingAction(actionId);
|
|
47080
47085
|
const timeout$$1 = timer(responseTimeout).subscribe(() => {
|
|
47081
47086
|
this._removePendingAction(actionId);
|
|
47082
|
-
reject(`Action with id ${actionId} timed out after ${responseTimeout}ms`);
|
|
47087
|
+
reject(new Error(`Action with id ${actionId} timed out after ${responseTimeout}ms`));
|
|
47083
47088
|
});
|
|
47084
47089
|
// listen for a response before writing
|
|
47085
47090
|
this.subscribeToCharacteristic({
|
|
@@ -47095,7 +47100,7 @@ class ReactNativeTransport {
|
|
|
47095
47100
|
// register action by writing
|
|
47096
47101
|
this.writeCharacteristic(characteristicName, payload).catch((error) => {
|
|
47097
47102
|
this._removePendingAction(actionId);
|
|
47098
|
-
reject(error
|
|
47103
|
+
reject(error);
|
|
47099
47104
|
});
|
|
47100
47105
|
}
|
|
47101
47106
|
else {
|
|
@@ -47104,7 +47109,7 @@ class ReactNativeTransport {
|
|
|
47104
47109
|
resolve(null);
|
|
47105
47110
|
})
|
|
47106
47111
|
.catch((error) => {
|
|
47107
|
-
reject(error
|
|
47112
|
+
reject(error);
|
|
47108
47113
|
});
|
|
47109
47114
|
}
|
|
47110
47115
|
}));
|
package/package.json
CHANGED
package/CHANGELOG.md
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# v5.0.0
|
|
2
|
-
|
|
3
|
-
- FEAT: Auto & manual device selection via `neurosity.selectDevice(...)` method
|
|
4
|
-
- FEAT: new methods: `neurosity.getDevices()` and `neurosity.onDeviceChange()`
|
|
5
|
-
- FIX: #46 Notion sends 1 packet of data even though it is asleep
|
|
6
|
-
- FIX: only send timesync actions if and when device is online
|
|
7
|
-
|
|
8
|
-
# v4.0.0
|
|
9
|
-
|
|
10
|
-
- Added types
|
|
11
|
-
- Improved documentation (Reference)
|
|
12
|
-
|
|
13
|
-
# v3.10.0
|
|
14
|
-
|
|
15
|
-
- Added periodic device status update call while subscribed to status
|
|
16
|
-
|
|
17
|
-
# v3.9.0
|
|
18
|
-
|
|
19
|
-
- Added clients connections and remove them when offline
|
|
20
|
-
|
|
21
|
-
# v3.8.1
|
|
22
|
-
|
|
23
|
-
### Package Updates
|
|
24
|
-
|
|
25
|
-
- Update IPK to v1.7.0
|