incyclist-devices 3.0.47 → 3.0.48
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/cjs/ble/base/adapter.js +6 -1
- package/lib/cjs/ble/base/interface.js +59 -17
- package/lib/cjs/ble/base/peripheral.js +22 -5
- package/lib/cjs/ble/fm/sensor.js +2 -0
- package/lib/esm/ble/base/adapter.js +6 -1
- package/lib/esm/ble/base/interface.js +59 -17
- package/lib/esm/ble/base/peripheral.js +23 -6
- package/lib/esm/ble/fm/sensor.js +2 -0
- package/lib/types/ble/base/interface.d.ts +9 -3
- package/lib/types/ble/base/peripheral.d.ts +1 -0
- package/lib/types/types/interface.d.ts +2 -2
- package/package.json +1 -1
|
@@ -407,7 +407,12 @@ class BleAdapter extends adpater_js_1.default {
|
|
|
407
407
|
}
|
|
408
408
|
return connected;
|
|
409
409
|
}
|
|
410
|
-
|
|
410
|
+
try {
|
|
411
|
+
await sensor.getPeripheral().disconnect();
|
|
412
|
+
}
|
|
413
|
+
catch (err) {
|
|
414
|
+
this.logEvent({ message: 'restart: disconnect failed', device: this.getName(), interface: this.getInterface(), reason: err?.message });
|
|
415
|
+
}
|
|
411
416
|
const success = await super.restart(pause);
|
|
412
417
|
return success;
|
|
413
418
|
}
|
|
@@ -11,6 +11,8 @@ const index_js_1 = require("../factories/index.js");
|
|
|
11
11
|
const consts_js_1 = require("../consts.js");
|
|
12
12
|
const consts_js_2 = require("../bkool/consts.js");
|
|
13
13
|
const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
|
|
14
|
+
const DEFAULT_PAUSE_REASON = 'app-background';
|
|
15
|
+
const TEMPORARY_SCAN_TIMEOUT = 30 * 1000;
|
|
14
16
|
const BLE_DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
|
|
15
17
|
let instanceCount = 0;
|
|
16
18
|
class BleInterface extends node_events_1.EventEmitter {
|
|
@@ -29,6 +31,8 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
29
31
|
expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
30
32
|
matching = [];
|
|
31
33
|
backgroundPaused = false;
|
|
34
|
+
pauseReasons = new Set();
|
|
35
|
+
pausedSince;
|
|
32
36
|
connectTask;
|
|
33
37
|
disconnectTask;
|
|
34
38
|
scanTask;
|
|
@@ -247,18 +251,30 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
247
251
|
delete this.scanTask;
|
|
248
252
|
return this.buildDeviceSettings(this.matching);
|
|
249
253
|
}
|
|
250
|
-
async pauseBackgroundActivity() {
|
|
251
|
-
if (this.
|
|
254
|
+
async pauseBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
255
|
+
if (this.pauseReasons.has(reason))
|
|
252
256
|
return;
|
|
257
|
+
this.pauseReasons.add(reason);
|
|
258
|
+
const alreadyPaused = this.backgroundPaused;
|
|
253
259
|
this.backgroundPaused = true;
|
|
254
|
-
this.logEvent({ message: 'pausing background activity' });
|
|
260
|
+
this.logEvent({ message: 'pausing background activity', reason, reasons: Array.from(this.pauseReasons).join(','), interface: 'ble' });
|
|
261
|
+
if (alreadyPaused)
|
|
262
|
+
return;
|
|
263
|
+
this.pausedSince = Date.now();
|
|
255
264
|
await this.stopPeripheralScan();
|
|
256
265
|
}
|
|
257
|
-
async resumeBackgroundActivity() {
|
|
258
|
-
if (!this.
|
|
266
|
+
async resumeBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
267
|
+
if (!this.pauseReasons.delete(reason))
|
|
259
268
|
return;
|
|
269
|
+
const reasons = Array.from(this.pauseReasons).join(',');
|
|
270
|
+
if (this.pauseReasons.size > 0) {
|
|
271
|
+
this.logEvent({ message: 'resuming background activity skipped', reason, reasons, interface: 'ble' });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
260
274
|
this.backgroundPaused = false;
|
|
261
|
-
this.
|
|
275
|
+
const pausedMs = this.pausedSince ? Date.now() - this.pausedSince : 0;
|
|
276
|
+
delete this.pausedSince;
|
|
277
|
+
this.logEvent({ message: 'resuming background activity', reason, reasons, pausedMs, interface: 'ble' });
|
|
262
278
|
await this.startPeripheralScan();
|
|
263
279
|
}
|
|
264
280
|
pauseLogging() {
|
|
@@ -293,22 +309,40 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
293
309
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
294
310
|
if (peripheral)
|
|
295
311
|
return Promise.resolve(peripheral);
|
|
296
|
-
return new Promise((done) => {
|
|
312
|
+
return new Promise((done, reject) => {
|
|
313
|
+
const temporary = this.backgroundPaused;
|
|
297
314
|
const wasDiscovering = this.isDiscovering();
|
|
298
|
-
|
|
299
|
-
|
|
315
|
+
const tsStart = Date.now();
|
|
316
|
+
let timer;
|
|
317
|
+
const finish = () => {
|
|
318
|
+
this.off('device', onDevice);
|
|
319
|
+
if (timer)
|
|
320
|
+
clearTimeout(timer);
|
|
321
|
+
if (temporary || !wasDiscovering)
|
|
322
|
+
this.stopPeripheralScan().catch(() => { });
|
|
323
|
+
};
|
|
300
324
|
const onDevice = (device) => {
|
|
301
325
|
if (device.address === settings.address) {
|
|
302
326
|
const peripheral = this.createPeripheralFromSettings(device);
|
|
303
327
|
if (peripheral) {
|
|
304
|
-
|
|
305
|
-
if (
|
|
306
|
-
this.
|
|
328
|
+
finish();
|
|
329
|
+
if (temporary)
|
|
330
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: true });
|
|
307
331
|
done(peripheral);
|
|
308
332
|
}
|
|
309
333
|
}
|
|
310
334
|
};
|
|
311
335
|
this.on('device', onDevice);
|
|
336
|
+
if (temporary) {
|
|
337
|
+
timer = setTimeout(() => {
|
|
338
|
+
finish();
|
|
339
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: false });
|
|
340
|
+
reject(new Error('peripheral not found'));
|
|
341
|
+
}, TEMPORARY_SCAN_TIMEOUT);
|
|
342
|
+
this.startPeripheralScan(false, true).catch(() => { });
|
|
343
|
+
}
|
|
344
|
+
else if (!wasDiscovering)
|
|
345
|
+
this.startPeripheralScan();
|
|
312
346
|
});
|
|
313
347
|
}
|
|
314
348
|
createDeviceSetting(service) {
|
|
@@ -336,8 +370,8 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
336
370
|
await this.disconnect();
|
|
337
371
|
await this.connectInternal(true);
|
|
338
372
|
}
|
|
339
|
-
async startPeripheralScan(retry = false) {
|
|
340
|
-
if (this.backgroundPaused)
|
|
373
|
+
async startPeripheralScan(retry = false, force = false) {
|
|
374
|
+
if (this.backgroundPaused && !force)
|
|
341
375
|
return;
|
|
342
376
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
343
377
|
return;
|
|
@@ -357,7 +391,7 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
357
391
|
this.logError(err, 'discover');
|
|
358
392
|
}
|
|
359
393
|
if (this.discoverTask.getState().result !== 'stopped') {
|
|
360
|
-
this.startPeripheralScan(true);
|
|
394
|
+
this.startPeripheralScan(true, force).catch(err => this.logError(err, 'discover'));
|
|
361
395
|
}
|
|
362
396
|
}
|
|
363
397
|
async stopPeripheralScan() {
|
|
@@ -644,10 +678,18 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
644
678
|
return supported.length > 0;
|
|
645
679
|
}
|
|
646
680
|
find(service) {
|
|
647
|
-
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address &&
|
|
681
|
+
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address && this.isAnnouncementValid(a));
|
|
648
682
|
}
|
|
649
683
|
getAll() {
|
|
650
|
-
return this.services.filter(a =>
|
|
684
|
+
return this.services.filter(a => this.isAnnouncementValid(a));
|
|
685
|
+
}
|
|
686
|
+
isAnnouncementValid(a) {
|
|
687
|
+
if (a.ts > Date.now() - BLE_EXPIRATION_TIMEOUT)
|
|
688
|
+
return true;
|
|
689
|
+
if (!this.backgroundPaused)
|
|
690
|
+
return false;
|
|
691
|
+
const id = a.service?.peripheral?.id;
|
|
692
|
+
return id !== undefined && this.connectedPeripherals.some(p => p.id === id);
|
|
651
693
|
}
|
|
652
694
|
setDebug(enabled) {
|
|
653
695
|
this.debug = enabled;
|
|
@@ -7,6 +7,7 @@ const interface_js_1 = require("./interface.js");
|
|
|
7
7
|
const SERVICE_COMPLETENESS_WHITELIST = ['MRK-R15'];
|
|
8
8
|
const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
|
|
9
9
|
const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
|
|
10
|
+
const BLE_DISCONNECT_STEP_TIMEOUT = 3000;
|
|
10
11
|
class BlePeripheral {
|
|
11
12
|
announcement;
|
|
12
13
|
connected = false;
|
|
@@ -88,8 +89,21 @@ class BlePeripheral {
|
|
|
88
89
|
}
|
|
89
90
|
async disconnect(connectionLost = false) {
|
|
90
91
|
this.disconnecting = true;
|
|
92
|
+
try {
|
|
93
|
+
return await this.doDisconnect(connectionLost);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
this.disconnecting = false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async doDisconnect(connectionLost) {
|
|
91
100
|
if (this.isConnected() || connectionLost) {
|
|
92
|
-
|
|
101
|
+
try {
|
|
102
|
+
await (0, utils_js_1.runWithTimeout)(this.unsubscribeAll(connectionLost), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
this.logEvent({ message: 'peripheral unsubscribe timeout', timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
106
|
+
}
|
|
93
107
|
}
|
|
94
108
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
95
109
|
const c = this.characteristics[uuid];
|
|
@@ -106,8 +120,12 @@ class BlePeripheral {
|
|
|
106
120
|
return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
|
|
107
121
|
};
|
|
108
122
|
}
|
|
109
|
-
|
|
110
|
-
.
|
|
123
|
+
try {
|
|
124
|
+
await (0, utils_js_1.runWithTimeout)(this.getPeripheral().disconnectAsync(), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
this.logEvent({ message: 'peripheral disconnect timeout', address: peripheral.address, timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
128
|
+
}
|
|
111
129
|
}
|
|
112
130
|
peripheral.removeAllListeners();
|
|
113
131
|
this.ble.unregisterConnected(peripheral.id);
|
|
@@ -116,8 +134,7 @@ class BlePeripheral {
|
|
|
116
134
|
delete this.onDisconnectHandler;
|
|
117
135
|
}
|
|
118
136
|
this.connected = false;
|
|
119
|
-
this.
|
|
120
|
-
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral.address });
|
|
137
|
+
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral?.address });
|
|
121
138
|
return !this.connected;
|
|
122
139
|
}
|
|
123
140
|
isConnected() {
|
package/lib/cjs/ble/fm/sensor.js
CHANGED
|
@@ -672,6 +672,8 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
|
|
|
672
672
|
res = await this.write(consts_js_1.FTMS_CP, data, props);
|
|
673
673
|
}
|
|
674
674
|
const responseData = Buffer.from(res);
|
|
675
|
+
if (responseData.length < 3)
|
|
676
|
+
throw new Error('not connected');
|
|
675
677
|
const opCode = responseData.readUInt8(0);
|
|
676
678
|
const request = responseData.readUInt8(1);
|
|
677
679
|
const result = responseData.readUInt8(2);
|
|
@@ -402,7 +402,12 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
402
402
|
}
|
|
403
403
|
return connected;
|
|
404
404
|
}
|
|
405
|
-
|
|
405
|
+
try {
|
|
406
|
+
await sensor.getPeripheral().disconnect();
|
|
407
|
+
}
|
|
408
|
+
catch (err) {
|
|
409
|
+
this.logEvent({ message: 'restart: disconnect failed', device: this.getName(), interface: this.getInterface(), reason: err?.message });
|
|
410
|
+
}
|
|
406
411
|
const success = await super.restart(pause);
|
|
407
412
|
return success;
|
|
408
413
|
}
|
|
@@ -8,6 +8,8 @@ import { BleAdapterFactory } from "../factories/index.js";
|
|
|
8
8
|
import { CSP } from "../consts.js";
|
|
9
9
|
import { BKOOL_SERVICE, isBkoolDeviceName } from "../bkool/consts.js";
|
|
10
10
|
const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
|
|
11
|
+
const DEFAULT_PAUSE_REASON = 'app-background';
|
|
12
|
+
const TEMPORARY_SCAN_TIMEOUT = 30 * 1000;
|
|
11
13
|
const BLE_DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
|
|
12
14
|
let instanceCount = 0;
|
|
13
15
|
export class BleInterface extends EventEmitter {
|
|
@@ -26,6 +28,8 @@ export class BleInterface extends EventEmitter {
|
|
|
26
28
|
expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
27
29
|
matching = [];
|
|
28
30
|
backgroundPaused = false;
|
|
31
|
+
pauseReasons = new Set();
|
|
32
|
+
pausedSince;
|
|
29
33
|
connectTask;
|
|
30
34
|
disconnectTask;
|
|
31
35
|
scanTask;
|
|
@@ -244,18 +248,30 @@ export class BleInterface extends EventEmitter {
|
|
|
244
248
|
delete this.scanTask;
|
|
245
249
|
return this.buildDeviceSettings(this.matching);
|
|
246
250
|
}
|
|
247
|
-
async pauseBackgroundActivity() {
|
|
248
|
-
if (this.
|
|
251
|
+
async pauseBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
252
|
+
if (this.pauseReasons.has(reason))
|
|
249
253
|
return;
|
|
254
|
+
this.pauseReasons.add(reason);
|
|
255
|
+
const alreadyPaused = this.backgroundPaused;
|
|
250
256
|
this.backgroundPaused = true;
|
|
251
|
-
this.logEvent({ message: 'pausing background activity' });
|
|
257
|
+
this.logEvent({ message: 'pausing background activity', reason, reasons: Array.from(this.pauseReasons).join(','), interface: 'ble' });
|
|
258
|
+
if (alreadyPaused)
|
|
259
|
+
return;
|
|
260
|
+
this.pausedSince = Date.now();
|
|
252
261
|
await this.stopPeripheralScan();
|
|
253
262
|
}
|
|
254
|
-
async resumeBackgroundActivity() {
|
|
255
|
-
if (!this.
|
|
263
|
+
async resumeBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
264
|
+
if (!this.pauseReasons.delete(reason))
|
|
256
265
|
return;
|
|
266
|
+
const reasons = Array.from(this.pauseReasons).join(',');
|
|
267
|
+
if (this.pauseReasons.size > 0) {
|
|
268
|
+
this.logEvent({ message: 'resuming background activity skipped', reason, reasons, interface: 'ble' });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
257
271
|
this.backgroundPaused = false;
|
|
258
|
-
this.
|
|
272
|
+
const pausedMs = this.pausedSince ? Date.now() - this.pausedSince : 0;
|
|
273
|
+
delete this.pausedSince;
|
|
274
|
+
this.logEvent({ message: 'resuming background activity', reason, reasons, pausedMs, interface: 'ble' });
|
|
259
275
|
await this.startPeripheralScan();
|
|
260
276
|
}
|
|
261
277
|
pauseLogging() {
|
|
@@ -290,22 +306,40 @@ export class BleInterface extends EventEmitter {
|
|
|
290
306
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
291
307
|
if (peripheral)
|
|
292
308
|
return Promise.resolve(peripheral);
|
|
293
|
-
return new Promise((done) => {
|
|
309
|
+
return new Promise((done, reject) => {
|
|
310
|
+
const temporary = this.backgroundPaused;
|
|
294
311
|
const wasDiscovering = this.isDiscovering();
|
|
295
|
-
|
|
296
|
-
|
|
312
|
+
const tsStart = Date.now();
|
|
313
|
+
let timer;
|
|
314
|
+
const finish = () => {
|
|
315
|
+
this.off('device', onDevice);
|
|
316
|
+
if (timer)
|
|
317
|
+
clearTimeout(timer);
|
|
318
|
+
if (temporary || !wasDiscovering)
|
|
319
|
+
this.stopPeripheralScan().catch(() => { });
|
|
320
|
+
};
|
|
297
321
|
const onDevice = (device) => {
|
|
298
322
|
if (device.address === settings.address) {
|
|
299
323
|
const peripheral = this.createPeripheralFromSettings(device);
|
|
300
324
|
if (peripheral) {
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
this.
|
|
325
|
+
finish();
|
|
326
|
+
if (temporary)
|
|
327
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: true });
|
|
304
328
|
done(peripheral);
|
|
305
329
|
}
|
|
306
330
|
}
|
|
307
331
|
};
|
|
308
332
|
this.on('device', onDevice);
|
|
333
|
+
if (temporary) {
|
|
334
|
+
timer = setTimeout(() => {
|
|
335
|
+
finish();
|
|
336
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: false });
|
|
337
|
+
reject(new Error('peripheral not found'));
|
|
338
|
+
}, TEMPORARY_SCAN_TIMEOUT);
|
|
339
|
+
this.startPeripheralScan(false, true).catch(() => { });
|
|
340
|
+
}
|
|
341
|
+
else if (!wasDiscovering)
|
|
342
|
+
this.startPeripheralScan();
|
|
309
343
|
});
|
|
310
344
|
}
|
|
311
345
|
createDeviceSetting(service) {
|
|
@@ -333,8 +367,8 @@ export class BleInterface extends EventEmitter {
|
|
|
333
367
|
await this.disconnect();
|
|
334
368
|
await this.connectInternal(true);
|
|
335
369
|
}
|
|
336
|
-
async startPeripheralScan(retry = false) {
|
|
337
|
-
if (this.backgroundPaused)
|
|
370
|
+
async startPeripheralScan(retry = false, force = false) {
|
|
371
|
+
if (this.backgroundPaused && !force)
|
|
338
372
|
return;
|
|
339
373
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
340
374
|
return;
|
|
@@ -354,7 +388,7 @@ export class BleInterface extends EventEmitter {
|
|
|
354
388
|
this.logError(err, 'discover');
|
|
355
389
|
}
|
|
356
390
|
if (this.discoverTask.getState().result !== 'stopped') {
|
|
357
|
-
this.startPeripheralScan(true);
|
|
391
|
+
this.startPeripheralScan(true, force).catch(err => this.logError(err, 'discover'));
|
|
358
392
|
}
|
|
359
393
|
}
|
|
360
394
|
async stopPeripheralScan() {
|
|
@@ -641,10 +675,18 @@ export class BleInterface extends EventEmitter {
|
|
|
641
675
|
return supported.length > 0;
|
|
642
676
|
}
|
|
643
677
|
find(service) {
|
|
644
|
-
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address &&
|
|
678
|
+
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address && this.isAnnouncementValid(a));
|
|
645
679
|
}
|
|
646
680
|
getAll() {
|
|
647
|
-
return this.services.filter(a =>
|
|
681
|
+
return this.services.filter(a => this.isAnnouncementValid(a));
|
|
682
|
+
}
|
|
683
|
+
isAnnouncementValid(a) {
|
|
684
|
+
if (a.ts > Date.now() - BLE_EXPIRATION_TIMEOUT)
|
|
685
|
+
return true;
|
|
686
|
+
if (!this.backgroundPaused)
|
|
687
|
+
return false;
|
|
688
|
+
const id = a.service?.peripheral?.id;
|
|
689
|
+
return id !== undefined && this.connectedPeripherals.some(p => p.id === id);
|
|
648
690
|
}
|
|
649
691
|
setDebug(enabled) {
|
|
650
692
|
this.debug = enabled;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { sleep } from "../../utils/utils.js";
|
|
1
|
+
import { runWithTimeout, sleep } from "../../utils/utils.js";
|
|
2
2
|
import { beautifyUUID, fullUUID, isSameServiceFamily, matches } from "../utils.js";
|
|
3
3
|
import { BleInterface } from "./interface.js";
|
|
4
4
|
const SERVICE_COMPLETENESS_WHITELIST = ['MRK-R15'];
|
|
5
5
|
const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
|
|
6
6
|
const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
|
|
7
|
+
const BLE_DISCONNECT_STEP_TIMEOUT = 3000;
|
|
7
8
|
export class BlePeripheral {
|
|
8
9
|
announcement;
|
|
9
10
|
connected = false;
|
|
@@ -85,8 +86,21 @@ export class BlePeripheral {
|
|
|
85
86
|
}
|
|
86
87
|
async disconnect(connectionLost = false) {
|
|
87
88
|
this.disconnecting = true;
|
|
89
|
+
try {
|
|
90
|
+
return await this.doDisconnect(connectionLost);
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
this.disconnecting = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async doDisconnect(connectionLost) {
|
|
88
97
|
if (this.isConnected() || connectionLost) {
|
|
89
|
-
|
|
98
|
+
try {
|
|
99
|
+
await runWithTimeout(this.unsubscribeAll(connectionLost), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
this.logEvent({ message: 'peripheral unsubscribe timeout', timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
103
|
+
}
|
|
90
104
|
}
|
|
91
105
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
92
106
|
const c = this.characteristics[uuid];
|
|
@@ -103,8 +117,12 @@ export class BlePeripheral {
|
|
|
103
117
|
return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
|
|
104
118
|
};
|
|
105
119
|
}
|
|
106
|
-
|
|
107
|
-
.
|
|
120
|
+
try {
|
|
121
|
+
await runWithTimeout(this.getPeripheral().disconnectAsync(), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
this.logEvent({ message: 'peripheral disconnect timeout', address: peripheral.address, timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
125
|
+
}
|
|
108
126
|
}
|
|
109
127
|
peripheral.removeAllListeners();
|
|
110
128
|
this.ble.unregisterConnected(peripheral.id);
|
|
@@ -113,8 +131,7 @@ export class BlePeripheral {
|
|
|
113
131
|
delete this.onDisconnectHandler;
|
|
114
132
|
}
|
|
115
133
|
this.connected = false;
|
|
116
|
-
this.
|
|
117
|
-
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral.address });
|
|
134
|
+
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral?.address });
|
|
118
135
|
return !this.connected;
|
|
119
136
|
}
|
|
120
137
|
isConnected() {
|
package/lib/esm/ble/fm/sensor.js
CHANGED
|
@@ -670,6 +670,8 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
670
670
|
res = await this.write(FTMS_CP, data, props);
|
|
671
671
|
}
|
|
672
672
|
const responseData = Buffer.from(res);
|
|
673
|
+
if (responseData.length < 3)
|
|
674
|
+
throw new Error('not connected');
|
|
673
675
|
const opCode = responseData.readUInt8(0);
|
|
674
676
|
const request = responseData.readUInt8(1);
|
|
675
677
|
const result = responseData.readUInt8(2);
|
|
@@ -28,6 +28,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
28
28
|
protected expectedServices: string[];
|
|
29
29
|
protected matching: string[];
|
|
30
30
|
protected backgroundPaused: boolean;
|
|
31
|
+
protected pauseReasons: Set<string>;
|
|
32
|
+
protected pausedSince?: number;
|
|
31
33
|
protected connectTask: InteruptableTask<TaskState, boolean>;
|
|
32
34
|
protected disconnectTask: InteruptableTask<TaskState, boolean>;
|
|
33
35
|
protected scanTask: InteruptableTask<TaskState, void>;
|
|
@@ -64,8 +66,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
64
66
|
scan(props: BleScanProps): Promise<DeviceSettings[]>;
|
|
65
67
|
stopScan(): Promise<boolean>;
|
|
66
68
|
onScanDone(): DeviceSettings[];
|
|
67
|
-
pauseBackgroundActivity(): Promise<void>;
|
|
68
|
-
resumeBackgroundActivity(): Promise<void>;
|
|
69
|
+
pauseBackgroundActivity(reason?: string): Promise<void>;
|
|
70
|
+
resumeBackgroundActivity(reason?: string): Promise<void>;
|
|
69
71
|
pauseLogging(): void;
|
|
70
72
|
resumeLogging(): void;
|
|
71
73
|
isLoggingPaused(): boolean;
|
|
@@ -74,7 +76,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
74
76
|
waitForPeripheral(settings: BleDeviceSettings): Promise<IBlePeripheral>;
|
|
75
77
|
createDeviceSetting(service: BlePeripheralAnnouncement): BleDeviceSettings;
|
|
76
78
|
protected reconnect(): Promise<void>;
|
|
77
|
-
protected startPeripheralScan(retry?: boolean): Promise<void>;
|
|
79
|
+
protected startPeripheralScan(retry?: boolean, force?: boolean): Promise<void>;
|
|
78
80
|
protected stopPeripheralScan(): Promise<void>;
|
|
79
81
|
protected emitDisconnectAllPeripherals(): void;
|
|
80
82
|
protected disconnectAllPeripherals(): Promise<void>;
|
|
@@ -103,6 +105,10 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
103
105
|
protected isSupportedPeripheral(service: BlePeripheralAnnouncement): boolean;
|
|
104
106
|
protected find(service: BlePeripheralAnnouncement): Announcement;
|
|
105
107
|
protected getAll(): Announcement[];
|
|
108
|
+
protected isAnnouncementValid(a: {
|
|
109
|
+
ts: number;
|
|
110
|
+
service: BlePeripheralAnnouncement;
|
|
111
|
+
}): boolean;
|
|
106
112
|
setDebug(enabled: boolean): void;
|
|
107
113
|
addKnownDevice(_settings: BleDeviceSettings): void;
|
|
108
114
|
protected connectBle(): Promise<boolean>;
|
|
@@ -28,6 +28,7 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
28
28
|
getInfo(): BleDeviceIdentifier;
|
|
29
29
|
connect(): Promise<boolean>;
|
|
30
30
|
disconnect(connectionLost?: boolean): Promise<boolean>;
|
|
31
|
+
protected doDisconnect(connectionLost: boolean): Promise<boolean>;
|
|
31
32
|
isConnected(): boolean;
|
|
32
33
|
isConnecting(): boolean;
|
|
33
34
|
onDisconnect(callback: () => void): void;
|
|
@@ -19,6 +19,6 @@ export interface IncyclistInterface extends EventEmitter {
|
|
|
19
19
|
addKnownDevice?(settings: DeviceSettings): void;
|
|
20
20
|
pauseLogging(): void;
|
|
21
21
|
resumeLogging(): void;
|
|
22
|
-
pauseBackgroundActivity?(): Promise<void>;
|
|
23
|
-
resumeBackgroundActivity?(): Promise<void>;
|
|
22
|
+
pauseBackgroundActivity?(reason?: string): Promise<void>;
|
|
23
|
+
resumeBackgroundActivity?(reason?: string): Promise<void>;
|
|
24
24
|
}
|