Skip to content

Commit c6eea1c

Browse files
author
Marco Crespi
committed
fix(hci): Fix peripheral connects
1 parent 355b912 commit c6eea1c

File tree

9 files changed

+19
-35
lines changed

9 files changed

+19
-35
lines changed

lib/bindings/hci/Adapter.js

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/Adapter.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/Peripheral.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/Peripheral.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/misc/Hci.js

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/misc/Hci.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bindings/hci/Adapter.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,12 @@ export class HciAdapter extends Adapter {
115115
throw new Error(`Advertising and connecting simultaneously is supported with Bluetooth 4.2+`);
116116
}
117117

118-
let timedOut = false;
119118
// Create the error outside the scope to have the correct error stack
120119
const timeoutError = new Error('Connecting timed out');
121-
const timeout = new Promise<void>((_, reject) =>
122-
setTimeout(() => {
123-
timedOut = true;
124-
reject(timeoutError);
125-
}, 10000)
126-
);
120+
const timeout = new Promise<void>((_, reject) => setTimeout(() => reject(timeoutError), 10000));
127121

128122
const connet = async () => {
129123
const handle = await this.hci.createLeConn(peripheral.address, peripheral.addressType);
130-
if (timedOut) {
131-
return;
132-
}
133124

134125
this.uuidToHandle.set(peripheral.uuid, handle);
135126
this.handleToUUID.set(handle, peripheral.uuid);

src/bindings/hci/Peripheral.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export class HciPeripheral extends Peripheral {
1717
private signaling: Signaling;
1818

1919
public async connect(): Promise<void> {
20+
if (this._state === 'connected') {
21+
return;
22+
}
23+
2024
this._state = 'connecting';
2125
await this.adapter.connect(this);
2226
}
@@ -34,6 +38,10 @@ export class HciPeripheral extends Peripheral {
3438
}
3539

3640
public async disconnect(): Promise<void> {
41+
if (this._state === 'disconnected') {
42+
return;
43+
}
44+
3745
this._state = 'disconnecting';
3846
await this.adapter.disconnect(this);
3947
}

src/bindings/hci/misc/Hci.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,6 @@ export class Hci extends EventEmitter {
585585
cmd.writeUInt16LE(0x0004, 25); // min ce length
586586
cmd.writeUInt16LE(0x0006, 27); // max ce length
587587

588-
try {
589-
// Cancel any other connection requests before trying this one
590-
await this.cancelLeConn();
591-
} catch {
592-
// NO-OP
593-
}
594-
595588
const origScope = new Error();
596589

597590
return new Promise<number>(async (resolve, reject) => {

0 commit comments

Comments
 (0)