Skip to content

Commit e55606f

Browse files
author
Marco Crespi
committed
Merge branch 'develop'
2 parents 88a55ca + 9418720 commit e55606f

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

lib/bindings/win/Peripheral.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

lib/bindings/win/Peripheral.js

+23-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/win/Peripheral.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bindings/win/Peripheral.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@ export class WinPeripheral extends Peripheral {
2121
public async connect(): Promise<GattRemote> {
2222
this._state = 'connecting';
2323

24-
this.adapter.noble.connect(this.uuid);
24+
await new Promise<void>((res, rej) => {
25+
const cleanup = () => {
26+
clearTimeout(timer);
27+
this.adapter.noble.off('connect', connHandler);
28+
};
29+
const resolve = () => {
30+
cleanup();
31+
res();
32+
};
33+
const reject = (err: Error) => {
34+
cleanup();
35+
rej(err);
36+
};
37+
38+
const timer = setTimeout(() => reject(new Error('Connecting timed out')), 10000);
2539

26-
await new Promise<void>((resolve, reject) => {
2740
const connHandler = (uuid: string, err: Error) => {
2841
if (uuid === this.uuid) {
29-
this.adapter.noble.off('connect', connHandler);
3042
if (err) {
3143
reject(err);
3244
} else {
@@ -35,6 +47,8 @@ export class WinPeripheral extends Peripheral {
3547
}
3648
};
3749
this.adapter.noble.on('connect', connHandler);
50+
51+
this.adapter.noble.connect(this.uuid);
3852
});
3953

4054
this._gatt = new WinGatt(this);
@@ -45,16 +59,23 @@ export class WinPeripheral extends Peripheral {
4559
public async disconnect(): Promise<void> {
4660
this._state = 'disconnecting';
4761

48-
this.adapter.noble.disconnect(this.uuid);
62+
await new Promise<void>((res) => {
63+
const resolve = () => {
64+
clearTimeout(timer);
65+
this.adapter.noble.off('connect', disconnHandler);
66+
res();
67+
};
68+
69+
const timer = setTimeout(resolve, 10000);
4970

50-
await new Promise<void>((resolve) => {
5171
const disconnHandler = (uuid: string) => {
5272
if (uuid === this.uuid) {
53-
this.adapter.noble.off('disconnect', disconnHandler);
5473
resolve();
5574
}
5675
};
5776
this.adapter.noble.on('disconnect', disconnHandler);
77+
78+
this.adapter.noble.disconnect(this.uuid);
5879
});
5980

6081
this._state = 'disconnected';

0 commit comments

Comments
 (0)