iOS6: CBPeripheral is being dealloc'ed while connecting

后端 未结 1 1118
遥遥无期
遥遥无期 2021-01-02 00:16

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

相关标签:
1条回答
  • 2021-01-02 00:43

    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;
    }
    
    0 讨论(0)
提交回复
热议问题