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

New feature: Auto Reconnect (iOS 17+) #127

Merged
merged 10 commits into from
Mar 21, 2025
Merged

New feature: Auto Reconnect (iOS 17+) #127

merged 10 commits into from
Mar 21, 2025

Conversation

philips77
Copy link
Member

This PR adds support for Auto Reconnect feature, added in iOS 17 with CBConnectPeripheralOptionEnableAutoReconnect.

Initialization

Connection with Auto Reconnection enabled is initiated with this flag:

// Enable the Auto Reconnect feature when it's supported.
if #available(iOS 17.0, *) {
centralManager.connect(blinky.basePeripheral, options: [
CBConnectPeripheralOptionEnableAutoReconnect : true,
])
} else {
centralManager.connect(blinky.basePeripheral)
}

It's supported on iOS 17 and newer.

Besides this flag, a new delegate method was added to iOS API:

optional func centralManager(
    _ central: CBCentralManager,
    didDisconnectPeripheral peripheral: CBPeripheral,
    timestamp: CFAbsoluteTime,
    isReconnecting: Bool,
    error: (any Error)?
)

This callback is ONLY CALLED when there the centralManager(_:didDisconnectPeripheral:error) is not implemented, and only on iOS 17+ (despite what the documentation says).

CoreBluetooth Mock

This PR adds the flag and the above callback to the library. For mock devices, the automatic connection will happen on the first received connectable packet from a mock peripheral.

However, it was not possible to reproduce this (unintended? undocumented? diffcult to use?) behavior of NOT calling this delegate method when the previous centralManager(_:didDisconnectPeripheral:error) was implemented (the library has to implement it, but can't check if the user has implemented it using Swift).

Instead, this delegate method will be called from the library and the default implementation will call the old one. So, if the app implements only the old one, only the old one will be called, like before. If the new and old one is implemented, the old one will not be implemented. This is DIFFERENT from the native behavior, which calls only the new one in such case.
We recommend only using the new one, which in this library will be called also for iOS pre-17.

@philips77 philips77 merged commit 5991a21 into main Mar 21, 2025
1 check passed
@philips77 philips77 deleted the auto-reconnect branch March 21, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant