Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/mac/src/ble_manager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,27 @@ - (void)updateMtuForPeripheral:(CBPeripheral*) peripheral {

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state != self.lastState && self.lastState == CBManagerStatePoweredOff && central.state == CBManagerStatePoweredOn) {
[self.peripherals enumerateKeysAndObjectsUsingBlock:^(id key, CBPeripheral* peripheral, BOOL *stop) {
if (peripheral.state == CBPeripheralStateConnected) {
[self.centralManager cancelPeripheralConnection:peripheral];
}
}];
// if (central.state != self.lastState && self.lastState == CBManagerStatePoweredOff && central.state == CBManagerStatePoweredOn) {
// [self.peripherals enumerateKeysAndObjectsUsingBlock:^(id key, CBPeripheral* peripheral, BOOL *stop) {
// if (peripheral.state == CBPeripheralStateConnected) {
// [self.centralManager cancelPeripheralConnection:peripheral];
// }
// }];
// }

// Detect transition from any non-powered-on state to powered-on (e.g., after sleep)
BOOL wasPoweredOff = (self.lastState != CBManagerStatePoweredOn);
BOOL isNowPoweredOn = (central.state == CBManagerStatePoweredOn);

if (wasPoweredOff && isNowPoweredOn) {
// Force disconnect ALL peripherals that Noble was tracking before sleep
NSArray *trackedUUIDs = [self.peripherals allKeys];
for (NSString *uuid in trackedUUIDs) {
CBPeripheral *peripheral = [self.peripherals objectForKey:uuid];

// Always force disconnect, regardless of the peripheral's current state
[self.centralManager cancelPeripheralConnection:peripheral];
}
}

self.lastState = central.state;
Expand Down
6 changes: 3 additions & 3 deletions lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class Noble extends EventEmitter {
debug(`stateChange ${state}`);

// If the state is poweredOff and the previous state was poweredOn, clean up the peripherals
if (state === 'poweredOff' && this._state === 'poweredOn') {
this._cleanupPeriperals();
}
// if (state === 'poweredOff' && this._state === 'poweredOn') {
// this._cleanupPeriperals();
// }

this._state = state;
this.emit('stateChange', state);
Expand Down