title: Objective-c bluetooth says it's connected but CBCentralManager does not discover it
body: <p>In my app, I search for previously connected devices with the delegate method <code>retrieveConnectedPeripheralsWithServices</code> first, then scan for nearby devices if nothing is found. Here is the code</p>
  
  <pre><code>- (void)searchForPeripherals {
      NSLog(@"***  scanning for Bluetooth peripherals...  ***");
  
      //Check to see if any devices were connected already
      if(self.ourPeripheral != nil) {
          [self connectToDevice];
  
      } else {
          //Search for previously paired devices...
          bool foundPairedDevices = [self checkForAndConnectToPreviouslyPairedDevices];
  
          //None found, scan for new devices nearby...
          if(!foundPairedDevices) {
              NSLog(@"No paired boxes found, checking device powered state...");
  
              //If the app user has "bluetooth" option turned on
              if(self.centralManager.state == CBCentralManagerStatePoweredOn) {
                  NSLog(@"--- central manager powered on.");
                  NSLog(@"...begin scanning...");
  
                  [self.centralManager scanForPeripheralsWithServices:nil
                                                              options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @(YES)}];
  
              //No, user has "bluetooth" turned off
              } else {
                  NSLog(@"--- central manager is NOT powered on.");
              }
          }
      }
  }
  
  
  - (bool)checkForAndConnectToPreviouslyPairedDevices {
      NSLog(@"our peripheral device: %@", self.ourPeripheral);
  
      NSArray *dcBoxesFound = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]];
      NSLog(@"Previously paired DC boxes?: %@", dcBoxesFound);
  
      //Are there any previously paired devices?
      if(dcBoxesFound != nil &amp;&amp; [dcBoxesFound count] &gt; 0) {
          CBPeripheral *newestBoxFound = [dcBoxesFound firstObject];
          newestBoxFound.delegate = self;
  
          self.ourPeripheral = newestBoxFound;
          self.deviceUUID = [CBUUID UUIDWithString:[newestBoxFound.identifier UUIDString]];
  
          [self connectToDevice];
  
          return YES;
  
      } else {
          return NO;
      }
  }
  
  
  - (void)connectToDevice {
      if(self.ourPeripheral != nil) {
          [self.centralManager connectPeripheral:self.ourPeripheral options:nil];
      }
  }
  
  
  - (void)centralManager:(CBCentralManager *)central
   didDiscoverPeripheral:(CBPeripheral *)peripheral
       advertisementData:(NSDictionary *)advertisementData
                    RSSI:(NSNumber *)RSSI {
  
      NSLog(@"scanned and found this peripheral: %@", peripheral);
      NSLog(@"advertisment data: %@", advertisementData);
  
      if(!self.isConnectedToDevice &amp;&amp; [[advertisementData objectForKey:@"kCBAdvDataLocalName"] localizedCaseInsensitiveContainsString:@"dc-"]) {
          NSLog(@"\n");
          NSLog(@"-------------------------------------------");
          NSLog(@"DC peripheral found! Attempting to connect to the following...: %@", peripheral);
  
          peripheral.delegate = self;
          self.ourPeripheral = peripheral;
  
          self.deviceUUID = [CBUUID UUIDWithString:[peripheral.identifier UUIDString]];
  
          [self connectToDevice];
  
          NSLog(@"-------------------------------------------");
          NSLog(@"\n");
      }
  }
  
  
  - (void)centralManager:(CBCentralManager *)central
    didConnectPeripheral:(CBPeripheral *)peripheral {
      NSLog(@"--- didConnectPeripheral");
  
      peripheral.delegate = self;
      [peripheral discoverServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]];
  }
  </code></pre>
  
  <p><strong>The Problem</strong></p>
  
  <p>When I start up the app it usually connects just fine. Everything is all good. Then at very unpredictable and odd times the BT connection gets "stuck" somehow. What I mean by stuck is that it never truely connects. The blue light on my box goes on as though the system has connected to it but in my app this is what happens.</p>
  
  <p>1) The <code>retrieveConnectedPeripheralsWithServices</code> method finds an empty array of previously connected devices, telling the method that called it that there are no previously connected devices found.</p>
  
  <p>2) The <code>scanForPeripheralsWithServices:nil</code> method is fired and begins scanning with the <code>didDiscoverPeripheral</code> method.</p>
  
  <p>3) The <code>didDiscoverPeripheral</code> never even once discovers any boxes nearby with the prefix in <code>kCBAdvDataLocalName</code> of "dc-" something or other</p>
  
  <p>If I were to go into iOS settings -> bluetooth -> forget this device (have to hit it twice which makes me feel like it's "stuck" somehow) and then actually turn off the BT option all together... Wait 2 seconds and then turn it back on again... then re-launch the application, it all loads fine. </p>
  
  <p>1) The app starts up </p>
  
  <p>2) See's no previously connected devices </p>
  
  <p>3) Scans for peripherals and finds my prefixed box name "dc-whatever"</p>
  
  <p>4) Asks me to pair with BT device and then I have full functionality back</p>
  
  <p>If I then shut down the app and re-launch it, the <code>retrieveConnectedPeripheralsWithServices</code> finds my previously connected box without trouble and connects seamlessly...</p>
  
  <p>So... what is going on here? Why does it seem to get "stuck" randomly some times?</p>
  
  <p><strong><em>EDIT:</em></strong></p>
  
  <p>I do realize that pairing and connecting are not the same thing so some method are named really poorly.</p>