NordicSemiconductor / IOS-CoreBluetooth-Mock

Mocking library for CoreBluetooth framework.
BSD 3-Clause "New" or "Revised" License
225 stars 51 forks source link

Make sure to get the latest object from CB on retrievePeripherals #67

Closed jason-gabriele closed 2 years ago

jason-gabriele commented 2 years ago

Normally, you can keep a cache of CBPeripheral objects and use them for operations. There seems to be an edge case where this isn't true: bluetoothd crashes. When bluetoothd crashes, CoreBluetooth seems to recreate all the objects and only accept operations from the new ones. Attempting to use an old object will just silently fail and print out an API MISUSE message to the console. The way to get around this is by always retrieving a new CBPeripheral after the central goes into a resetting state and back into poweredOn.

I don't have a way of reliably crashing bluetoothd, so this case is pretty much impossible to test. The only way to really tell the objects apart is by checking the pointer address.

CLAassistant commented 2 years ago

CLA assistant check
All committers have signed the CLA.

philips77 commented 2 years ago

Thank you for the PR! Looks good.

junebeans commented 2 years ago

I've had an issue where the iOS Bluetooth stack crashed on iOS 14. Haven't been able to reliably reproduce the crash on iOS 15+, but for iOS 14 when I connected lots of devices (approximately 11+ devices), the Bluetooth stack was crashing.

What I've observed was that the identifier was also changing once the Bluetooth stack crashed. From there I had to re-scan the device and interact using the CBPeripheral retrieved from that scan as it was invalidated. From the description & change I see, it seems like the changes are assuming that the identifier will remain the same after the Bluetooth stack crash. Do you have any thoughts on this?

I'm not sure if this is the right channel to discuss, but sharing some though as they seem to be related. I really appreciate everyone's hard work. Thanks!

jason-gabriele commented 2 years ago

In my experience, the identifier did stay the same. It's really hard to test this since there's no reliable way of crashing bluetoothd.

Did you have a bond with the device?

junebeans commented 2 years ago

Yes, I had the devices bonded with iPhone.

jason-gabriele commented 2 years ago

Maybe you can just make sure to use retrieveConnectedPeripherals(withServices:) each time you get the powered on event? That way you won't need to worry about the CB identifier changing out beneath you.