diff --git a/src/ios/BLECentralPlugin.h b/src/ios/BLECentralPlugin.h index e1223dbf..9b03d0d6 100644 --- a/src/ios/BLECentralPlugin.h +++ b/src/ios/BLECentralPlugin.h @@ -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; diff --git a/src/ios/BLECentralPlugin.m b/src/ios/BLECentralPlugin.m index ace165c9..41ec3b2d 100644 --- a/src/ios/BLECentralPlugin.m +++ b/src/ios/BLECentralPlugin.m @@ -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 *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 *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 setObject: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 { @@ -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) { @@ -1014,18 +1014,7 @@ - (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 { @@ -1033,7 +1022,7 @@ - (CBPeripheral*)retrievePeripheralWithUUID:(NSUUID*)typedUUID { CBPeripheral *peripheral = nil; if ([existingPeripherals count] > 0) { peripheral = [existingPeripherals firstObject]; - [peripherals addObject:peripheral]; + [peripherals setObject:peripheral forKey:peripheral.identifier]; } return peripheral; }