13

I'm trying to connect to a bluetooth BTLE device. I have no problem discovering the peripheral.

However, when I attempt to connect to the peripheral, I received the following warning.

2013-04-05 22:10:36.110 CoreBluetooth[WARNING] 7DA9E322-D710-081B-4A9D-526DE546B13C, Name = "Find My Car Smarter", IsConnected = NO> is being dealloc'ed while connecting

Furthermore, neither of the relevant delegate methods are called:

didConnectPeripheral:
didFailToConnectPeripheral:

I've been struggling with this for hours... Please help.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
mikeholp
  • 965
  • 3
  • 11
  • 14

1 Answers1

38

Short answer: You need to retain the peripheral.

Long explanation: Core Bluetooth does not know whether you are interested in this peripheral when it is discovered. Connecting to it is not enough, you need to retain it.

Add a property to the class where you are doing all that:

@property (strong) CBPeripheral     *connectingPeripheral;

And then assign the peripheral to this property when the device is discovered, before you return from didDiscoverPeripheral:

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
  DDLogVerbose(@"Discovered peripheral: %@ advertisement %@ RSSI: %@", [peripheral description], [advertisementData description], [RSSI description]);

  [central connectPeripheral:peripheral options:nil];
  self.connectingPeripheral = peripheral;
}
sarfata
  • 4,625
  • 4
  • 28
  • 36
  • That did the trick! This has been driving me crazy... Much thanks! – mikeholp Apr 10 '13 at 00:27
  • 4
    Please approve the answer (the green tick next to it). It helps people know that this answer works and me to get some more points and badges ;) – sarfata Apr 10 '13 at 12:18
  • I'm having the same issue. I'm keeping the reference in an array but when I try to connect I get this same error. @property NSMutableArray *heartRateMonitors; [self setHeartRateMonitors:[[NSMutableArray alloc] init]]; [[self heartRateMonitors] addObject:peripheral]; If I store it in an individual variable like your example the problem goes away. Any idea what I've done wrong? – Sir Eisenhower Jun 12 '13 at 15:56
  • The array should retain the objects it contains but maybe you are releasing the array too early? You should add a debug message in the dealloc method of your object to make sure the object itself is not released (which would release the heartRateMonitors property). – sarfata Jun 12 '13 at 20:17
  • Thank you for the answer. I've been stuck on it for a while – Ashish Agarwal Nov 01 '13 at 21:03