Basic Bluetooth: CBPeripheral turns off every ~ 10 seconds

I see a strange bug in iOS 8.3, and I wonder if anyone else sees the same thing.

I have an iPad Air (in central mode) and an iPhone 6 (in peripheral mode) in the immediate vicinity.

  • My CBCentralManager initialized with a sequential background queue and the CBCentralManagerOptionRestoreIdentifierKey option
  • The manager starts scanning peripheral devices using the CBCentralManagerScanOptionAllowDuplicatesKey: true option CBCentralManagerScanOptionAllowDuplicatesKey: true
  • Inside centralManager:didDiscoverPeripheral: I check the list of peripherals 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() } 
  • After connecting, I discover services and features.

Now the peripheral device is turned off after 10 seconds, immediately detected again and again connected. After 10 seconds, this procedure is repeated.

Is this a mistake or am I doing something wrong here?

I also tried to directly subscribe to the testimonial on the periphery, but that didn't change anything.

+6
source share
1 answer

This is the expected behavior. Bluetooth was designed to consume very little power, so it disconnects the connection as soon as possible. Also, at least in iOS 7 there was no need to reopen the peripheral device - after disconnecting, you were able to reconnect to the already detected device. I am sure that this is also true for iOS 8. The recommended way to work with BLE if you do not need a permanent connection:

  • Discover for yourself
  • Connect
  • Read / write everything you need as soon as possible.
  • Setting timer
  • When the timer fires, go to step 2

If you need a permanent connection (for example, you need to take heart rate data in real time), you should subscribe to the characteristic using - setNotifyValue: forCharacteristic:

When you turn on notifications for characteristic values, peripheral calls are peripheral: didUpdateNotificationStateForCharacteristic: error: its delegate object method notifies your application when characteristics change value. Because it’s the periphery that chooses when to send updates, your application should be ready to process them as long as notifications or directions remain on.

+7
source

Source: https://habr.com/ru/post/985409/


All Articles