I am seeing a weird bug in iOS 8.3 and am wondering if anyone else is seeing the same thing.
I have an iPad Air (in Central mode) and an iPhone 6 (in Peripheral mode) in very close proximity.
CBCentralManager
is initialized with a serial background queue and the CBCentralManagerOptionRestoreIdentifierKey
optionCBCentralManagerScanOptionAllowDuplicatesKey: true
optioncentralManager:didDiscoverPeripheral:
I check against a list of peripherals that are already discovered:let connect: () -> () = { peripheral.delegate = self self.devices[peripheral.identifier.UUIDString] = peripheral self.manager.connectPeripheral(peripheral, options: nil) } if let device = devices[peripheral.identifier.UUIDString] { if device.peripheral.state == .Disconnected { connect() } } else if peripheral.state == .Disconnected { connect() }
The peripheral now disconnects after about 10 seconds, is immediately discovered again and connects again. 10 seconds later this procedure repeats itself.
Is this a bug or am I doing something wrong here?
I have also tried to directly subscribe to a characteristic on the peripheral, but this did not seem to change anything.
It is an expected behaviour. Bluetooth was designed to consume very little amount of energy so it drops the connection as soon as possible. Also at least in iOS 7 it was not necessary to rediscover peripheral device - you was able to reconnect to already discovered device after disconnection. I am pretty sure it is also true for iOS 8. The recommended way to work with BLE if you do not need a permanent connection is:
If you need a permanent connection (for example you need to take data about heart rate in real time) you should subscribe for characteristic with - setNotifyValue:forCharacteristic:
When you enable notifications for characteristic’s value, the peripheral calls the peripheral:didUpdateNotificationStateForCharacteristic:error: method of its delegate object to notify your app when the characteristic’s value changes. Because it is the peripheral that chooses when to send an update, your app should be prepared to handle them as long as notifications or indications remain enabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With