iOS6: CBPeripheral is being dealloc'ed while connecting

纵饮孤独 提交于 2019-11-30 11:41:21

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!