title: cannot connect to BLE peripheral
body: <p>I cannot seem to connect to a BLE peripheral from OSX. I can scan just fine, but connection never happens. I am saving a local copy of the <code>CBPeripheral*</code> object - in fact, I can query it and the connection state is <code>CBPeripheralStateConnecting</code>, and if I cancel connection it changes back to <code>CBPeripheralStateDisconnected</code>. But it never actually connects - the state doesn't change to <code>CBPeripheralStateConnected</code> and none of my connect callbacks ever get called.</p>
  
  <p>I can use the LightBlue OSX app to connect, scan services, and read characteristics, so I know my dongle is fine. My code is based on someone else's code, and I know that code worked for him. My code also works fine on iOS, but not on my MacBook. I went ahead and upgraded to El Capitan as well, but that didn't help.</p>
  
  <p>I've tried various things: saving discovered peripheral objects in an array, saving the peripheral I'm connecting to in a property, dispatching the call to <code>connectPeripheral</code> to the main thread... no luck. The relevant part of the code is below, and <a href="https://gist.github.com/kived/0e7d910994ba69e807ff5bb9cf662d68" rel="nofollow">my entire BLE code is in a gist</a>.</p>
  
  <pre><code>- (id) init {
      if (self = [super init]) {
          _peripherals = [NSMutableArray new];
          _connecting = nil;
          eventlock = [[NSLock alloc] init];
          events = [[NSMutableArray alloc] init];
          central = [[CBCentralManager alloc] initWithDelegate:self
                                              queue:dispatch_get_main_queue()];
      }
      return self;
  }
  
  - (void) addPeripheral:(CBPeripheral *)peripheral {
      if (![self.peripherals containsObject:peripheral]) {
          NSLog(@"saving peripheral");
          [self.peripherals addObject:peripheral];
      }
  }
  
  - (void) connectPeripheral:(CBPeripheral *)peripheral {
      if (![self.peripherals containsObject:peripheral]) {
          NSLog(@"error: connect to invalid peripheral");
          return;
      }
      __weak cbcentral *this = self;
      NSLog(@"dispatch connect peripheral");
      self.connecting = peripheral;
      dispatch_async(dispatch_get_main_queue(), ^{
          NSLog(@"connect peripheral");
          //this.connecting = peripheral;
          [central connectPeripheral:this.connecting
                   options:nil];
      });
  }
  
  #pragma mark CBCentralManagerDelegate
  
  - (void) centralManager:(CBCentralManager *)central
           didDiscoverPeripheral:(CBPeripheral *)peripheral
           advertisementData:(NSDictionary *)advertisementData
           RSSI:(NSNumber *)RSSI {
      NSLog(@"did discover peripheral");
      [self addPeripheral:peripheral];
      NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didDiscoverPeripheral_advertisementData_RSSI_",
                                                        central,
                                                        peripheral,
                                                        advertisementData,
                                                        RSSI,
                                                        nil];
      [self queueEvent:event];
  }
  
  - (void) centralManager:(CBCentralManager *)central
           didConnectPeripheral:(CBPeripheral *)peripheral {
      NSLog(@"did connect peripheral");
      [self addPeripheral:peripheral];
      NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didConnectPeripheral_",
                                                        central,
                                                        peripheral,
                                                        nil];
      [self queueEvent:event];
  }
  </code></pre>