@iotize/device-com-ble.cordova 3.7.2 → 4.0.0-alpha.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/CHANGELOG.md +143 -0
- package/package.json +7 -21
- package/plugin.cjs +35 -0
- package/plugin.js +35 -0
- package/plugin.xml +8 -3
- package/src/android/build.gradle +2 -11
- package/src/android/libs/iotize-device-com-ble-1.0.0-alpha.9.aar +0 -0
- package/{lib → src/lib}/ble-com-protocol.d.ts +20 -20
- package/{lib → src/lib}/cordova-ble-error.d.ts +29 -29
- package/{lib → src/lib}/cordova-interface.d.ts +5 -5
- package/{lib → src/lib}/cordova-service-adapter.d.ts +58 -58
- package/{lib → src/lib}/definitions.d.ts +43 -43
- package/{lib → src/lib}/iotize-ble-cordova-plugin.d.ts +27 -27
- package/src/lib/logger.d.ts +2 -0
- package/{lib → src/lib}/scanner.d.ts +55 -55
- package/{lib → src/lib}/utility.d.ts +6 -6
- package/src/plugin.d.ts +8 -0
- package/src/windows/iotize-ble-com.js +11 -11
- package/LICENSE +0 -23
- package/README.md +0 -174
- package/bundles/iotize-device-com-ble.cordova.umd.js +0 -1418
- package/bundles/iotize-device-com-ble.cordova.umd.js.map +0 -1
- package/bundles/iotize-device-com-ble.cordova.umd.min.js +0 -2
- package/bundles/iotize-device-com-ble.cordova.umd.min.js.map +0 -1
- package/esm2015/iotize-device-com-ble.cordova.js +0 -6
- package/esm2015/iotize-device-com-ble.cordova.js.map +0 -1
- package/esm2015/iotize-device-com-ble.cordova.metadata.json +0 -1
- package/esm2015/iotize-device-com-ble.cordova.ngsummary.json +0 -1
- package/esm2015/lib/ble-com-protocol.js +0 -76
- package/esm2015/lib/ble-com-protocol.js.map +0 -1
- package/esm2015/lib/ble-com-protocol.metadata.json +0 -1
- package/esm2015/lib/ble-com-protocol.ngsummary.json +0 -1
- package/esm2015/lib/cordova-ble-error.js +0 -36
- package/esm2015/lib/cordova-ble-error.js.map +0 -1
- package/esm2015/lib/cordova-ble-error.metadata.json +0 -1
- package/esm2015/lib/cordova-ble-error.ngsummary.json +0 -1
- package/esm2015/lib/cordova-interface.js +0 -2
- package/esm2015/lib/cordova-interface.js.map +0 -1
- package/esm2015/lib/cordova-interface.metadata.json +0 -1
- package/esm2015/lib/cordova-interface.ngsummary.json +0 -1
- package/esm2015/lib/cordova-service-adapter.js +0 -214
- package/esm2015/lib/cordova-service-adapter.js.map +0 -1
- package/esm2015/lib/cordova-service-adapter.metadata.json +0 -1
- package/esm2015/lib/cordova-service-adapter.ngsummary.json +0 -1
- package/esm2015/lib/definitions.js +0 -2
- package/esm2015/lib/definitions.js.map +0 -1
- package/esm2015/lib/definitions.metadata.json +0 -1
- package/esm2015/lib/definitions.ngsummary.json +0 -1
- package/esm2015/lib/iotize-ble-cordova-plugin.js +0 -170
- package/esm2015/lib/iotize-ble-cordova-plugin.js.map +0 -1
- package/esm2015/lib/iotize-ble-cordova-plugin.metadata.json +0 -1
- package/esm2015/lib/iotize-ble-cordova-plugin.ngsummary.json +0 -1
- package/esm2015/lib/logger.js +0 -3
- package/esm2015/lib/logger.js.map +0 -1
- package/esm2015/lib/logger.metadata.json +0 -1
- package/esm2015/lib/logger.ngsummary.json +0 -1
- package/esm2015/lib/scanner.js +0 -151
- package/esm2015/lib/scanner.js.map +0 -1
- package/esm2015/lib/scanner.metadata.json +0 -1
- package/esm2015/lib/scanner.ngsummary.json +0 -1
- package/esm2015/lib/utility.js +0 -26
- package/esm2015/lib/utility.js.map +0 -1
- package/esm2015/lib/utility.metadata.json +0 -1
- package/esm2015/lib/utility.ngsummary.json +0 -1
- package/esm2015/public_api.js +0 -5
- package/esm2015/public_api.js.map +0 -1
- package/esm2015/public_api.metadata.json +0 -1
- package/esm2015/public_api.ngsummary.json +0 -1
- package/fesm2015/iotize-device-com-ble.cordova.js +0 -660
- package/fesm2015/iotize-device-com-ble.cordova.js.map +0 -1
- package/iotize-device-com-ble.cordova.d.ts +0 -6
- package/iotize-device-com-ble.cordova.metadata.json +0 -1
- package/lib/logger.d.ts +0 -1
- package/public_api.d.ts +0 -5
- package/src/android/.gradle/4.8.1/fileChanges/last-build.bin +0 -0
- package/src/android/.gradle/4.8.1/fileHashes/fileHashes.bin +0 -0
- package/src/android/.gradle/4.8.1/fileHashes/fileHashes.lock +0 -0
- package/src/android/.gradle/6.8.2/fileChanges/last-build.bin +0 -0
- package/src/android/.gradle/6.8.2/fileHashes/fileHashes.lock +0 -0
- package/src/android/.gradle/6.8.2/gc.properties +0 -0
- package/src/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/src/android/.gradle/buildOutputCleanup/cache.properties +0 -2
- package/src/android/.gradle/checksums/checksums.lock +0 -0
- package/src/android/.gradle/configuration-cache/gc.properties +0 -0
- package/src/android/.gradle/vcs-1/gc.properties +0 -0
- package/src/android/.gradle/vcsWorkingDirs/gc.properties +0 -0
- package/src/android/.idea/android.iml +0 -9
- package/src/android/.idea/gradle.xml +0 -16
- package/src/android/.idea/modules.xml +0 -8
- package/src/android/.idea/vcs.xml +0 -6
- package/www/plugin.js +0 -2
- package/www/plugin.js.LICENSE.txt +0 -14
|
@@ -1,660 +0,0 @@
|
|
|
1
|
-
import { ConnectionState } from '@iotize/tap/protocol/api';
|
|
2
|
-
import { QueueComProtocol } from '@iotize/tap/protocol/core';
|
|
3
|
-
import { defer, throwError, BehaviorSubject, Subject } from 'rxjs';
|
|
4
|
-
import { tap, filter, first, catchError, distinctUntilChanged, shareReplay, map, switchMap } from 'rxjs/operators';
|
|
5
|
-
import { CodeError } from '@iotize/common/error';
|
|
6
|
-
import { createDebugger } from '@iotize/common/debug';
|
|
7
|
-
import { BleComError } from '@iotize/tap/protocol/ble/common';
|
|
8
|
-
import { hexStringToBuffer as hexStringToBuffer$1, bufferToHexString } from '@iotize/common/byte-converter';
|
|
9
|
-
import { safeEnumValue } from '@iotize/common/utility';
|
|
10
|
-
|
|
11
|
-
class CordovaBLEError extends CodeError {
|
|
12
|
-
static invalidErrorResult(errObject) {
|
|
13
|
-
return new CordovaBLEError(`Internal error: ble cordova plugin returned an unexpected error object`, CordovaBLEError.Code.InternalError);
|
|
14
|
-
}
|
|
15
|
-
static invalidErrorCode(errObject) {
|
|
16
|
-
return new CordovaBLEError(`Internal error: ble cordova plugin returned an invalid error code ${errObject.code} with message ${errObject.message}`, CordovaBLEError.Code.InternalError);
|
|
17
|
-
}
|
|
18
|
-
static invalidNativeCallResult(result, cause) {
|
|
19
|
-
return new CordovaBLEError(`Internal error: native call returned an invalid data type. ${cause.message}`, CordovaBLEError.Code.InternalError);
|
|
20
|
-
}
|
|
21
|
-
static isValidErrorCode(code) {
|
|
22
|
-
return code in CordovaBLEError.Code;
|
|
23
|
-
}
|
|
24
|
-
static iotizeBLEMissing() {
|
|
25
|
-
return new CordovaBLEError(`iotizeBLE global variable does not exist. Are you sure app is running inside a Cordova application ?`, CordovaBLEError.Code.InternalError);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/* istanbul ignore next */
|
|
29
|
-
(function (CordovaBLEError) {
|
|
30
|
-
let Code;
|
|
31
|
-
(function (Code) {
|
|
32
|
-
Code["InternalError"] = "CordovaBLEErrorInternalError";
|
|
33
|
-
Code["IllegalArgument"] = "CordovaBLEErrorIllegalArgument";
|
|
34
|
-
Code["BLENotAvailable"] = "CordovaBLEErrorBLENotAvailable";
|
|
35
|
-
Code["InvalidMacAddress"] = "InvalidMacAddressInvalidMacAddress";
|
|
36
|
-
Code["ConnectionError"] = "CordovaBLEErrorConnectionError";
|
|
37
|
-
Code["RequestError"] = "CordovaBLEErrorRequestError";
|
|
38
|
-
Code["LocationServiceDisabled"] = "CordovaBLEErrorLocationServiceDisabled";
|
|
39
|
-
Code["DisconnectError"] = "CordovaBLEErrorDisconnectError";
|
|
40
|
-
Code["IllegalAction"] = "CordovaBLEErrorIllegalAction";
|
|
41
|
-
Code["NotConnectedError"] = "CordovaBLEErrorNotConnectedError";
|
|
42
|
-
Code["StatusCodeError"] = "CordovaBLEErrorStatusCodeError";
|
|
43
|
-
})(Code = CordovaBLEError.Code || (CordovaBLEError.Code = {}));
|
|
44
|
-
})(CordovaBLEError || (CordovaBLEError = {}));
|
|
45
|
-
|
|
46
|
-
const debug = createDebugger(`@iotize/device-com-ble.cordova`);
|
|
47
|
-
|
|
48
|
-
function getIoTizeBleCordovaPlugin() {
|
|
49
|
-
if (typeof iotizeBLE !== "object") {
|
|
50
|
-
throw CordovaBLEError.iotizeBLEMissing();
|
|
51
|
-
}
|
|
52
|
-
return iotizeBLE;
|
|
53
|
-
}
|
|
54
|
-
function getScanRecordsFromBytes(bytes) {
|
|
55
|
-
//LTV encoding
|
|
56
|
-
const records = [];
|
|
57
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
58
|
-
//first byte is length
|
|
59
|
-
let size = bytes[i];
|
|
60
|
-
if (size == 0) {
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
let type = bytes[i + 1];
|
|
64
|
-
records.push({
|
|
65
|
-
type,
|
|
66
|
-
data: bytes.slice(i + 2, i + size + 1)
|
|
67
|
-
});
|
|
68
|
-
i += size;
|
|
69
|
-
}
|
|
70
|
-
return records;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
var __awaiter$3 = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
74
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
75
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
76
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
77
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
78
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
79
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
class BLEComProtocol extends QueueComProtocol {
|
|
83
|
-
constructor(deviceId, comProtocolOptions, cordovaInterfaceOverwrite) {
|
|
84
|
-
super();
|
|
85
|
-
this.deviceId = deviceId;
|
|
86
|
-
this.cordovaInterfaceOverwrite = cordovaInterfaceOverwrite;
|
|
87
|
-
if (comProtocolOptions) {
|
|
88
|
-
this.options = comProtocolOptions;
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
this.options.connect.timeout = 60000;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Lazy reference to iotizeBLE.
|
|
96
|
-
* We don't want to reference iotizeBLE in constructor as it may be referenced
|
|
97
|
-
* before cordova plugin is loaded
|
|
98
|
-
*/
|
|
99
|
-
get pluginInterface() {
|
|
100
|
-
return this.cordovaInterfaceOverwrite || getIoTizeBleCordovaPlugin();
|
|
101
|
-
}
|
|
102
|
-
_connect(options) {
|
|
103
|
-
debug('_connect', options);
|
|
104
|
-
return this.pluginInterface
|
|
105
|
-
.connectAndDiscoverTapServices(this.deviceId)
|
|
106
|
-
.pipe(tap((state) => {
|
|
107
|
-
this.setConnectionState(state);
|
|
108
|
-
}), filter((state) => state === ConnectionState.CONNECTED), first());
|
|
109
|
-
}
|
|
110
|
-
_disconnect(options) {
|
|
111
|
-
debug('_disconnect', options);
|
|
112
|
-
return defer(() => this.pluginInterface.disConnect(this.deviceId));
|
|
113
|
-
}
|
|
114
|
-
write(data) {
|
|
115
|
-
return __awaiter$3(this, void 0, void 0, function* () {
|
|
116
|
-
throw new Error('Method not implemented.');
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
read() {
|
|
120
|
-
return __awaiter$3(this, void 0, void 0, function* () {
|
|
121
|
-
throw new Error('Method not implemented.');
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
send(data, options) {
|
|
125
|
-
return defer(() => this.pluginInterface.send(this.deviceId, data)).pipe(catchError((err) => {
|
|
126
|
-
var _a;
|
|
127
|
-
if (((_a = err) === null || _a === void 0 ? void 0 : _a.code) ===
|
|
128
|
-
CordovaBLEError.Code.NotConnectedError) {
|
|
129
|
-
this.setConnectionState(ConnectionState.DISCONNECTED);
|
|
130
|
-
}
|
|
131
|
-
return throwError(err);
|
|
132
|
-
}));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
var __awaiter$2 = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
137
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
138
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
139
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
140
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
141
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
142
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
class CordovaPeripheralAdapter {
|
|
146
|
-
constructor(deviceId, cordovaPlugin = getIoTizeBleCordovaPlugin()) {
|
|
147
|
-
this.deviceId = deviceId;
|
|
148
|
-
this.cordovaPlugin = cordovaPlugin;
|
|
149
|
-
this._stateChange = new BehaviorSubject(ConnectionState.DISCONNECTED);
|
|
150
|
-
}
|
|
151
|
-
get stateChange() {
|
|
152
|
-
return this._stateChange.asObservable().pipe(distinctUntilChanged());
|
|
153
|
-
}
|
|
154
|
-
get name() {
|
|
155
|
-
// TODO device name ?
|
|
156
|
-
return this.id;
|
|
157
|
-
}
|
|
158
|
-
get id() {
|
|
159
|
-
return this.deviceId;
|
|
160
|
-
}
|
|
161
|
-
get state() {
|
|
162
|
-
return ConnectionState[this._stateChange.value];
|
|
163
|
-
}
|
|
164
|
-
discoverServices(serviceUUIDs) {
|
|
165
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
166
|
-
this.serviceListCache = yield this.cordovaPlugin.discoverServices(this.deviceId);
|
|
167
|
-
const result = {};
|
|
168
|
-
if (serviceUUIDs) {
|
|
169
|
-
for (const uuid of serviceUUIDs) {
|
|
170
|
-
const servicDefinition = this.serviceListCache.find((def) => def.uuid === uuid);
|
|
171
|
-
if (servicDefinition) {
|
|
172
|
-
result[uuid] = new CordovaServiceAdapter(servicDefinition, this);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
for (const serviceDefinition of this.serviceListCache) {
|
|
178
|
-
result[serviceDefinition.uuid] = new CordovaServiceAdapter(serviceDefinition, this);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return result;
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
connect() {
|
|
185
|
-
var _a;
|
|
186
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
187
|
-
(_a = this.connectionStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
188
|
-
const connectObservable = this.cordovaPlugin
|
|
189
|
-
.connect(this.deviceId)
|
|
190
|
-
.pipe(shareReplay());
|
|
191
|
-
this.connectionStateSubscription = connectObservable.subscribe((state) => {
|
|
192
|
-
this._stateChange.next(state);
|
|
193
|
-
});
|
|
194
|
-
yield connectObservable
|
|
195
|
-
.pipe(filter((state) => state === ConnectionState.CONNECTED), first())
|
|
196
|
-
.toPromise();
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
disconnect() {
|
|
200
|
-
var _a;
|
|
201
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
202
|
-
yield this.cordovaPlugin.disConnect(this.deviceId);
|
|
203
|
-
this._stateChange.next(ConnectionState.DISCONNECTED);
|
|
204
|
-
(_a = this.connectionStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
close() {
|
|
208
|
-
var _a;
|
|
209
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
210
|
-
yield this.cordovaPlugin.close(this.deviceId);
|
|
211
|
-
this._stateChange.next(ConnectionState.DISCONNECTED);
|
|
212
|
-
(_a = this.connectionStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
getService(uuid) {
|
|
216
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
217
|
-
if (!this.serviceListCache) {
|
|
218
|
-
yield this.discoverServices([uuid]);
|
|
219
|
-
}
|
|
220
|
-
const serviceDefinition = this.serviceListCache.find((s) => s.uuid === uuid);
|
|
221
|
-
if (!serviceDefinition) {
|
|
222
|
-
throw BleComError.serviceNotFound(uuid);
|
|
223
|
-
}
|
|
224
|
-
return new CordovaServiceAdapter(serviceDefinition, this);
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
class CordovaServiceAdapter {
|
|
229
|
-
constructor(config, peripheral) {
|
|
230
|
-
this.config = config;
|
|
231
|
-
this.peripheral = peripheral;
|
|
232
|
-
}
|
|
233
|
-
get uuid() {
|
|
234
|
-
return this.config.uuid;
|
|
235
|
-
}
|
|
236
|
-
getCharacteristic(charcUUID) {
|
|
237
|
-
var _a;
|
|
238
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
239
|
-
const characteristicDescription = (_a = this.config.characteristics) === null || _a === void 0 ? void 0 : _a.find((c) => c.uuid === charcUUID);
|
|
240
|
-
if (!characteristicDescription) {
|
|
241
|
-
throw BleComError.charcacteristicNotFound(charcUUID);
|
|
242
|
-
}
|
|
243
|
-
return new CordovaCharacteristicAdapter(this, characteristicDescription);
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
getCharacteristics() {
|
|
247
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
248
|
-
if (!this.config.characteristics) {
|
|
249
|
-
return [];
|
|
250
|
-
}
|
|
251
|
-
return this.config.characteristics.map((c) => new CordovaCharacteristicAdapter(this, c));
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
class CordovaCharacteristicAdapter {
|
|
256
|
-
constructor(service, config) {
|
|
257
|
-
this.service = service;
|
|
258
|
-
this.config = config;
|
|
259
|
-
}
|
|
260
|
-
get uuid() {
|
|
261
|
-
return this.config.uuid;
|
|
262
|
-
}
|
|
263
|
-
get properties() {
|
|
264
|
-
return this.config.properties;
|
|
265
|
-
}
|
|
266
|
-
get data() {
|
|
267
|
-
return this.setupDataStreamIfRequired().pipe(map((data) => ({
|
|
268
|
-
data,
|
|
269
|
-
isNotification: true,
|
|
270
|
-
})));
|
|
271
|
-
}
|
|
272
|
-
get deviceId() {
|
|
273
|
-
return this.service.peripheral.deviceId;
|
|
274
|
-
}
|
|
275
|
-
get pluginInterface() {
|
|
276
|
-
return this.service.peripheral.cordovaPlugin;
|
|
277
|
-
}
|
|
278
|
-
read() {
|
|
279
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
280
|
-
return this.pluginInterface.characteristicReadValue(this.deviceId, this.service.uuid, this.uuid);
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
getDescriptor() {
|
|
284
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
285
|
-
throw new Error(`Not implemented yet`);
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
getDescriptors() {
|
|
289
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
290
|
-
if (!this.config.descriptors) {
|
|
291
|
-
return [];
|
|
292
|
-
}
|
|
293
|
-
return this.config.descriptors.map((descriptor) => new CordovaDescriptorAdapter(descriptor, this));
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
write(data, writeWithoutResponse) {
|
|
297
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
298
|
-
if (writeWithoutResponse) {
|
|
299
|
-
yield this.pluginInterface.characteristicWriteWithoutResponse(this.deviceId, this.service.uuid, this.uuid, data);
|
|
300
|
-
}
|
|
301
|
-
else {
|
|
302
|
-
yield this.pluginInterface.characteristicWrite(this.deviceId, this.service.uuid, this.uuid, data);
|
|
303
|
-
}
|
|
304
|
-
return data;
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
enableNotifications(enabled) {
|
|
308
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
309
|
-
if (enabled) {
|
|
310
|
-
this.setupDataStreamIfRequired();
|
|
311
|
-
yield this.pluginInterface.characteristicStartNotification(this.deviceId, this.service.uuid, this.uuid);
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
yield this.pluginInterface.characteristicStopNotification(this.deviceId, this.service.uuid, this.uuid);
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
setupDataStreamIfRequired() {
|
|
319
|
-
if (!this._dataStream) {
|
|
320
|
-
this._dataStream = this.pluginInterface.characteristicChanged(this.deviceId, this.service.uuid, this.uuid);
|
|
321
|
-
}
|
|
322
|
-
return this._dataStream;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
class CordovaDescriptorAdapter {
|
|
326
|
-
constructor(config, characteristic) {
|
|
327
|
-
this.config = config;
|
|
328
|
-
this.characteristic = characteristic;
|
|
329
|
-
}
|
|
330
|
-
get uuid() {
|
|
331
|
-
return this.config.uuid;
|
|
332
|
-
}
|
|
333
|
-
readValue() {
|
|
334
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
335
|
-
throw new Error(`Reading descriptor value is not implemented yet`);
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
writeValue(data) {
|
|
339
|
-
return __awaiter$2(this, void 0, void 0, function* () {
|
|
340
|
-
throw new Error(`Writing descriptor value is not implemented yet`);
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
//
|
|
346
|
-
// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
|
|
347
|
-
//
|
|
348
|
-
// scanner.ts
|
|
349
|
-
// device-com-ble.cordova BLE Cordova Plugin
|
|
350
|
-
//
|
|
351
|
-
var __awaiter$1 = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
352
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
353
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
354
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
355
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
356
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
357
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
358
|
-
});
|
|
359
|
-
};
|
|
360
|
-
/**
|
|
361
|
-
* BLE Tap scanner for cordova apps
|
|
362
|
-
*/
|
|
363
|
-
class BLEScanner {
|
|
364
|
-
/**
|
|
365
|
-
*
|
|
366
|
-
* @param cordovaInterfaceOverwrite overwrite cordova interface. Used for testing
|
|
367
|
-
*/
|
|
368
|
-
constructor(cordovaInterfaceOverwrite,
|
|
369
|
-
/**
|
|
370
|
-
* Request device options used to filter scan results
|
|
371
|
-
*/
|
|
372
|
-
requestDeviceOptions) {
|
|
373
|
-
this.cordovaInterfaceOverwrite = cordovaInterfaceOverwrite;
|
|
374
|
-
this.requestDeviceOptions = requestDeviceOptions;
|
|
375
|
-
this._results = new BehaviorSubject([]);
|
|
376
|
-
this._scanning$ = new BehaviorSubject(false);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Lazy reference to iotizeBLE.
|
|
380
|
-
* We don't want to reference iotizeBLE in constructor as it may be referenced
|
|
381
|
-
* before cordova plugin is loaded
|
|
382
|
-
*/
|
|
383
|
-
get cordovaInterface() {
|
|
384
|
-
return this.cordovaInterfaceOverwrite || getIoTizeBleCordovaPlugin();
|
|
385
|
-
}
|
|
386
|
-
get scanning() {
|
|
387
|
-
return this._scanning$.asObservable();
|
|
388
|
-
}
|
|
389
|
-
get isScanning() {
|
|
390
|
-
return this._scanning$.value;
|
|
391
|
-
}
|
|
392
|
-
/**
|
|
393
|
-
* Gets the observable on the devices$ Subject
|
|
394
|
-
* @return
|
|
395
|
-
*/
|
|
396
|
-
get results() {
|
|
397
|
-
return this._results.asObservable();
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Launches the scan for BLE devices
|
|
401
|
-
* Throws if BLE is not available
|
|
402
|
-
*/
|
|
403
|
-
start(options) {
|
|
404
|
-
var _a;
|
|
405
|
-
return __awaiter$1(this, void 0, void 0, function* () {
|
|
406
|
-
(_a = this.scanSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
407
|
-
this.scanSubscription = undefined;
|
|
408
|
-
this.clearResults();
|
|
409
|
-
const isAvailable = yield this.checkAvailable();
|
|
410
|
-
if (!isAvailable) {
|
|
411
|
-
throw BleComError.bleNotAvailable(`BLE is not available. Make sure that BLE is enabled on your device`);
|
|
412
|
-
}
|
|
413
|
-
debug("Start Scanning ...");
|
|
414
|
-
this._scanning$.next(true);
|
|
415
|
-
return new Promise((resolve, reject) => {
|
|
416
|
-
this.scanSubscription = this.cordovaInterface
|
|
417
|
-
.startScan(this.requestDeviceOptions)
|
|
418
|
-
.subscribe((result) => {
|
|
419
|
-
if (result == "Ok") {
|
|
420
|
-
resolve();
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
this.addOrRefreshDevice(result);
|
|
424
|
-
}, (error) => {
|
|
425
|
-
debug("Start scan failed with error: ", error);
|
|
426
|
-
this.cordovaInterface
|
|
427
|
-
.getLastError()
|
|
428
|
-
.then((lasterror) => {
|
|
429
|
-
debug("Last BLE error " + lasterror);
|
|
430
|
-
})
|
|
431
|
-
.catch((err) => {
|
|
432
|
-
debug("Cannot get last BLE error: ", err);
|
|
433
|
-
});
|
|
434
|
-
reject(error);
|
|
435
|
-
this._scanning$.next(false);
|
|
436
|
-
});
|
|
437
|
-
});
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
*
|
|
442
|
-
*/
|
|
443
|
-
stop() {
|
|
444
|
-
var _a;
|
|
445
|
-
return __awaiter$1(this, void 0, void 0, function* () {
|
|
446
|
-
debug("Stop Scanning ...");
|
|
447
|
-
try {
|
|
448
|
-
(_a = this.scanSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
449
|
-
this.scanSubscription = undefined;
|
|
450
|
-
yield this.cordovaInterface.stopScan();
|
|
451
|
-
this._scanning$.next(false);
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
catch (err) {
|
|
455
|
-
this._scanning$.next(false);
|
|
456
|
-
throw err;
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Returns true if this scanner is available
|
|
462
|
-
*/
|
|
463
|
-
checkAvailable() {
|
|
464
|
-
return this.cordovaInterface.checkAvailable();
|
|
465
|
-
}
|
|
466
|
-
get devices() {
|
|
467
|
-
return this._results.value;
|
|
468
|
-
}
|
|
469
|
-
clearResults() {
|
|
470
|
-
this._results.next([]);
|
|
471
|
-
}
|
|
472
|
-
addOrRefreshDevice(newDevice) {
|
|
473
|
-
let storedDeviceIndex = this.devices.findIndex((entry) => entry.address == newDevice.address);
|
|
474
|
-
if (storedDeviceIndex >= 0) {
|
|
475
|
-
let storedDevice = this.devices[storedDeviceIndex];
|
|
476
|
-
if (storedDevice.name != newDevice.name ||
|
|
477
|
-
storedDevice.rssi != newDevice.rssi) {
|
|
478
|
-
debug(`Updating device at index ${storedDeviceIndex}, name=${storedDevice.name} with rssi=${storedDevice.rssi}`);
|
|
479
|
-
this.devices[storedDeviceIndex] = newDevice;
|
|
480
|
-
// this.devices = [...this.devices];
|
|
481
|
-
this._results.next(this.devices);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
else {
|
|
485
|
-
debug(`Adding new device name=${newDevice.name} with rssi=${newDevice.rssi}`);
|
|
486
|
-
this.devices.push(newDevice);
|
|
487
|
-
this._results.next(this.devices);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
493
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
494
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
495
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
496
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
497
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
498
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
499
|
-
});
|
|
500
|
-
};
|
|
501
|
-
function hexStringToBuffer(str) {
|
|
502
|
-
try {
|
|
503
|
-
return hexStringToBuffer$1(str);
|
|
504
|
-
}
|
|
505
|
-
catch (err) {
|
|
506
|
-
throw CordovaBLEError.invalidNativeCallResult(str, err);
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
class IoTizeBleCordovaPlugin {
|
|
510
|
-
checkAvailable() {
|
|
511
|
-
return this.execSingleResult("checkAvailable", []);
|
|
512
|
-
}
|
|
513
|
-
requestEnableBle() {
|
|
514
|
-
return this.execSingleResult("enable", []);
|
|
515
|
-
}
|
|
516
|
-
startScan(requestDeviceOptions) {
|
|
517
|
-
return this.execMultipleResult("startScan", [
|
|
518
|
-
JSON.stringify(requestDeviceOptions),
|
|
519
|
-
]);
|
|
520
|
-
}
|
|
521
|
-
stopScan() {
|
|
522
|
-
return this.execSingleResult("stopScan", []);
|
|
523
|
-
}
|
|
524
|
-
connect(deviceId, enableBleIfNot = true) {
|
|
525
|
-
return this.askBleEnable(enableBleIfNot).pipe(switchMap(() => {
|
|
526
|
-
return this.execMultipleResult("connect", [deviceId]).pipe(map((state) => {
|
|
527
|
-
if (!(state in ConnectionState)) {
|
|
528
|
-
console.warn(`Plugin native code returned an invalid connection state: "${state}".`);
|
|
529
|
-
}
|
|
530
|
-
return safeEnumValue(ConnectionState, state);
|
|
531
|
-
}));
|
|
532
|
-
}));
|
|
533
|
-
}
|
|
534
|
-
askBleEnable(askEnable) {
|
|
535
|
-
return defer(() => __awaiter(this, void 0, void 0, function* () {
|
|
536
|
-
if (askEnable) {
|
|
537
|
-
if (!(yield this.checkAvailable())) {
|
|
538
|
-
yield this.requestEnableBle();
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
}));
|
|
542
|
-
}
|
|
543
|
-
requestMTU(deviceId, mtu) {
|
|
544
|
-
return this.execSingleResult("requestMTU", [deviceId, mtu]);
|
|
545
|
-
}
|
|
546
|
-
connectAndDiscoverTapServices(deviceId, enableBleIfNot = true) {
|
|
547
|
-
return this.askBleEnable(enableBleIfNot).pipe(switchMap(() => {
|
|
548
|
-
return this.execMultipleResult("connectAndDiscoverTapServices", [deviceId]).pipe(map((state) => {
|
|
549
|
-
if (!(state in ConnectionState)) {
|
|
550
|
-
console.warn(`Plugin native code returned an invalid connection state: "${state}".`);
|
|
551
|
-
}
|
|
552
|
-
return safeEnumValue(ConnectionState, state);
|
|
553
|
-
}));
|
|
554
|
-
}));
|
|
555
|
-
}
|
|
556
|
-
disConnect(deviceId) {
|
|
557
|
-
return this.execSingleResult("disConnect", [deviceId]);
|
|
558
|
-
}
|
|
559
|
-
close(deviceId) {
|
|
560
|
-
return this.execSingleResult("close", [deviceId]);
|
|
561
|
-
}
|
|
562
|
-
isConnected(deviceId) {
|
|
563
|
-
return this.execSingleResult("isConnected", [deviceId]);
|
|
564
|
-
}
|
|
565
|
-
send(deviceId, data) {
|
|
566
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
567
|
-
const hexString = yield this.execSingleResult("sendRequest", [
|
|
568
|
-
deviceId,
|
|
569
|
-
bufferToHexString(data),
|
|
570
|
-
]);
|
|
571
|
-
return hexStringToBuffer(hexString);
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
getLastError() {
|
|
575
|
-
return this.execSingleResult("getLastError", []);
|
|
576
|
-
}
|
|
577
|
-
characteristicStartNotification(deviceId, serviceId, characId) {
|
|
578
|
-
return this.execSingleResult("characteristicStartNotification", [
|
|
579
|
-
deviceId,
|
|
580
|
-
serviceId,
|
|
581
|
-
characId,
|
|
582
|
-
]);
|
|
583
|
-
}
|
|
584
|
-
characteristicChanged(deviceId, serviceId, characId) {
|
|
585
|
-
return this.execMultipleResult("characteristicChanged", [
|
|
586
|
-
deviceId,
|
|
587
|
-
serviceId,
|
|
588
|
-
characId,
|
|
589
|
-
]).pipe(map((hexString) => {
|
|
590
|
-
return hexStringToBuffer(hexString);
|
|
591
|
-
}));
|
|
592
|
-
}
|
|
593
|
-
characteristicStopNotification(deviceId, serviceId, characId) {
|
|
594
|
-
return this.execSingleResult("characteristicStopNotification", [
|
|
595
|
-
deviceId,
|
|
596
|
-
serviceId,
|
|
597
|
-
characId,
|
|
598
|
-
]);
|
|
599
|
-
}
|
|
600
|
-
characteristicReadValue(deviceId, serviceId, characId) {
|
|
601
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
602
|
-
const hexString = yield this.execSingleResult("characteristicRead", [deviceId, serviceId, characId]);
|
|
603
|
-
return hexStringToBuffer(hexString);
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
characteristicWrite(deviceId, serviceId, characId, data) {
|
|
607
|
-
return this.execSingleResult("characteristicWrite", [
|
|
608
|
-
deviceId,
|
|
609
|
-
serviceId,
|
|
610
|
-
characId,
|
|
611
|
-
bufferToHexString(data),
|
|
612
|
-
]);
|
|
613
|
-
}
|
|
614
|
-
characteristicWriteWithoutResponse(deviceId, serviceId, characId, data) {
|
|
615
|
-
return this.execSingleResult("characteristicWriteWithoutResponse", [
|
|
616
|
-
deviceId,
|
|
617
|
-
serviceId,
|
|
618
|
-
characId,
|
|
619
|
-
bufferToHexString(data),
|
|
620
|
-
]);
|
|
621
|
-
}
|
|
622
|
-
discoverServices(deviceId) {
|
|
623
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
624
|
-
const services = yield this.execSingleResult("discoverServices", [deviceId]);
|
|
625
|
-
return services;
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
execSingleResult(methodName, args) {
|
|
629
|
-
return new Promise((resolve, reject) => {
|
|
630
|
-
cordova.exec(resolve, (errObject) => {
|
|
631
|
-
reject(objectErrorToError(errObject));
|
|
632
|
-
}, "BLECom", methodName, args);
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
execMultipleResult(methodName, args) {
|
|
636
|
-
const subject = new Subject();
|
|
637
|
-
cordova.exec((data) => {
|
|
638
|
-
subject.next(data);
|
|
639
|
-
}, (err) => {
|
|
640
|
-
subject.error(objectErrorToError(err));
|
|
641
|
-
}, "BLECom", methodName, args);
|
|
642
|
-
return subject.asObservable();
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
function objectErrorToError(errObject) {
|
|
646
|
-
if (typeof errObject !== "object" || !errObject.code || !errObject.message) {
|
|
647
|
-
return CordovaBLEError.invalidErrorResult(errObject);
|
|
648
|
-
}
|
|
649
|
-
if (!CordovaBLEError.isValidErrorCode(errObject.code)) {
|
|
650
|
-
return CordovaBLEError.invalidErrorCode(errObject);
|
|
651
|
-
}
|
|
652
|
-
return new CordovaBLEError(errObject.message, CordovaBLEError.Code[errObject.code]);
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
/**
|
|
656
|
-
* Generated bundle index. Do not edit.
|
|
657
|
-
*/
|
|
658
|
-
|
|
659
|
-
export { BLEComProtocol, BLEScanner, CordovaCharacteristicAdapter, CordovaPeripheralAdapter, CordovaServiceAdapter, getIoTizeBleCordovaPlugin, getScanRecordsFromBytes, IoTizeBleCordovaPlugin as ɵc };
|
|
660
|
-
//# sourceMappingURL=iotize-device-com-ble.cordova.js.map
|