incyclist-devices 3.0.47 → 3.0.49
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 +75 -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 +75 -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 +15 -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 {
|
|
@@ -24,11 +26,14 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
24
26
|
logDisabled;
|
|
25
27
|
internalEvents;
|
|
26
28
|
services = [];
|
|
29
|
+
pendingWaits = [];
|
|
27
30
|
incompleteServices = [];
|
|
28
31
|
unsupported = [];
|
|
29
32
|
expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
30
33
|
matching = [];
|
|
31
34
|
backgroundPaused = false;
|
|
35
|
+
pauseReasons = new Set();
|
|
36
|
+
pausedSince;
|
|
32
37
|
connectTask;
|
|
33
38
|
disconnectTask;
|
|
34
39
|
scanTask;
|
|
@@ -247,18 +252,30 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
247
252
|
delete this.scanTask;
|
|
248
253
|
return this.buildDeviceSettings(this.matching);
|
|
249
254
|
}
|
|
250
|
-
async pauseBackgroundActivity() {
|
|
251
|
-
if (this.
|
|
255
|
+
async pauseBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
256
|
+
if (this.pauseReasons.has(reason))
|
|
252
257
|
return;
|
|
258
|
+
this.pauseReasons.add(reason);
|
|
259
|
+
const alreadyPaused = this.backgroundPaused;
|
|
253
260
|
this.backgroundPaused = true;
|
|
254
|
-
this.logEvent({ message: 'pausing background activity' });
|
|
261
|
+
this.logEvent({ message: 'pausing background activity', reason, reasons: Array.from(this.pauseReasons).join(','), interface: 'ble' });
|
|
262
|
+
if (alreadyPaused)
|
|
263
|
+
return;
|
|
264
|
+
this.pausedSince = Date.now();
|
|
255
265
|
await this.stopPeripheralScan();
|
|
256
266
|
}
|
|
257
|
-
async resumeBackgroundActivity() {
|
|
258
|
-
if (!this.
|
|
267
|
+
async resumeBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
268
|
+
if (!this.pauseReasons.delete(reason))
|
|
259
269
|
return;
|
|
270
|
+
const reasons = Array.from(this.pauseReasons).join(',');
|
|
271
|
+
if (this.pauseReasons.size > 0) {
|
|
272
|
+
this.logEvent({ message: 'resuming background activity skipped', reason, reasons, interface: 'ble' });
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
260
275
|
this.backgroundPaused = false;
|
|
261
|
-
this.
|
|
276
|
+
const pausedMs = this.pausedSince ? Date.now() - this.pausedSince : 0;
|
|
277
|
+
delete this.pausedSince;
|
|
278
|
+
this.logEvent({ message: 'resuming background activity', reason, reasons, pausedMs, interface: 'ble' });
|
|
262
279
|
await this.startPeripheralScan();
|
|
263
280
|
}
|
|
264
281
|
pauseLogging() {
|
|
@@ -293,24 +310,57 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
293
310
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
294
311
|
if (peripheral)
|
|
295
312
|
return Promise.resolve(peripheral);
|
|
296
|
-
return new Promise((done) => {
|
|
313
|
+
return new Promise((done, reject) => {
|
|
314
|
+
const temporary = this.backgroundPaused;
|
|
297
315
|
const wasDiscovering = this.isDiscovering();
|
|
298
|
-
|
|
299
|
-
|
|
316
|
+
const tsStart = Date.now();
|
|
317
|
+
let timer;
|
|
318
|
+
const finish = () => {
|
|
319
|
+
this.off('device', onDevice);
|
|
320
|
+
if (timer)
|
|
321
|
+
clearTimeout(timer);
|
|
322
|
+
this.pendingWaits = this.pendingWaits.filter(w => w !== pending);
|
|
323
|
+
if (temporary || !wasDiscovering)
|
|
324
|
+
this.stopPeripheralScan().catch(() => { });
|
|
325
|
+
};
|
|
326
|
+
const pending = {
|
|
327
|
+
address: settings.address,
|
|
328
|
+
name: settings.name,
|
|
329
|
+
cancel: () => {
|
|
330
|
+
finish();
|
|
331
|
+
reject(new Error('peripheral wait cancelled'));
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
this.pendingWaits.push(pending);
|
|
300
335
|
const onDevice = (device) => {
|
|
301
336
|
if (device.address === settings.address) {
|
|
302
337
|
const peripheral = this.createPeripheralFromSettings(device);
|
|
303
338
|
if (peripheral) {
|
|
304
|
-
|
|
305
|
-
if (
|
|
306
|
-
this.
|
|
339
|
+
finish();
|
|
340
|
+
if (temporary)
|
|
341
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: true });
|
|
307
342
|
done(peripheral);
|
|
308
343
|
}
|
|
309
344
|
}
|
|
310
345
|
};
|
|
311
346
|
this.on('device', onDevice);
|
|
347
|
+
if (temporary) {
|
|
348
|
+
timer = setTimeout(() => {
|
|
349
|
+
finish();
|
|
350
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: false });
|
|
351
|
+
reject(new Error('peripheral not found'));
|
|
352
|
+
}, TEMPORARY_SCAN_TIMEOUT);
|
|
353
|
+
this.startPeripheralScan(false, true).catch(() => { });
|
|
354
|
+
}
|
|
355
|
+
else if (!wasDiscovering)
|
|
356
|
+
this.startPeripheralScan();
|
|
312
357
|
});
|
|
313
358
|
}
|
|
359
|
+
cancelWait(settings) {
|
|
360
|
+
this.pendingWaits
|
|
361
|
+
.filter(w => settings.address !== undefined ? w.address === settings.address : w.name === settings.name)
|
|
362
|
+
.forEach(w => w.cancel());
|
|
363
|
+
}
|
|
314
364
|
createDeviceSetting(service) {
|
|
315
365
|
const { peripheral } = service;
|
|
316
366
|
if (peripheral.address === undefined || peripheral.address === '')
|
|
@@ -336,8 +386,8 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
336
386
|
await this.disconnect();
|
|
337
387
|
await this.connectInternal(true);
|
|
338
388
|
}
|
|
339
|
-
async startPeripheralScan(retry = false) {
|
|
340
|
-
if (this.backgroundPaused)
|
|
389
|
+
async startPeripheralScan(retry = false, force = false) {
|
|
390
|
+
if (this.backgroundPaused && !force)
|
|
341
391
|
return;
|
|
342
392
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
343
393
|
return;
|
|
@@ -357,7 +407,7 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
357
407
|
this.logError(err, 'discover');
|
|
358
408
|
}
|
|
359
409
|
if (this.discoverTask.getState().result !== 'stopped') {
|
|
360
|
-
this.startPeripheralScan(true);
|
|
410
|
+
this.startPeripheralScan(true, force).catch(err => this.logError(err, 'discover'));
|
|
361
411
|
}
|
|
362
412
|
}
|
|
363
413
|
async stopPeripheralScan() {
|
|
@@ -644,10 +694,18 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
644
694
|
return supported.length > 0;
|
|
645
695
|
}
|
|
646
696
|
find(service) {
|
|
647
|
-
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address &&
|
|
697
|
+
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address && this.isAnnouncementValid(a));
|
|
648
698
|
}
|
|
649
699
|
getAll() {
|
|
650
|
-
return this.services.filter(a =>
|
|
700
|
+
return this.services.filter(a => this.isAnnouncementValid(a));
|
|
701
|
+
}
|
|
702
|
+
isAnnouncementValid(a) {
|
|
703
|
+
if (a.ts > Date.now() - BLE_EXPIRATION_TIMEOUT)
|
|
704
|
+
return true;
|
|
705
|
+
if (!this.backgroundPaused)
|
|
706
|
+
return false;
|
|
707
|
+
const id = a.service?.peripheral?.id;
|
|
708
|
+
return id !== undefined && this.connectedPeripherals.some(p => p.id === id);
|
|
651
709
|
}
|
|
652
710
|
setDebug(enabled) {
|
|
653
711
|
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 {
|
|
@@ -21,11 +23,14 @@ export class BleInterface extends EventEmitter {
|
|
|
21
23
|
logDisabled;
|
|
22
24
|
internalEvents;
|
|
23
25
|
services = [];
|
|
26
|
+
pendingWaits = [];
|
|
24
27
|
incompleteServices = [];
|
|
25
28
|
unsupported = [];
|
|
26
29
|
expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
27
30
|
matching = [];
|
|
28
31
|
backgroundPaused = false;
|
|
32
|
+
pauseReasons = new Set();
|
|
33
|
+
pausedSince;
|
|
29
34
|
connectTask;
|
|
30
35
|
disconnectTask;
|
|
31
36
|
scanTask;
|
|
@@ -244,18 +249,30 @@ export class BleInterface extends EventEmitter {
|
|
|
244
249
|
delete this.scanTask;
|
|
245
250
|
return this.buildDeviceSettings(this.matching);
|
|
246
251
|
}
|
|
247
|
-
async pauseBackgroundActivity() {
|
|
248
|
-
if (this.
|
|
252
|
+
async pauseBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
253
|
+
if (this.pauseReasons.has(reason))
|
|
249
254
|
return;
|
|
255
|
+
this.pauseReasons.add(reason);
|
|
256
|
+
const alreadyPaused = this.backgroundPaused;
|
|
250
257
|
this.backgroundPaused = true;
|
|
251
|
-
this.logEvent({ message: 'pausing background activity' });
|
|
258
|
+
this.logEvent({ message: 'pausing background activity', reason, reasons: Array.from(this.pauseReasons).join(','), interface: 'ble' });
|
|
259
|
+
if (alreadyPaused)
|
|
260
|
+
return;
|
|
261
|
+
this.pausedSince = Date.now();
|
|
252
262
|
await this.stopPeripheralScan();
|
|
253
263
|
}
|
|
254
|
-
async resumeBackgroundActivity() {
|
|
255
|
-
if (!this.
|
|
264
|
+
async resumeBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
265
|
+
if (!this.pauseReasons.delete(reason))
|
|
256
266
|
return;
|
|
267
|
+
const reasons = Array.from(this.pauseReasons).join(',');
|
|
268
|
+
if (this.pauseReasons.size > 0) {
|
|
269
|
+
this.logEvent({ message: 'resuming background activity skipped', reason, reasons, interface: 'ble' });
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
257
272
|
this.backgroundPaused = false;
|
|
258
|
-
this.
|
|
273
|
+
const pausedMs = this.pausedSince ? Date.now() - this.pausedSince : 0;
|
|
274
|
+
delete this.pausedSince;
|
|
275
|
+
this.logEvent({ message: 'resuming background activity', reason, reasons, pausedMs, interface: 'ble' });
|
|
259
276
|
await this.startPeripheralScan();
|
|
260
277
|
}
|
|
261
278
|
pauseLogging() {
|
|
@@ -290,24 +307,57 @@ export class BleInterface extends EventEmitter {
|
|
|
290
307
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
291
308
|
if (peripheral)
|
|
292
309
|
return Promise.resolve(peripheral);
|
|
293
|
-
return new Promise((done) => {
|
|
310
|
+
return new Promise((done, reject) => {
|
|
311
|
+
const temporary = this.backgroundPaused;
|
|
294
312
|
const wasDiscovering = this.isDiscovering();
|
|
295
|
-
|
|
296
|
-
|
|
313
|
+
const tsStart = Date.now();
|
|
314
|
+
let timer;
|
|
315
|
+
const finish = () => {
|
|
316
|
+
this.off('device', onDevice);
|
|
317
|
+
if (timer)
|
|
318
|
+
clearTimeout(timer);
|
|
319
|
+
this.pendingWaits = this.pendingWaits.filter(w => w !== pending);
|
|
320
|
+
if (temporary || !wasDiscovering)
|
|
321
|
+
this.stopPeripheralScan().catch(() => { });
|
|
322
|
+
};
|
|
323
|
+
const pending = {
|
|
324
|
+
address: settings.address,
|
|
325
|
+
name: settings.name,
|
|
326
|
+
cancel: () => {
|
|
327
|
+
finish();
|
|
328
|
+
reject(new Error('peripheral wait cancelled'));
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
this.pendingWaits.push(pending);
|
|
297
332
|
const onDevice = (device) => {
|
|
298
333
|
if (device.address === settings.address) {
|
|
299
334
|
const peripheral = this.createPeripheralFromSettings(device);
|
|
300
335
|
if (peripheral) {
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
this.
|
|
336
|
+
finish();
|
|
337
|
+
if (temporary)
|
|
338
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: true });
|
|
304
339
|
done(peripheral);
|
|
305
340
|
}
|
|
306
341
|
}
|
|
307
342
|
};
|
|
308
343
|
this.on('device', onDevice);
|
|
344
|
+
if (temporary) {
|
|
345
|
+
timer = setTimeout(() => {
|
|
346
|
+
finish();
|
|
347
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: false });
|
|
348
|
+
reject(new Error('peripheral not found'));
|
|
349
|
+
}, TEMPORARY_SCAN_TIMEOUT);
|
|
350
|
+
this.startPeripheralScan(false, true).catch(() => { });
|
|
351
|
+
}
|
|
352
|
+
else if (!wasDiscovering)
|
|
353
|
+
this.startPeripheralScan();
|
|
309
354
|
});
|
|
310
355
|
}
|
|
356
|
+
cancelWait(settings) {
|
|
357
|
+
this.pendingWaits
|
|
358
|
+
.filter(w => settings.address !== undefined ? w.address === settings.address : w.name === settings.name)
|
|
359
|
+
.forEach(w => w.cancel());
|
|
360
|
+
}
|
|
311
361
|
createDeviceSetting(service) {
|
|
312
362
|
const { peripheral } = service;
|
|
313
363
|
if (peripheral.address === undefined || peripheral.address === '')
|
|
@@ -333,8 +383,8 @@ export class BleInterface extends EventEmitter {
|
|
|
333
383
|
await this.disconnect();
|
|
334
384
|
await this.connectInternal(true);
|
|
335
385
|
}
|
|
336
|
-
async startPeripheralScan(retry = false) {
|
|
337
|
-
if (this.backgroundPaused)
|
|
386
|
+
async startPeripheralScan(retry = false, force = false) {
|
|
387
|
+
if (this.backgroundPaused && !force)
|
|
338
388
|
return;
|
|
339
389
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
340
390
|
return;
|
|
@@ -354,7 +404,7 @@ export class BleInterface extends EventEmitter {
|
|
|
354
404
|
this.logError(err, 'discover');
|
|
355
405
|
}
|
|
356
406
|
if (this.discoverTask.getState().result !== 'stopped') {
|
|
357
|
-
this.startPeripheralScan(true);
|
|
407
|
+
this.startPeripheralScan(true, force).catch(err => this.logError(err, 'discover'));
|
|
358
408
|
}
|
|
359
409
|
}
|
|
360
410
|
async stopPeripheralScan() {
|
|
@@ -641,10 +691,18 @@ export class BleInterface extends EventEmitter {
|
|
|
641
691
|
return supported.length > 0;
|
|
642
692
|
}
|
|
643
693
|
find(service) {
|
|
644
|
-
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address &&
|
|
694
|
+
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address && this.isAnnouncementValid(a));
|
|
645
695
|
}
|
|
646
696
|
getAll() {
|
|
647
|
-
return this.services.filter(a =>
|
|
697
|
+
return this.services.filter(a => this.isAnnouncementValid(a));
|
|
698
|
+
}
|
|
699
|
+
isAnnouncementValid(a) {
|
|
700
|
+
if (a.ts > Date.now() - BLE_EXPIRATION_TIMEOUT)
|
|
701
|
+
return true;
|
|
702
|
+
if (!this.backgroundPaused)
|
|
703
|
+
return false;
|
|
704
|
+
const id = a.service?.peripheral?.id;
|
|
705
|
+
return id !== undefined && this.connectedPeripherals.some(p => p.id === id);
|
|
648
706
|
}
|
|
649
707
|
setDebug(enabled) {
|
|
650
708
|
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);
|
|
@@ -23,11 +23,18 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
23
23
|
protected logDisabled: boolean;
|
|
24
24
|
protected internalEvents: EventEmitter;
|
|
25
25
|
protected services: Announcement[];
|
|
26
|
+
protected pendingWaits: Array<{
|
|
27
|
+
address?: string;
|
|
28
|
+
name?: string;
|
|
29
|
+
cancel: () => void;
|
|
30
|
+
}>;
|
|
26
31
|
protected incompleteServices: BlePeripheralAnnouncement[];
|
|
27
32
|
protected unsupported: BlePeripheralAnnouncement[];
|
|
28
33
|
protected expectedServices: string[];
|
|
29
34
|
protected matching: string[];
|
|
30
35
|
protected backgroundPaused: boolean;
|
|
36
|
+
protected pauseReasons: Set<string>;
|
|
37
|
+
protected pausedSince?: number;
|
|
31
38
|
protected connectTask: InteruptableTask<TaskState, boolean>;
|
|
32
39
|
protected disconnectTask: InteruptableTask<TaskState, boolean>;
|
|
33
40
|
protected scanTask: InteruptableTask<TaskState, void>;
|
|
@@ -64,17 +71,18 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
64
71
|
scan(props: BleScanProps): Promise<DeviceSettings[]>;
|
|
65
72
|
stopScan(): Promise<boolean>;
|
|
66
73
|
onScanDone(): DeviceSettings[];
|
|
67
|
-
pauseBackgroundActivity(): Promise<void>;
|
|
68
|
-
resumeBackgroundActivity(): Promise<void>;
|
|
74
|
+
pauseBackgroundActivity(reason?: string): Promise<void>;
|
|
75
|
+
resumeBackgroundActivity(reason?: string): Promise<void>;
|
|
69
76
|
pauseLogging(): void;
|
|
70
77
|
resumeLogging(): void;
|
|
71
78
|
isLoggingPaused(): boolean;
|
|
72
79
|
createPeripheral(announcement: BlePeripheralAnnouncement): IBlePeripheral;
|
|
73
80
|
createPeripheralFromSettings(settings: BleDeviceSettings): IBlePeripheral;
|
|
74
81
|
waitForPeripheral(settings: BleDeviceSettings): Promise<IBlePeripheral>;
|
|
82
|
+
cancelWait(settings: BleDeviceSettings): void;
|
|
75
83
|
createDeviceSetting(service: BlePeripheralAnnouncement): BleDeviceSettings;
|
|
76
84
|
protected reconnect(): Promise<void>;
|
|
77
|
-
protected startPeripheralScan(retry?: boolean): Promise<void>;
|
|
85
|
+
protected startPeripheralScan(retry?: boolean, force?: boolean): Promise<void>;
|
|
78
86
|
protected stopPeripheralScan(): Promise<void>;
|
|
79
87
|
protected emitDisconnectAllPeripherals(): void;
|
|
80
88
|
protected disconnectAllPeripherals(): Promise<void>;
|
|
@@ -103,6 +111,10 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
103
111
|
protected isSupportedPeripheral(service: BlePeripheralAnnouncement): boolean;
|
|
104
112
|
protected find(service: BlePeripheralAnnouncement): Announcement;
|
|
105
113
|
protected getAll(): Announcement[];
|
|
114
|
+
protected isAnnouncementValid(a: {
|
|
115
|
+
ts: number;
|
|
116
|
+
service: BlePeripheralAnnouncement;
|
|
117
|
+
}): boolean;
|
|
106
118
|
setDebug(enabled: boolean): void;
|
|
107
119
|
addKnownDevice(_settings: BleDeviceSettings): void;
|
|
108
120
|
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
|
}
|