incyclist-devices 2.3.8 → 2.3.10
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/lib/ble/base/interface.d.ts +12 -5
- package/lib/ble/base/interface.js +100 -54
- package/lib/ble/base/peripheral.d.ts +4 -1
- package/lib/ble/base/peripheral.js +72 -24
- package/package.json +1 -1
|
@@ -27,13 +27,18 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
27
27
|
protected expectedServices: string[];
|
|
28
28
|
protected matching: string[];
|
|
29
29
|
protected connectTask: InteruptableTask<TaskState, boolean>;
|
|
30
|
+
protected disconnectTask: InteruptableTask<TaskState, boolean>;
|
|
30
31
|
protected scanTask: InteruptableTask<TaskState, void>;
|
|
31
32
|
protected discoverTask: InteruptableTask<TaskState, void>;
|
|
32
33
|
protected onDiscovered: (peripheral: BleRawPeripheral) => void;
|
|
33
34
|
protected instanceId: number;
|
|
34
|
-
protected connectedPeripherals:
|
|
35
|
-
|
|
35
|
+
protected connectedPeripherals: {
|
|
36
|
+
id: string;
|
|
37
|
+
peripheral: IBlePeripheral;
|
|
38
|
+
}[];
|
|
36
39
|
protected emitted: BlePeripheralAnnouncement[];
|
|
40
|
+
protected confirmedBleState: BleInterfaceState;
|
|
41
|
+
protected currentBleState: BleInterfaceState;
|
|
37
42
|
static getInstance(props?: InterfaceProps): BleInterface;
|
|
38
43
|
protected constructor(props: InterfaceProps);
|
|
39
44
|
setProps(props: InterfaceProps): void;
|
|
@@ -46,8 +51,10 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
46
51
|
connect(reconnect?: boolean): Promise<boolean>;
|
|
47
52
|
disconnect(connectionLost?: boolean): Promise<boolean>;
|
|
48
53
|
isConnected(): boolean;
|
|
49
|
-
registerConnected(peripheral: IBlePeripheral): void;
|
|
54
|
+
registerConnected(peripheral: IBlePeripheral, id: string): void;
|
|
55
|
+
unregisterConnected(id: string): void;
|
|
50
56
|
protected isConnecting(): boolean;
|
|
57
|
+
protected isDisconnecting(): boolean;
|
|
51
58
|
scan(props: BleScanProps): Promise<DeviceSettings[]>;
|
|
52
59
|
stopScan(): Promise<boolean>;
|
|
53
60
|
onScanDone(): DeviceSettings[];
|
|
@@ -90,9 +97,9 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
90
97
|
protected connectBle(): Promise<boolean>;
|
|
91
98
|
protected waitForBleConnected(): Promise<boolean>;
|
|
92
99
|
protected onError(err: Error): void;
|
|
93
|
-
protected onConnected(): void
|
|
100
|
+
protected onConnected(): Promise<void>;
|
|
94
101
|
protected onDisconnected(): Promise<void>;
|
|
95
|
-
protected onBleStateChange(state: BleInterfaceState): void
|
|
102
|
+
protected onBleStateChange(state: BleInterfaceState): Promise<void>;
|
|
96
103
|
protected getAdapterFactory(): BleAdapterFactory<TBleSensor>;
|
|
97
104
|
protected getConnectTimeout(): number;
|
|
98
105
|
protected getExpectedServices(): string[];
|
|
@@ -50,7 +50,6 @@ class BleInterface extends events_1.default {
|
|
|
50
50
|
this.expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
51
51
|
this.matching = [];
|
|
52
52
|
this.connectedPeripherals = [];
|
|
53
|
-
this.connectAttemptCnt = 0;
|
|
54
53
|
this.emitted = [];
|
|
55
54
|
this.instanceId = ++instanceCount;
|
|
56
55
|
this.props = props;
|
|
@@ -111,49 +110,82 @@ class BleInterface extends events_1.default {
|
|
|
111
110
|
log: this.logEvent.bind(this),
|
|
112
111
|
});
|
|
113
112
|
const success = yield this.connectTask.run().catch(() => false);
|
|
114
|
-
if (success) {
|
|
115
|
-
this.startPeripheralScan();
|
|
116
|
-
}
|
|
117
113
|
return success;
|
|
118
114
|
});
|
|
119
115
|
}
|
|
120
116
|
disconnect(connectionLost) {
|
|
121
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
-
|
|
123
|
-
if (!this.getBinding()) {
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
if (!this.isConnected() && !connectionLost)
|
|
118
|
+
if (!this.isConnected())
|
|
127
119
|
return true;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
120
|
+
const performDisconnect = () => __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
var _a;
|
|
122
|
+
if (!this.getBinding()) {
|
|
123
|
+
this.confirmedBleState = 'poweredOff';
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
this.getBinding().removeAllListeners('error');
|
|
127
|
+
if (!this.isConnected() && !connectionLost) {
|
|
128
|
+
this.confirmedBleState = 'poweredOff';
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (!connectionLost)
|
|
132
|
+
this.logEvent({ message: 'disconnect request' });
|
|
133
|
+
this.emit('disconnect-request');
|
|
134
|
+
yield this.stopPeripheralScan();
|
|
135
|
+
if (connectionLost) {
|
|
136
|
+
this.emitDisconnectAllPeripherals();
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
yield this.disconnectAllPeripherals();
|
|
140
|
+
}
|
|
141
|
+
if (this.isConnecting())
|
|
142
|
+
yield ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.stop());
|
|
143
|
+
this.emit('disconnect-done');
|
|
144
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
145
|
+
this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
|
|
146
|
+
this.getBinding().on('error', this.onError.bind(this));
|
|
147
|
+
this.logEvent({ message: 'interface disconnected', interface: 'BLE' });
|
|
148
|
+
this.confirmedBleState = 'poweredOff';
|
|
149
|
+
return true;
|
|
150
|
+
});
|
|
151
|
+
if (this.isDisconnecting()) {
|
|
152
|
+
return this.disconnectTask.getPromise();
|
|
137
153
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
154
|
+
this.disconnectTask = new task_1.InteruptableTask(performDisconnect(), {
|
|
155
|
+
timeout: this.getConnectTimeout(),
|
|
156
|
+
name: 'BLE connect',
|
|
157
|
+
errorOnTimeout: false,
|
|
158
|
+
log: this.logEvent.bind(this),
|
|
159
|
+
});
|
|
160
|
+
const success = yield this.disconnectTask.run().catch(() => false);
|
|
161
|
+
return success;
|
|
144
162
|
});
|
|
145
163
|
}
|
|
146
164
|
isConnected() {
|
|
147
|
-
|
|
148
|
-
return this.connectAttemptCnt > 0 && ((_a = this.getBinding()) === null || _a === void 0 ? void 0 : _a.state) === 'poweredOn';
|
|
165
|
+
return this.confirmedBleState === 'poweredOn';
|
|
149
166
|
}
|
|
150
|
-
registerConnected(peripheral) {
|
|
151
|
-
this.connectedPeripherals.
|
|
167
|
+
registerConnected(peripheral, id) {
|
|
168
|
+
const p = this.connectedPeripherals.find(p => p.id === id);
|
|
169
|
+
if (p) {
|
|
170
|
+
p.peripheral = peripheral;
|
|
171
|
+
}
|
|
172
|
+
else
|
|
173
|
+
this.connectedPeripherals.push({ peripheral, id });
|
|
174
|
+
}
|
|
175
|
+
unregisterConnected(id) {
|
|
176
|
+
const p = this.connectedPeripherals.find(p => p.id === id);
|
|
177
|
+
if (p) {
|
|
178
|
+
this.connectedPeripherals.splice(this.connectedPeripherals.indexOf(p), 1);
|
|
179
|
+
}
|
|
152
180
|
}
|
|
153
181
|
isConnecting() {
|
|
154
182
|
var _a;
|
|
155
183
|
return ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.isRunning()) === true;
|
|
156
184
|
}
|
|
185
|
+
isDisconnecting() {
|
|
186
|
+
var _a;
|
|
187
|
+
return ((_a = this.disconnectTask) === null || _a === void 0 ? void 0 : _a.isRunning()) === true;
|
|
188
|
+
}
|
|
157
189
|
scan(props) {
|
|
158
190
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
191
|
this.resumeLogging();
|
|
@@ -261,12 +293,12 @@ class BleInterface extends events_1.default {
|
|
|
261
293
|
}
|
|
262
294
|
startPeripheralScan() {
|
|
263
295
|
return __awaiter(this, arguments, void 0, function* (retry = false) {
|
|
264
|
-
this.expectedServices = this.getExpectedServices();
|
|
265
|
-
if (!retry)
|
|
266
|
-
this.logEvent({ message: 'starting peripheral discovery ...' });
|
|
267
296
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
268
297
|
return;
|
|
269
298
|
}
|
|
299
|
+
this.expectedServices = this.getExpectedServices();
|
|
300
|
+
if (!retry)
|
|
301
|
+
this.logEvent({ message: 'starting peripheral discovery ...' });
|
|
270
302
|
this.discoverTask = new task_1.InteruptableTask(this.discoverPeripherals(), {
|
|
271
303
|
errorOnTimeout: false,
|
|
272
304
|
name: 'discover',
|
|
@@ -298,14 +330,14 @@ class BleInterface extends events_1.default {
|
|
|
298
330
|
}
|
|
299
331
|
emitDisconnectAllPeripherals() {
|
|
300
332
|
this.connectedPeripherals.forEach(p => {
|
|
301
|
-
const peripheral = p.getPeripheral();
|
|
333
|
+
const peripheral = p.peripheral.getPeripheral();
|
|
302
334
|
peripheral.emit('disconnect');
|
|
303
335
|
});
|
|
304
336
|
this.connectedPeripherals = [];
|
|
305
337
|
}
|
|
306
338
|
disconnectAllPeripherals() {
|
|
307
339
|
return __awaiter(this, void 0, void 0, function* () {
|
|
308
|
-
const promises = this.connectedPeripherals.map(p => p.disconnect());
|
|
340
|
+
const promises = this.connectedPeripherals.map(p => p.peripheral.disconnect());
|
|
309
341
|
yield Promise.allSettled(promises);
|
|
310
342
|
this.connectedPeripherals = [];
|
|
311
343
|
});
|
|
@@ -406,11 +438,11 @@ class BleInterface extends events_1.default {
|
|
|
406
438
|
try {
|
|
407
439
|
peripheral.on('error', (err) => {
|
|
408
440
|
peripheral.removeAllListeners();
|
|
409
|
-
this.logEvent({ message: 'peripheral error', error: err.message });
|
|
441
|
+
this.logEvent({ message: 'discover services: peripheral error', error: err.message });
|
|
410
442
|
});
|
|
411
443
|
peripheral.on('disconnect', () => {
|
|
412
444
|
peripheral.removeAllListeners();
|
|
413
|
-
this.logEvent({ message: 'peripheral disconnected' });
|
|
445
|
+
this.logEvent({ message: 'discover services: peripheral disconnected' });
|
|
414
446
|
});
|
|
415
447
|
yield peripheral.connectAsync();
|
|
416
448
|
if (peripheral.discoverServicesAsync !== undefined) {
|
|
@@ -545,10 +577,9 @@ class BleInterface extends events_1.default {
|
|
|
545
577
|
}
|
|
546
578
|
connectBle() {
|
|
547
579
|
return __awaiter(this, void 0, void 0, function* () {
|
|
548
|
-
this.
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
this.logEvent({ message: 'BLE connected' });
|
|
580
|
+
this.currentBleState = this.getBinding().state;
|
|
581
|
+
if (this.currentBleState === 'poweredOn') {
|
|
582
|
+
this.onConnected();
|
|
552
583
|
return true;
|
|
553
584
|
}
|
|
554
585
|
const res = yield this.waitForBleConnected();
|
|
@@ -562,13 +593,13 @@ class BleInterface extends events_1.default {
|
|
|
562
593
|
return done(false);
|
|
563
594
|
});
|
|
564
595
|
this.getBinding().on('stateChange', (state) => {
|
|
596
|
+
if (state === this.confirmedBleState)
|
|
597
|
+
return;
|
|
598
|
+
this.logEvent({ message: 'BLE state change', state });
|
|
565
599
|
if (state === 'poweredOn') {
|
|
566
600
|
this.onConnected();
|
|
567
601
|
return done(true);
|
|
568
602
|
}
|
|
569
|
-
else {
|
|
570
|
-
this.logEvent({ message: 'BLE state change', state });
|
|
571
|
-
}
|
|
572
603
|
});
|
|
573
604
|
});
|
|
574
605
|
}
|
|
@@ -576,26 +607,41 @@ class BleInterface extends events_1.default {
|
|
|
576
607
|
this.logError(err, 'BLE connect');
|
|
577
608
|
}
|
|
578
609
|
onConnected() {
|
|
579
|
-
this
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
610
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
611
|
+
if (this.isConnected())
|
|
612
|
+
return;
|
|
613
|
+
this.confirmedBleState = 'poweredOn';
|
|
614
|
+
this.getBinding().removeAllListeners('error');
|
|
615
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
616
|
+
this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
|
|
617
|
+
this.getBinding().on('error', this.onError.bind(this));
|
|
618
|
+
this.logEvent({ message: 'BLE connected' });
|
|
619
|
+
this.startPeripheralScan();
|
|
620
|
+
});
|
|
584
621
|
}
|
|
585
622
|
onDisconnected() {
|
|
586
623
|
return __awaiter(this, void 0, void 0, function* () {
|
|
624
|
+
if (this.isDisconnecting() || !this.isConnected())
|
|
625
|
+
return;
|
|
587
626
|
this.logEvent({ message: 'BLE Disconnected' });
|
|
588
627
|
yield this.disconnect(true);
|
|
589
|
-
this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
|
|
590
|
-
this.getBinding().on('error', this.onError.bind(this));
|
|
591
628
|
});
|
|
592
629
|
}
|
|
593
630
|
onBleStateChange(state) {
|
|
594
|
-
|
|
595
|
-
this.
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
631
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
632
|
+
if (state === this.currentBleState)
|
|
633
|
+
return;
|
|
634
|
+
this.currentBleState = state;
|
|
635
|
+
if (state === 'poweredOff') {
|
|
636
|
+
this.onDisconnected();
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
if (this.isDisconnecting()) {
|
|
640
|
+
yield this.disconnectTask.getPromise();
|
|
641
|
+
}
|
|
642
|
+
this.onConnected();
|
|
643
|
+
}
|
|
644
|
+
});
|
|
599
645
|
}
|
|
600
646
|
getAdapterFactory() {
|
|
601
647
|
return factories_1.BleAdapterFactory.getInstance('ble');
|
|
@@ -3,6 +3,7 @@ import { BleInterface } from "./interface";
|
|
|
3
3
|
export declare class BlePeripheral implements IBlePeripheral {
|
|
4
4
|
protected announcement: BlePeripheralAnnouncement;
|
|
5
5
|
protected connected: boolean;
|
|
6
|
+
protected connectPromise: Promise<void>;
|
|
6
7
|
protected characteristics: Record<string, BleRawCharacteristic>;
|
|
7
8
|
protected onDisconnectHandler?: () => void;
|
|
8
9
|
protected ble: BleInterface;
|
|
@@ -11,6 +12,7 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
11
12
|
callback: (data: Buffer) => void;
|
|
12
13
|
}>;
|
|
13
14
|
protected disconnecting: boolean;
|
|
15
|
+
protected disconnectedSignalled: boolean;
|
|
14
16
|
protected onErrorHandler: any;
|
|
15
17
|
constructor(announcement: BlePeripheralAnnouncement);
|
|
16
18
|
get services(): BleService[];
|
|
@@ -33,6 +35,7 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
33
35
|
unsubscribeAll(connectionLost?: boolean): Promise<void>;
|
|
34
36
|
read(characteristicUUID: string): Promise<Buffer>;
|
|
35
37
|
write(characteristicUUID: string, data: Buffer, options?: BleWriteProps): Promise<Buffer>;
|
|
36
|
-
protected
|
|
38
|
+
protected queryRawCharacteristic(uuid: string): Promise<BleRawCharacteristic>;
|
|
39
|
+
protected getRawCharacteristic(uuid: string, query?: boolean): BleRawCharacteristic;
|
|
37
40
|
logEvent(event: any): void;
|
|
38
41
|
}
|
|
@@ -19,6 +19,7 @@ class BlePeripheral {
|
|
|
19
19
|
this.characteristics = {};
|
|
20
20
|
this.subscribed = [];
|
|
21
21
|
this.disconnecting = false;
|
|
22
|
+
this.disconnectedSignalled = false;
|
|
22
23
|
this.onErrorHandler = this.onPeripheralError.bind(this);
|
|
23
24
|
this.ble = interface_1.BleInterface.getInstance();
|
|
24
25
|
}
|
|
@@ -32,28 +33,42 @@ class BlePeripheral {
|
|
|
32
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
33
34
|
if (this.isConnected())
|
|
34
35
|
return true;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
if (this.connectPromise !== undefined) {
|
|
37
|
+
return this.connectPromise.then(() => this.connected);
|
|
38
|
+
}
|
|
39
|
+
this.connectPromise = new Promise((done) => {
|
|
40
|
+
const peripheral = this.getPeripheral();
|
|
41
|
+
this.logEvent({ message: 'connect peripheral', address: peripheral.address });
|
|
42
|
+
peripheral.connectAsync().then(() => {
|
|
43
|
+
this.ble.registerConnected(this, peripheral.id);
|
|
44
|
+
peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
|
|
45
|
+
peripheral.on('error', this.onErrorHandler);
|
|
46
|
+
this.connected = true;
|
|
47
|
+
done();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
yield this.connectPromise;
|
|
51
|
+
delete this.connectPromise;
|
|
41
52
|
return this.connected;
|
|
42
53
|
});
|
|
43
54
|
}
|
|
44
55
|
disconnect() {
|
|
45
56
|
return __awaiter(this, arguments, void 0, function* (connectionLost = false) {
|
|
46
57
|
this.disconnecting = true;
|
|
47
|
-
if (!this.isConnected())
|
|
58
|
+
if (!this.isConnected()) {
|
|
48
59
|
return true;
|
|
60
|
+
}
|
|
49
61
|
yield this.unsubscribeAll(connectionLost);
|
|
50
62
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
51
63
|
const c = this.characteristics[uuid];
|
|
52
64
|
c.removeAllListeners();
|
|
53
65
|
});
|
|
66
|
+
this.characteristics = {};
|
|
67
|
+
this.subscribed = [];
|
|
54
68
|
const peripheral = this.getPeripheral();
|
|
55
69
|
if (peripheral) {
|
|
56
70
|
if (!connectionLost) {
|
|
71
|
+
this.logEvent({ message: 'disconnect peripheral', address: peripheral.address });
|
|
57
72
|
if (!peripheral.disconnectAsync) {
|
|
58
73
|
peripheral.disconnectAsync = () => {
|
|
59
74
|
return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
|
|
@@ -63,6 +78,7 @@ class BlePeripheral {
|
|
|
63
78
|
}
|
|
64
79
|
peripheral.removeAllListeners();
|
|
65
80
|
}
|
|
81
|
+
this.ble.unregisterConnected(peripheral.id);
|
|
66
82
|
this.connected = false;
|
|
67
83
|
this.disconnecting = false;
|
|
68
84
|
return !this.connected;
|
|
@@ -79,27 +95,35 @@ class BlePeripheral {
|
|
|
79
95
|
}
|
|
80
96
|
onPeripheralDisconnect() {
|
|
81
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
-
|
|
98
|
+
var _a;
|
|
99
|
+
if (this.disconnectedSignalled || this.disconnecting)
|
|
100
|
+
return;
|
|
101
|
+
this.disconnectedSignalled = true;
|
|
102
|
+
this.logEvent({ message: 'peripheral disconnected', address: (_a = this.getPeripheral()) === null || _a === void 0 ? void 0 : _a.address });
|
|
83
103
|
try {
|
|
84
104
|
yield this.disconnect(true);
|
|
105
|
+
this.disconnectedSignalled = false;
|
|
85
106
|
}
|
|
86
|
-
catch (
|
|
107
|
+
catch (_b) { }
|
|
87
108
|
if (this.onDisconnectHandler)
|
|
88
109
|
this.onDisconnectHandler();
|
|
89
110
|
});
|
|
90
111
|
}
|
|
91
112
|
onPeripheralError(err) {
|
|
92
|
-
|
|
113
|
+
var _a;
|
|
114
|
+
this.logEvent({ message: 'peripheral error', address: (_a = this.getPeripheral()) === null || _a === void 0 ? void 0 : _a.address, error: err.message });
|
|
93
115
|
}
|
|
94
116
|
discoverServices() {
|
|
95
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
if (!this.getPeripheral())
|
|
119
|
+
return [];
|
|
96
120
|
if (this.getPeripheral().discoverServicesAsync) {
|
|
97
|
-
this.logEvent({ message: 'discover services' });
|
|
121
|
+
this.logEvent({ message: 'discover services', address: this.getPeripheral().address });
|
|
98
122
|
const services = yield this.getPeripheral().discoverServicesAsync([]);
|
|
99
123
|
return services.map(s => s.uuid);
|
|
100
124
|
}
|
|
101
125
|
else {
|
|
102
|
-
this.logEvent({ message: 'discover services and characteristics' });
|
|
126
|
+
this.logEvent({ message: 'discover services and characteristics', address: this.getPeripheral().address });
|
|
103
127
|
const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
|
|
104
128
|
return res.services.map(s => s.uuid);
|
|
105
129
|
}
|
|
@@ -107,6 +131,9 @@ class BlePeripheral {
|
|
|
107
131
|
}
|
|
108
132
|
discoverCharacteristics(serviceUUID) {
|
|
109
133
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
if (!this.getPeripheral())
|
|
135
|
+
return [];
|
|
136
|
+
this.logEvent({ message: 'discover services and characteristics', service: serviceUUID, address: this.getPeripheral().address });
|
|
110
137
|
const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([serviceUUID], []);
|
|
111
138
|
res.characteristics.forEach(c => this.characteristics[(0, utils_1.beautifyUUID)(c.uuid)] = c);
|
|
112
139
|
return res.characteristics.map(c => {
|
|
@@ -120,20 +147,23 @@ class BlePeripheral {
|
|
|
120
147
|
try {
|
|
121
148
|
if (this.disconnecting || !this.connected)
|
|
122
149
|
return false;
|
|
150
|
+
const uuid = (0, utils_1.beautifyUUID)(characteristicUUID);
|
|
123
151
|
const onData = (data) => {
|
|
124
152
|
try {
|
|
125
153
|
callback(characteristicUUID, data);
|
|
126
154
|
}
|
|
127
155
|
catch (_a) { }
|
|
128
156
|
};
|
|
129
|
-
const subscription = this.subscribed.find(s => s.uuid ===
|
|
157
|
+
const subscription = this.subscribed.find(s => s.uuid === uuid);
|
|
130
158
|
if (subscription) {
|
|
131
159
|
const c = this.getRawCharacteristic(characteristicUUID);
|
|
132
|
-
if (c)
|
|
160
|
+
if (c) {
|
|
161
|
+
c.off('data', onData);
|
|
133
162
|
c.on('data', onData);
|
|
163
|
+
}
|
|
134
164
|
return true;
|
|
135
165
|
}
|
|
136
|
-
|
|
166
|
+
let c = yield this.queryRawCharacteristic(characteristicUUID);
|
|
137
167
|
if (!c) {
|
|
138
168
|
return false;
|
|
139
169
|
}
|
|
@@ -142,22 +172,22 @@ class BlePeripheral {
|
|
|
142
172
|
if (info) {
|
|
143
173
|
return Promise.resolve(true);
|
|
144
174
|
}
|
|
145
|
-
|
|
146
|
-
this.logEvent({ message: 'subscribe request', characteristic: uuid, success: true });
|
|
175
|
+
this.logEvent({ message: 'subscribe request', address: this.getPeripheral().address, characteristic: uuid, success: true });
|
|
147
176
|
c.subscribe((err) => {
|
|
148
177
|
if (err) {
|
|
149
|
-
this.logEvent({ message: 'subscribe result', characteristic: uuid, success: false, reason: err.message });
|
|
178
|
+
this.logEvent({ message: 'subscribe result', address: this.getPeripheral().address, characteristic: uuid, success: false, reason: err.message });
|
|
150
179
|
resolve(false);
|
|
151
180
|
}
|
|
152
181
|
else {
|
|
153
182
|
if (callback) {
|
|
154
|
-
this.subscribed.push({ uuid
|
|
183
|
+
this.subscribed.push({ uuid, callback: onData });
|
|
184
|
+
c.off('data', onData);
|
|
155
185
|
c.on('data', onData);
|
|
156
186
|
}
|
|
157
187
|
else {
|
|
158
|
-
this.subscribed.push({ uuid
|
|
188
|
+
this.subscribed.push({ uuid, callback: null });
|
|
159
189
|
}
|
|
160
|
-
this.logEvent({ message: 'subscribe result', characteristic: uuid, success: true });
|
|
190
|
+
this.logEvent({ message: 'subscribe result', address: this.getPeripheral().address, characteristic: uuid, success: true });
|
|
161
191
|
resolve(true);
|
|
162
192
|
}
|
|
163
193
|
});
|
|
@@ -171,7 +201,8 @@ class BlePeripheral {
|
|
|
171
201
|
}
|
|
172
202
|
unsubscribe(characteristicUUID) {
|
|
173
203
|
try {
|
|
174
|
-
const
|
|
204
|
+
const uuid = (0, utils_1.beautifyUUID)(characteristicUUID);
|
|
205
|
+
const subscription = this.subscribed.find(s => s.uuid === uuid);
|
|
175
206
|
if (!subscription) {
|
|
176
207
|
return Promise.resolve(true);
|
|
177
208
|
}
|
|
@@ -185,7 +216,7 @@ class BlePeripheral {
|
|
|
185
216
|
resolve(false);
|
|
186
217
|
}
|
|
187
218
|
else {
|
|
188
|
-
const info = this.subscribed.find(s => s.uuid ===
|
|
219
|
+
const info = this.subscribed.find(s => s.uuid === uuid);
|
|
189
220
|
if (info) {
|
|
190
221
|
this.subscribed.splice(this.subscribed.indexOf(info), 1);
|
|
191
222
|
if (info.callback)
|
|
@@ -328,7 +359,24 @@ class BlePeripheral {
|
|
|
328
359
|
});
|
|
329
360
|
});
|
|
330
361
|
}
|
|
331
|
-
|
|
362
|
+
queryRawCharacteristic(uuid) {
|
|
363
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
364
|
+
const characteristicUUID = (0, utils_1.beautifyUUID)(uuid);
|
|
365
|
+
let c = this.characteristics[(0, utils_1.beautifyUUID)(uuid)];
|
|
366
|
+
if (c)
|
|
367
|
+
return c;
|
|
368
|
+
const res = yield this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], [characteristicUUID]);
|
|
369
|
+
if (res.characteristics.length === 0) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
c = res.characteristics.find(dc => (0, utils_1.beautifyUUID)(dc.uuid) === (0, utils_1.beautifyUUID)(characteristicUUID));
|
|
373
|
+
if (c) {
|
|
374
|
+
this.characteristics[characteristicUUID] = c;
|
|
375
|
+
}
|
|
376
|
+
return c;
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
getRawCharacteristic(uuid, query = false) {
|
|
332
380
|
return this.characteristics[(0, utils_1.beautifyUUID)(uuid)];
|
|
333
381
|
}
|
|
334
382
|
logEvent(event) {
|