@@ -50,7 +50,7 @@ - (void)pluginInitialize {
5050 options[CBCentralManagerOptionRestoreIdentifierKey] = restoreIdentifier;
5151 }
5252
53- peripherals = [NSMutableSet new ];
53+ peripherals = [NSMutableDictionary new ];
5454 manager = [[CBCentralManager alloc ] initWithDelegate: self queue: nil options: options];
5555
5656 restoredState = nil ;
@@ -534,7 +534,7 @@ - (void)connectedPeripheralsWithServices:(CDVInvokedUrlCommand*)command {
534534 NSMutableArray <NSDictionary *> *connected = [NSMutableArray new ];
535535
536536 for (CBPeripheral *peripheral in connectedPeripherals) {
537- [peripherals addObject : peripheral];
537+ [peripherals setObject : peripheral forKey: peripheral.identifier ];
538538 [connected addObject: [peripheral asDictionary ]];
539539 }
540540
@@ -562,7 +562,7 @@ - (void)peripheralsWithIdentifiers:(CDVInvokedUrlCommand*)command {
562562 NSMutableArray <NSDictionary *> *found = [NSMutableArray new ];
563563
564564 for (CBPeripheral *peripheral in foundPeripherals) {
565- [peripherals addObject : peripheral]; // TODO do we save these?
565+ [peripherals setObject : peripheral forKey: peripheral.identifier];
566566 [found addObject: [peripheral asDictionary ]];
567567 }
568568
@@ -677,7 +677,7 @@ -(void)stopScanTimer:(NSTimer *)timer {
677677
678678- (void )centralManager : (CBCentralManager *)central didDiscoverPeripheral : (CBPeripheral *)peripheral advertisementData : (NSDictionary *)advertisementData RSSI : (NSNumber *)RSSI {
679679
680- [peripherals addObject : peripheral];
680+ [peripherals setObject : peripheral forKey: peripheral.identifier ];
681681 [peripheral setAdvertisementData: advertisementData RSSI: RSSI];
682682
683683 if (discoverPeripheralCallbackId) {
@@ -710,11 +710,11 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central
710710 }
711711
712712 // check and handle disconnected peripherals
713- for (CBPeripheral * peripheral in peripherals ) {
713+ [peripherals enumerateKeysAndObjectsUsingBlock: ^( id key, CBPeripheral* peripheral, BOOL * stop ) {
714714 if (peripheral.state == CBPeripheralStateDisconnected) {
715715 [self centralManager: central didDisconnectPeripheral: peripheral error: nil ];
716716 }
717- }
717+ }];
718718}
719719
720720- (void )centralManager : (CBCentralManager *)central didConnectPeripheral : (CBPeripheral *)peripheral {
@@ -820,7 +820,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForServi
820820 [self .commandDelegate sendPluginResult: pluginResult callbackId: connectCallbackId];
821821 }
822822 return ;
823- }
823+ }
824824
825825 NSLog (@" Found characteristics for service %@ " , service);
826826 for (CBCharacteristic *characteristic in service.characteristics ) {
@@ -1014,26 +1014,15 @@ - (void)peripheral:(CBPeripheral *)peripheral didOpenL2CAPChannel:(CBL2CAPChanne
10141014#pragma mark - internal implemetation
10151015
10161016- (CBPeripheral*)findPeripheralByUUID : (NSUUID *)uuid {
1017- CBPeripheral *peripheral = nil ;
1018-
1019- for (CBPeripheral *p in peripherals) {
1020-
1021- NSUUID * other = p.identifier ;
1022-
1023- if ([uuid isEqual: other]) {
1024- peripheral = p;
1025- break ;
1026- }
1027- }
1028- return peripheral;
1017+ return [peripherals objectForKey: uuid];
10291018}
10301019
10311020- (CBPeripheral*)retrievePeripheralWithUUID : (NSUUID *)typedUUID {
10321021 NSArray *existingPeripherals = [manager retrievePeripheralsWithIdentifiers: @[typedUUID]];
10331022 CBPeripheral *peripheral = nil ;
10341023 if ([existingPeripherals count ] > 0 ) {
10351024 peripheral = [existingPeripherals firstObject ];
1036- [peripherals addObject : peripheral];
1025+ [peripherals setObject : peripheral forKey: peripheral.identifier ];
10371026 }
10381027 return peripheral;
10391028}
0 commit comments