incyclist-devices 2.3.0-beta.11 → 2.3.0-beta.12
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.
|
@@ -42,7 +42,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
42
42
|
getBinding(): BleBinding;
|
|
43
43
|
protected autoConnect(): void;
|
|
44
44
|
connect(reconnect?: boolean): Promise<boolean>;
|
|
45
|
-
disconnect(
|
|
45
|
+
disconnect(connectionLost?: boolean): Promise<boolean>;
|
|
46
46
|
isConnected(): boolean;
|
|
47
47
|
registerConnected(peripheral: IBlePeripheral): void;
|
|
48
48
|
protected isConnecting(): boolean;
|
|
@@ -59,6 +59,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
59
59
|
protected reconnect(): Promise<void>;
|
|
60
60
|
protected startPeripheralScan(retry?: boolean): Promise<void>;
|
|
61
61
|
protected stopPeripheralScan(): Promise<void>;
|
|
62
|
+
protected emitDisconnectAllPeripherals(): void;
|
|
63
|
+
protected disconnectAllPeripherals(): Promise<void>;
|
|
62
64
|
protected isDiscovering(): boolean;
|
|
63
65
|
protected discoverPeripherals(): Promise<void>;
|
|
64
66
|
protected onPeripheralFound(peripheral: BleRawPeripheral): void;
|
|
@@ -110,21 +110,24 @@ class BleInterface extends events_1.default {
|
|
|
110
110
|
return success;
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
|
-
disconnect(
|
|
113
|
+
disconnect(connectionLost) {
|
|
114
114
|
return __awaiter(this, void 0, void 0, function* () {
|
|
115
115
|
var _a;
|
|
116
116
|
if (!this.getBinding()) {
|
|
117
117
|
return false;
|
|
118
118
|
}
|
|
119
|
-
if (!this.isConnected() && !
|
|
119
|
+
if (!this.isConnected() && !connectionLost)
|
|
120
120
|
return true;
|
|
121
|
-
if (!
|
|
121
|
+
if (!connectionLost)
|
|
122
122
|
this.logEvent({ message: 'disconnect request' });
|
|
123
123
|
this.emit('disconnect-request');
|
|
124
124
|
yield this.stopPeripheralScan();
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
if (connectionLost) {
|
|
126
|
+
this.emitDisconnectAllPeripherals();
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
yield this.disconnectAllPeripherals();
|
|
130
|
+
}
|
|
128
131
|
if (this.isConnecting())
|
|
129
132
|
yield ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.stop());
|
|
130
133
|
this.getBinding().removeAllListeners();
|
|
@@ -274,6 +277,20 @@ class BleInterface extends events_1.default {
|
|
|
274
277
|
});
|
|
275
278
|
});
|
|
276
279
|
}
|
|
280
|
+
emitDisconnectAllPeripherals() {
|
|
281
|
+
this.connectedPeripherals.forEach(p => {
|
|
282
|
+
const peripheral = p.getPeripheral();
|
|
283
|
+
peripheral.emit('disconnect');
|
|
284
|
+
});
|
|
285
|
+
this.connectedPeripherals = [];
|
|
286
|
+
}
|
|
287
|
+
disconnectAllPeripherals() {
|
|
288
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
289
|
+
const promises = this.connectedPeripherals.map(p => p.disconnect());
|
|
290
|
+
yield Promise.allSettled(promises);
|
|
291
|
+
this.connectedPeripherals = [];
|
|
292
|
+
});
|
|
293
|
+
}
|
|
277
294
|
isDiscovering() {
|
|
278
295
|
var _a;
|
|
279
296
|
return ((_a = this.discoverTask) === null || _a === void 0 ? void 0 : _a.isRunning()) === true;
|
|
@@ -369,11 +386,11 @@ class BleInterface extends events_1.default {
|
|
|
369
386
|
try {
|
|
370
387
|
peripheral.on('error', (err) => {
|
|
371
388
|
peripheral.removeAllListeners();
|
|
372
|
-
this.logEvent({ message: '
|
|
389
|
+
this.logEvent({ message: 'peripheral error', error: err.message });
|
|
373
390
|
});
|
|
374
391
|
peripheral.on('disconnect', () => {
|
|
375
392
|
peripheral.removeAllListeners();
|
|
376
|
-
this.logEvent({ message: '
|
|
393
|
+
this.logEvent({ message: 'peripheral disconnected' });
|
|
377
394
|
});
|
|
378
395
|
yield peripheral.connectAsync();
|
|
379
396
|
if (peripheral.discoverServicesAsync !== undefined) {
|
|
@@ -11,14 +11,17 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
11
11
|
callback: (data: Buffer) => void;
|
|
12
12
|
}>;
|
|
13
13
|
protected disconnecting: boolean;
|
|
14
|
+
protected onErrorHandler: any;
|
|
14
15
|
constructor(announcement: BlePeripheralAnnouncement);
|
|
15
16
|
get services(): BleService[];
|
|
16
|
-
|
|
17
|
+
getPeripheral(): BleRawPeripheral;
|
|
17
18
|
connect(): Promise<boolean>;
|
|
18
|
-
disconnect(): Promise<boolean>;
|
|
19
|
+
disconnect(connectionLost?: boolean): Promise<boolean>;
|
|
19
20
|
isConnected(): boolean;
|
|
20
21
|
isConnecting(): boolean;
|
|
21
22
|
onDisconnect(callback: () => void): void;
|
|
23
|
+
protected onPeripheralDisconnect(): Promise<void>;
|
|
24
|
+
protected onPeripheralError(err: Error): void;
|
|
22
25
|
discoverServices(): Promise<string[]>;
|
|
23
26
|
discoverCharacteristics(serviceUUID: string): Promise<BleCharacteristic[]>;
|
|
24
27
|
subscribe(characteristicUUID: string, callback: (characteristicUuid: string, data: Buffer) => void): Promise<boolean>;
|
|
@@ -27,7 +30,7 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
27
30
|
discoverAllCharacteristics(): Promise<string[]>;
|
|
28
31
|
discoverSomeCharacteristics(characteristics: string[]): Promise<string[]>;
|
|
29
32
|
subscribeAll(callback: (characteristicUuid: string, data: Buffer) => void): Promise<boolean>;
|
|
30
|
-
unsubscribeAll(): Promise<
|
|
33
|
+
unsubscribeAll(connectionLost?: boolean): Promise<void>;
|
|
31
34
|
read(characteristicUUID: string): Promise<Buffer>;
|
|
32
35
|
write(characteristicUUID: string, data: Buffer, options?: BleWriteProps): Promise<Buffer>;
|
|
33
36
|
protected getRawCharacteristic(uuid: string): BleRawCharacteristic;
|
|
@@ -19,6 +19,7 @@ class BlePeripheral {
|
|
|
19
19
|
this.characteristics = {};
|
|
20
20
|
this.subscribed = [];
|
|
21
21
|
this.disconnecting = false;
|
|
22
|
+
this.onErrorHandler = this.onPeripheralError.bind(this);
|
|
22
23
|
this.ble = interface_1.BleInterface.getInstance();
|
|
23
24
|
}
|
|
24
25
|
get services() {
|
|
@@ -31,28 +32,37 @@ class BlePeripheral {
|
|
|
31
32
|
return __awaiter(this, void 0, void 0, function* () {
|
|
32
33
|
if (this.isConnected())
|
|
33
34
|
return true;
|
|
34
|
-
|
|
35
|
+
const peripheral = this.getPeripheral();
|
|
36
|
+
yield peripheral.connectAsync();
|
|
35
37
|
this.ble.registerConnected(this);
|
|
38
|
+
peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
|
|
39
|
+
peripheral.on('error', this.onErrorHandler);
|
|
36
40
|
this.connected = true;
|
|
37
41
|
return this.connected;
|
|
38
42
|
});
|
|
39
43
|
}
|
|
40
44
|
disconnect() {
|
|
41
|
-
return __awaiter(this,
|
|
45
|
+
return __awaiter(this, arguments, void 0, function* (connectionLost = false) {
|
|
42
46
|
this.disconnecting = true;
|
|
43
47
|
if (!this.isConnected())
|
|
44
48
|
return true;
|
|
45
|
-
yield this.unsubscribeAll();
|
|
49
|
+
yield this.unsubscribeAll(connectionLost);
|
|
46
50
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
47
51
|
const c = this.characteristics[uuid];
|
|
48
52
|
c.removeAllListeners();
|
|
49
53
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
const peripheral = this.getPeripheral();
|
|
55
|
+
if (peripheral) {
|
|
56
|
+
if (!connectionLost) {
|
|
57
|
+
if (!peripheral.disconnectAsync) {
|
|
58
|
+
peripheral.disconnectAsync = () => {
|
|
59
|
+
return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
yield this.getPeripheral().disconnectAsync();
|
|
63
|
+
}
|
|
64
|
+
peripheral.removeAllListeners();
|
|
54
65
|
}
|
|
55
|
-
yield this.getPeripheral().disconnectAsync();
|
|
56
66
|
this.connected = false;
|
|
57
67
|
this.disconnecting = false;
|
|
58
68
|
return !this.connected;
|
|
@@ -67,6 +77,20 @@ class BlePeripheral {
|
|
|
67
77
|
onDisconnect(callback) {
|
|
68
78
|
this.onDisconnectHandler = callback;
|
|
69
79
|
}
|
|
80
|
+
onPeripheralDisconnect() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
this.logEvent({ message: 'disconnect' });
|
|
83
|
+
try {
|
|
84
|
+
yield this.disconnect(true);
|
|
85
|
+
}
|
|
86
|
+
catch (_a) { }
|
|
87
|
+
if (this.onDisconnectHandler)
|
|
88
|
+
this.onDisconnectHandler();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
onPeripheralError(err) {
|
|
92
|
+
this.logEvent({ message: 'peripheral error', error: err.message });
|
|
93
|
+
}
|
|
70
94
|
discoverServices() {
|
|
71
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
72
96
|
if (this.getPeripheral().discoverServicesAsync) {
|
|
@@ -184,16 +208,16 @@ class BlePeripheral {
|
|
|
184
208
|
yield this.discoverAllCharacteristics();
|
|
185
209
|
}
|
|
186
210
|
const retry = [];
|
|
187
|
-
for (
|
|
188
|
-
const c = this.getRawCharacteristic(
|
|
211
|
+
for (const element of characteristics) {
|
|
212
|
+
const c = this.getRawCharacteristic(element);
|
|
189
213
|
if (c === null || c === void 0 ? void 0 : c.properties.includes('notify')) {
|
|
190
214
|
const success = yield this.subscribe(c.uuid, callback);
|
|
191
215
|
if (!success)
|
|
192
216
|
retry.push(c);
|
|
193
217
|
}
|
|
194
218
|
}
|
|
195
|
-
for (
|
|
196
|
-
const c =
|
|
219
|
+
for (const element of retry) {
|
|
220
|
+
const c = element;
|
|
197
221
|
yield this.subscribe(c.uuid, callback);
|
|
198
222
|
}
|
|
199
223
|
return true;
|
|
@@ -247,13 +271,16 @@ class BlePeripheral {
|
|
|
247
271
|
});
|
|
248
272
|
}
|
|
249
273
|
unsubscribeAll() {
|
|
250
|
-
return __awaiter(this,
|
|
274
|
+
return __awaiter(this, arguments, void 0, function* (connectionLost = false) {
|
|
275
|
+
if (connectionLost) {
|
|
276
|
+
this.subscribed = [];
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
251
279
|
const promises = [];
|
|
252
280
|
this.subscribed.forEach(d => {
|
|
253
281
|
promises.push(this.unsubscribe(d.uuid));
|
|
254
282
|
});
|
|
255
283
|
yield Promise.allSettled(promises);
|
|
256
|
-
return true;
|
|
257
284
|
});
|
|
258
285
|
}
|
|
259
286
|
read(characteristicUUID) {
|
|
@@ -305,6 +332,8 @@ class BlePeripheral {
|
|
|
305
332
|
return this.characteristics[(0, utils_1.beautifyUUID)(uuid)];
|
|
306
333
|
}
|
|
307
334
|
logEvent(event) {
|
|
335
|
+
var _a;
|
|
336
|
+
event.peripheral = (_a = this.announcement) === null || _a === void 0 ? void 0 : _a.name;
|
|
308
337
|
this.ble.logEvent(event);
|
|
309
338
|
}
|
|
310
339
|
}
|