NordicSemiconductor / IOS-CoreBluetooth-Mock

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

Service discovery not firing #97

Closed brokkc1 closed 1 year ago

brokkc1 commented 1 year ago

I am having an issue with service discovery, using version 0.16.1 via Swift package manager.

After connecting to our physical device, and issuing peripheral.discoverServices([]) our delegate is not receiving a didDiscoverCharacteristicsFor callback.

Stepping through the code I have found when we set our delegate it seems that inside CBMCentralManagerNative.swift our delegate is thrown away and a wrapper delegate (that has no reference to ours) is used in place.

https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock/blob/bc042dc9cb2833e3573f8b6d66a54c7a96690104/CoreBluetoothMock/CBMCentralManagerNative.swift#L464-L476

I can see didDiscoverServices is being called on the CBPeripheralDelegateWrapper, but not being passed down to our delegate. https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock/blob/bc042dc9cb2833e3573f8b6d66a54c7a96690104/CoreBluetoothMock/CBMCentralManagerNative.swift#L261-L265

I set the delegate via the CBPeripheral we receive during didDiscover phase. Everything works perfectly when mocking, seems like an issue with the native flow only.

Other then that fantastic library, I wrote the code purely against this library and once we received the physical device everything else seems to work perfectly. Well done.

philips77 commented 1 year ago

Hello, thanks for the feedback.

our delegate is thrown away

It is not thrown away, it's stored as delegate. Jus the didSet implementation doesn't touch it, just checks for presence. This delegate is then called in the code you provided: impl.delegate?....

Try changing:

peripheral.discoverServices([])

to

peripheral.discoverServices(nil)
brokkc1 commented 1 year ago

Hi Phillip, Thanks for the quick response,

I've tried changing peripheral.discoverServices([]) to peripheral.discoverServices(nil) still no luck sadly.

But I do now understand by our delegate is set to impl.delegate, I can see that now thank you. I'll keep investigating it must be a config issue.

brokkc1 commented 1 year ago

The issue was caused by our delegate being cleaned, keeping an internal reference stopped this from happening.