title: iOS Core Bluetooth notify app when Bluetooth device connects
body: <p>Ive been working on this project for quite some time. Needing to collect data from a bluetooth device periodically when the app is in the app switcher in the background.</p>
  
  <p>My most recent thought is using Core Bluetooth to notify my app when a bluetooth device has connected so I can check if its my device and then do what I need to do.</p>
  
  <p>Or am I miss interpreting the docs when it says: "didConnectPeripheral"?</p>
  
  <p>Im having trouble finding the function in my CBCentralManager object to start a "watcher" of some sort to give me these notifications.</p>
  
  <p>Am I on the wrong path here?</p>
  
  <p>Thanks.</p>
  
  <p>Code attempt to use core bluetooth:</p>
  
  <pre><code>CBCentralManager *mgr;
  
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
      [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  
      mgr = [CBCentralManager alloc];
      CBPeripheral *peripheral = [CBPeripheral alloc];
      [mgr connectPeripheral:peripheral options:nil];
      // Override point for customization after application launch.
      return YES;
  }
  
  - (void) mgr:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
      NSLog(@"Device Connected!");
  }
  - (void) mgr:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
      NSLog(@"Device Disconnected!");
  }
  - (void)mgr:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
      NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData);
      //Do something when a peripheral is discovered.
  }
  </code></pre>
  
  <p>Gets error: </p>
  
  <pre><code>2014-11-21 23:30:27.730 TelematicsService[436:185202] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
  *** First throw call stack:
  (0x286db49f 0x35e91c8b 0x285fb873 0x285fb657 0x283adb85 0xf005f 0x2bc084f1 0x2bdfd43f 0x2bdff98b 0x2be0a209 0x2bdfe217 0x2ee6c0d1 0x286a1d7d 0x286a1041 0x2869fb7b 0x285ed3c1 0x285ed1d3 0x2bc021bf 0x2bbfcfa1 0xf2a29 0x36411aaf)
  libc++abi.dylib: terminating with uncaught exception of type NSException
  </code></pre>
  
  <p>Due to peripheral not being fully defined I believe</p>