connectPeripheral on CBCentralManager not working in iOS10 in background

I try to connect to a peripheral that is found by my CBCentralMangager, but the didConnect never called in ios10



func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        let discoveredPeripheral = peripheral
        print("found new CBPeripheral") //prints found new CBPeripheral both for ios9 and ios 10
        centralManager.connectPeripheral(discoveredPeripheral, options: nil)
}




func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("did connect") //not printing in ios 10 running on my iphone 5, prints on iPad running ios 9
        peripheral.delegate = self
        peripheral.discoverServices([AppCBUUID]) 
    }


I use the exact the same code for testing the central side on iPad(running ios9) and iPhone5(running ios10), but gots different results. I wonder whether this is bug in core bluetooth in ios10

Are you seeing this on an app that has not been recompiled for ios10/Xcode8?


I'm running into similar hard-to-trace Bluetooth issues on our app. Did you try power cycling the bluetooth device and resetting Bluetooth by turning it off in Settings, restarting the phone, then turning it on again?

Same for me, compiled for iOS 9.3 under xcode 8

You don't appear to be retaining the peripheral object.


It is being deallocated before the delegate is called.

I can not offer a solution, but I see something similar:

I have two iPhones side by side running same version of my app. One iPhone is updated to iOS 10 recompiled using Xcode 8, the other iPhone is iOS 9.2 compiled using Xcode 7. I have a Bluetooth LE device transmitting non-connectable broadcast advertisements.


The iOS 10 iPhone fails to detect ~30% of the adverts (failure to invoke

didDiscoverPeripheral
).

For several versions of iOS, have had no problem. Has iOS 10 Core Bluetooth broken something?

Hi

Any solutions for this? I am facing the same issue

The solution is to maintain a reference to the peripheral, before calling connectPeripheral.

Hello kesong


Try replacing your connection code explicitly in a main thread, like this


DispatchQueue.main.async {

//connection code

}

you can declare a CBPeripheral variable as a class member variable instead of the local variable,and then assign its value in func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber){....}

for example:


class BLECentralManager:NSObject,CBCentralManagerDelegate {

var discoveredPeripheral:CBPeripheral!

...

..

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

discoveredPeripheral = peripheral

print("found new CBPeripheral") //prints found new CBPeripheral both for ios9 and ios 10

centralManager.connectPeripheral(discoveredPeripheral, options: nil)

}


I have tried,it works!

There is a bug in IOS10 that causes the screen to fail to update. I found it debugging a IOS10 UITableview problem in one of my apps. The app works fine in IOS8 and IOS9. The screen data did not change but under debug I could see that the data was written to the UITableview. Apple seems to have cached to UIView and lost track of when it needs update the screen. The screen updates when I manual scroll it. Updating the storyboard did not fix it.

I have 2 apps which uses BLE on same device. I have a BLE module to which I can connect without any issues. Covid Safe app is my 2nd app in device (loaded from App Store so I am not the owner of the code). I'm facing the same issue reported here when Covid app is running in background. I mean once my code
Code Block
centralManager.connectPeripheral(discoveredPeripheral, options: nil)
is executed, it does not call its delegate methods
Code Block
didConnectPeripheral
or
Code Block
didFailToConnect
.

But when the Covid app is killed or denied BLE permissions then my app is working fine.

Mostly this is happening in iOS 13.x versions + iPhone X and above models


Thanks Nikesh.

Also finding our CovidSafe app (from Aus Govt ver1.5) running in background *eventually* prevents our app from receiving didConnect callback after discovery.

Initially been telling users to reset iOS device, until reaslising can simply shutdown (and restart) CovidSafe app to resolve! Restarting CovidSafe app doesn't immediately repeat issue except after some days - even after repeatedly bringing CovidSafe app back into foreground. So unable to reproduce problem through manual steps.

Running iOS13.5.1 on iPhoneXs.
I am also facing reconnection issue.iPhone and band are not reconnected after coming back in BLE range.

Issue reproduction route:
A band is connected with an iOS app.
Keep iPhone and band out of BLE range for some time.
Take iPhone and band in BLE range after some time. [around 30 minutes]
Reconnection is not established between iPhone and band.

iOS app log analysis: 
App sends connection request when it gets didDisconnect callback from core Bluetooth F/W due to out of range disconnection. But didConnect is not called even though both iPhone and band are kept in range.
connectPeripheral on CBCentralManager not working in iOS10 in background
 
 
Q