Skip to content

Commit

Permalink
Convert peripherals list to dict on iOS to avoid ghost peripherals #1039
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Oct 25, 2024
1 parent 860455c commit 8f70d48
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/ios/BLECentralPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (void)pluginInitialize {
options[CBCentralManagerOptionRestoreIdentifierKey] = restoreIdentifier;
}

peripherals = [NSMutableSet new];
peripherals = [NSMutableDictionary new];
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];

restoredState = nil;
Expand Down Expand Up @@ -534,7 +534,7 @@ - (void)connectedPeripheralsWithServices:(CDVInvokedUrlCommand*)command {
NSMutableArray<NSDictionary *> *connected = [NSMutableArray new];

for (CBPeripheral *peripheral in connectedPeripherals) {
[peripherals addObject:peripheral];
[peripherals setObject:peripheral forKey:peripheral.identifier];
[connected addObject:[peripheral asDictionary]];
}

Expand Down Expand Up @@ -562,7 +562,7 @@ - (void)peripheralsWithIdentifiers:(CDVInvokedUrlCommand*)command {
NSMutableArray<NSDictionary *> *found = [NSMutableArray new];

for (CBPeripheral *peripheral in foundPeripherals) {
[peripherals addObject:peripheral]; // TODO do we save these?
[peripherals setObject:peripheral forKey:peripheral.identifier];
[found addObject:[peripheral asDictionary]];
}

Expand Down Expand Up @@ -677,7 +677,7 @@ -(void)stopScanTimer:(NSTimer *)timer {

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

[peripherals addObject:peripheral];
[peripherals setbject:peripheral forKey:peripheral.identifier];
[peripheral setAdvertisementData:advertisementData RSSI:RSSI];

if (discoverPeripheralCallbackId) {
Expand Down Expand Up @@ -710,11 +710,11 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central
}

// check and handle disconnected peripherals
for (CBPeripheral *peripheral in peripherals) {
[peripherals enumerateKeysAndObjectsUsingBlock:^(id key, CBPeripheral* peripheral, BOOL* stop) {
if (peripheral.state == CBPeripheralStateDisconnected) {
[self centralManager:central didDisconnectPeripheral:peripheral error:nil];
}
}
}];
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
Expand Down Expand Up @@ -1014,26 +1014,15 @@ - (void)peripheral:(CBPeripheral *)peripheral didOpenL2CAPChannel:(CBL2CAPChanne
#pragma mark - internal implemetation

- (CBPeripheral*)findPeripheralByUUID:(NSUUID*)uuid {
CBPeripheral *peripheral = nil;

for (CBPeripheral *p in peripherals) {

NSUUID* other = p.identifier;

if ([uuid isEqual:other]) {
peripheral = p;
break;
}
}
return peripheral;
return [peripherals objectForKey:uuid];
}

- (CBPeripheral*)retrievePeripheralWithUUID:(NSUUID*)typedUUID {
NSArray *existingPeripherals = [manager retrievePeripheralsWithIdentifiers:@[typedUUID]];
CBPeripheral *peripheral = nil;
if ([existingPeripherals count] > 0) {
peripheral = [existingPeripherals firstObject];
[peripherals addObject:peripheral];
[peripherals setObject:peripheral forKey:peripheral.identifier];
}
return peripheral;
}
Expand Down

0 comments on commit 8f70d48

Please sign in to comment.