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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Convert peripherals list to dict on iOS to avoid ghost peripherals #1039
peitschie committed Oct 25, 2024
commit 8f70d481e3bbe6ac09a562fbdc4d3b6c9cae48f4
27 changes: 8 additions & 19 deletions src/ios/BLECentralPlugin.m
Original file line number Diff line number Diff line change
@@ -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;
@@ -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]];
}

@@ -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]];
}

@@ -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) {
@@ -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 {
@@ -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;
}