Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert peripherals list to dict on iOS to avoid ghost peripherals #1039 #1040

Merged
merged 1 commit into from
Feb 13, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/ios/BLECentralPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
NSTimer *scanTimer;
}

@property (strong, nonatomic) NSMutableSet *peripherals;
@property (strong, nonatomic) NSMutableDictionary *peripherals;
@property (strong, nonatomic) CBCentralManager *manager;

- (void)startScanWithOptions:(CDVInvokedUrlCommand *)command;
Expand Down
29 changes: 9 additions & 20 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 setObject: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 @@ -820,7 +820,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForServi
[self.commandDelegate sendPluginResult:pluginResult callbackId:connectCallbackId];
}
return;
}
}

NSLog(@"Found characteristics for service %@", service);
for (CBCharacteristic *characteristic in service.characteristics) {
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
Loading