Search code examples
iosios5ios6core-bluetoothbluetooth-lowenergy

CoreBluetooth - Can connectPeripheral be called multiple times


Can an application attempt to connect to multiple devices at once?

When an app calls 'connectPeripheral', it will attempt to connect for an unspecified amount of time since there is no time-out. However, if 'connectPeripheral' is called a second time, does it cancel out the previous call, or will the app attempt to make two separate connections at once?


Solution

  • In my testing I've seen if you call it multiple times prior to being connected, it will take longer to actually connect (and often actually prevents a connection from occurring), but usually functions as normal after that.

    However, the one massive thing I've noticed is if you try calling connectPeripheral: inside of the didConnectPeripheral: callback, you will put yourself in an infinite loop that keeps returning on didConnectPeripheral: over and over again.

    Long story short, every single connectPeripheral: call should be matched with a cancelPeripheralConnection: (or didDisconnectPeripheral:) call if you don't want undefined behavior like this to happen.