NordicSemiconductor / IOS-CoreBluetooth-Mock

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

Peripheral connected through the system never connects if not advertising #92

Closed florianbuerger closed 1 year ago

florianbuerger commented 1 year ago

I am not too familiar with the internal workings, so maybe I am missing something: We upgraded to the recent version, but all our tests are breaking now because a mock peripheral that is already connected by the system never connects.

Looking through the diff, it seems the manager now only set mock.state = .connecting. The actual connection is only triggered in static CBMCentralManagerMock.peripheralBecameAvailable(_:) which is called when a peripheral is advertising. Previously, the connection was established during the CBMCentralManagerMock.connect(_:options:).

Our peripheral does connect to iOS in the background without any involvement from our app. The peripheral stops advertising when connected. Our mock looks like this

CBMPeripheralSpec
    .simulatePeripheral(proximity: .near)
    .advertising(
        advertisementData: [
            CBMAdvertisementDataServiceUUIDsKey: (...)),
            CBMAdvertisementDataLocalNameKey: "Mock",
        ],
        withInterval: 0.250,
        alsoWhenConnected: false
    )
    .connected(
        name: "Mock",
        services: [(...)],
        delegate: MockSpecDelegate()
    )
    .build()

If I revert the change in CBMCentralManagerMock:728

mock.state = .connecting
mock.connect() { result in
  switch result {
    case .success:
        self.delegate?.centralManager(self, didConnect: mock)
    case .failure(let error):
        self.delegate?.centralManager(self, didFailToConnect: mock, error: error)
}

everything works again.


Is this an oversight? Or am I missing something? Is there a new way to simulate this use case?

philips77 commented 1 year ago

Hello,

First of all, sorry for the delay. I missed your post.

You have a valid point. I will update the library. If the device is already connected, the connection should happen even if the device is no longer advertising. Expect 0.16.1 soon.

philips77 commented 1 year ago

The version is now tagged. I'm releasing 0.16.1 on CocoaPods now. Please, give it a spin. Your issue should now be fixed. Additionally, the fix also includes fast connection to already connected devices.

florianbuerger commented 1 year ago

Thank you 🙌