title: iOS 8: When App in Background CBCentralManager Delegate Method didDiscoverPeripheral, didConnectPeripheral, didFailToConnectPeripheral not called
body: <p>I am using Xcode 6 with iOS 8. I am trying to search and connect bluetooth device when app is running in Background. I am using corebluetooth framework. I have added bluetooth-central and bluetooth-peripheral using Xcode capabilities option.</p>
  
  <p>Code:</p>
  
  <p><strong>Appdelegate.h</strong></p>
  
  <pre><code>@interface WSAppDelegate : UIResponder &lt;UIApplicationDelegate, UITabBarControllerDelegate,CBCentralManagerDelegate,CBPeripheralDelegate&gt;
  
  @property (strong, nonatomic) NSString *savedUUID;
  @property (strong, nonatomic) CBCentralManager *CBCM;
  </code></pre>
  
  <p><strong>AppDelegate.m</strong></p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
  {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  
      NSUserDefaults *defaults=[[NSUserDefaults alloc] init];
      self.savedUUID= [defaults stringForKey:@"uuid"];
  
      UIApplication *app = [UIApplication sharedApplication];
      UIBackgroundTaskIdentifier bgTask = 0;
      bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
  
          NSLog(@"Object Created::::");
           self.CBCM = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
  
  
          [app endBackgroundTask:bgTask];
      }];
  
  }
  
  
  - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
      printf("Status of CoreBluetooth central manager changed \n");
  
      if(self.CBCM.state == CBCentralManagerStatePoweredOn)
      {
          //okay your good to go and can now scan
          //  NSLog(@"your good to go and can now scan");
          NSLog(@"Searching for Device::::");
          NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:self.savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere
  
          NSArray *peripherals = [self.CBCM retrievePeripheralsWithIdentifiers:@[uuid]];
  
          for(CBPeripheral *periph in peripherals)
          {
              [self.CBCM connectPeripheral:periph options:nil];
          }
      }
      else
      {
          //Unable to use CentralManager methods so print out the central.state and find out why
          NSLog(@"Unable to use CentralManager methods so print out the central state and find out why:::%d",self.CBCM.state);
      }
  
  
  }
  
  - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
  
      NSLog(@"Peripheral Found::::%@",peripheral);
  
  }
  
  - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
  
      printf("Connection to peripheral with UUID successfull\r\n");
  
  }
  
  - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
  {
      printf("Connection to peripheral with UUID Fail,didFailToConnectPeripheral\r\n");
      printf("Error code was %s\r\n",[[error description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy]);
  
  }
  </code></pre>
  
  <p>didDiscoverPeripheral method is not called so I am using saved uuid of last connected device but it is not connecting and not calling delegates method didConnectPeripheral and  didFailToConnectPeripheral.
  So how to solve this problem and connect with bluetooth while app is running in Background?</p>