Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnection on disconnection when forceMock == false does not work due #113

Open
nonameplum opened this issue Jan 14, 2025 · 3 comments
Open
Labels
bug Something isn't working

Comments

@nonameplum
Copy link

Hey,

The problem is that when the delegate call central manager (_:diddisconnectperipheral:error:) is handled, we would like to use it to automatically "reconnect" by calling centralManager.connect(peripheral) since attempts to connect to a peripheral don’t time out.

But it does not work, as after digging into the issue, the underlying CBPeripherals are stored by the CBMCentralManagerNative which is removed on didDisconnectPeripheral so when connect is called it is blocked the if statement as the peripheral is no longer hold in the dictionary.

Potentially a quick fix could be:

func centralManager(_ central: CBCentralManager,
                    didDisconnectPeripheral peripheral: CBPeripheral,
                    error: Error?) {
    manager.delegate?.centralManager(manager,
                                     didDisconnectPeripheral: getPeripheral(peripheral),
                                     error: error)
    // Check if the error is present otherwise the disconnection was explicit
    if error == nil {
        removePeripheral(peripheral)
    }
}

But I guess, the biggest issue is that CBPeripherals are strongly hold by CBMCentralManagerNative in overall.

@philips77 philips77 added the bug Something isn't working label Jan 15, 2025
@philips77
Copy link
Member

Hi,
Thank you for creating the issue. Great catch.
How about first getting the reference to the peripheral, removing it, and calling the delegate:

func centralManager(_ central: CBCentralManager,
                    didDisconnectPeripheral peripheral: CBPeripheral,
                    error: Error?) {
    let p = getPeripheral(peripheral)
    removePeripheral(peripheral)
    manager.delegate?.centralManager(manager,
                                     didDisconnectPeripheral: p,
                                     error: error)
}

Unfortunately, I don't have time to look into it now, but I hope I can get back soon.

@nonameplum
Copy link
Author

This way it won't help unfortunately, because connect is called on the central manager later and the same if statement will not allow proceeding as peripheral will be removed from the dictionary at this point.

@philips77
Copy link
Member

Right. It should not remove the peripheral, or the connect. I don't remember why it does so. I need to spend some time recalling the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants