Skip to content

Commit e01b9a6

Browse files
author
Marco Crespi
committed
fix(hci): Fix incorrect advertising states
1 parent d70ab0b commit e01b9a6

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

lib/bindings/hci/Adapter.js

Lines changed: 10 additions & 4 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.

src/bindings/hci/Adapter.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,19 @@ export class HciAdapter extends Adapter {
121121
// For BLE <= 4.2:
122122
// - Disable advertising while we're connected.
123123
// - Don't connect if we have a connection in master mode
124+
let advertisingWasDisabled = false;
124125
if (this.hci.hciVersion < 8) {
125126
if ([...this.connectedDevices.values()].some((d) => d.isMaster)) {
126127
throw new Error(`Connecting in master & slave role concurrently is only supported in BLE 5+`);
127128
}
128129

129130
if (this.advertising) {
130-
this.wasAdvertising = true;
131131
try {
132132
await this.stopAdvertising();
133+
this.wasAdvertising = true;
134+
advertisingWasDisabled = true;
133135
} catch (err) {
134136
this.emit('error', `Could not disable advertising before connecting: ${err}`);
135-
this.wasAdvertising = false;
136137
}
137138
}
138139
}
@@ -150,7 +151,7 @@ export class HciAdapter extends Adapter {
150151
peripheral.onDisconnect();
151152

152153
// Re-enable advertising since we didn't establish a connection
153-
if (this.wasAdvertising) {
154+
if (advertisingWasDisabled) {
154155
await this.startAdvertising(this.deviceName, this.advertisedServiceUUIDs);
155156
this.wasAdvertising = false;
156157
}
@@ -199,7 +200,11 @@ export class HciAdapter extends Adapter {
199200
return;
200201
}
201202

202-
await this.gap.stopAdvertising();
203+
try {
204+
await this.gap.stopAdvertising();
205+
} catch {
206+
// NO-OP: Errors here probably mean we already stopped advertising
207+
}
203208

204209
this.advertising = false;
205210
}

0 commit comments

Comments
 (0)