feat(darwin): notify central of background connection events#264
feat(darwin): notify central of background connection events#264jonasbark wants to merge 1 commit into
Conversation
Set CBConnectPeripheralOptionNotifyOnConnection/Disconnection/Notification on connect() so CoreBluetooth surfaces connection, disconnection and notification events that occur while the app is suspended (relaunching it into the background to handle them). This keeps a backgrounded central — e.g. a fitness app reconnecting to a previously paired trainer — responsive without the user having to foreground the app. Consolidated to a single manager.connect(options:) call; the iOS 17+ auto-reconnect option is merged into the same dictionary, so behavior on that path is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request modifies the Swift connection logic in UniversalBlePlugin to unconditionally enable background connection options (CBConnectPeripheralOptionNotifyOnConnectionKey, CBConnectPeripheralOptionNotifyOnDisconnectionKey, and CBConnectPeripheralOptionNotifyOnNotificationKey) when connecting to a peripheral. Feedback indicates that enabling these options by default can trigger intrusive system alerts when the app is suspended, and recommends making them configurable or opt-in instead.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| var options: [String: Any] = [ | ||
| CBConnectPeripheralOptionNotifyOnConnectionKey: true, | ||
| CBConnectPeripheralOptionNotifyOnDisconnectionKey: true, | ||
| CBConnectPeripheralOptionNotifyOnNotificationKey: true, | ||
| ] |
There was a problem hiding this comment.
Enabling CBConnectPeripheralOptionNotifyOnConnectionKey, CBConnectPeripheralOptionNotifyOnDisconnectionKey, and CBConnectPeripheralOptionNotifyOnNotificationKey unconditionally will cause iOS to display system alerts (popups/notifications) to the user whenever these events occur while the app is suspended. This is an intrusive behavior change for existing users of the library who do not expect or want these alerts.\n\nThese options should be opt-in and configurable (e.g., via platform-specific connection options passed from the Dart layer) rather than enabled by default.
var options: [String: Any] = [:]
Summary
Pass the three "notify on background event" options to
manager.connect(_:options:)on Apple platforms so that CoreBluetooth surfaces connection / disconnection / characteristic-notification events that occur while the app is suspended, relaunching the app into the background to handle them:CBConnectPeripheralOptionNotifyOnConnectionKeyCBConnectPeripheralOptionNotifyOnDisconnectionKeyCBConnectPeripheralOptionNotifyOnNotificationKeyThe change consolidates the three previous
manager.connect(...)call sites into a singleconnect(peripheral, options:); the existing iOS 17+CBConnectPeripheralOptionEnableAutoReconnectis merged into the same dictionary, so that path is behaviorally unchanged.Motivation
I maintain a fitness app (BikeControl) that stays connected to a smart trainer/sensors for the duration of a workout. Users routinely background the app (lock the phone, switch to music, etc.). Combined with the
bluetooth-centralbackground mode and the CoreBluetooth state restoration that already landed in 2.0/2.1, these options let the central keep reacting to peripheral events — most importantly reconnecting to a previously paired peripheral — while suspended, instead of going silent until the user foregrounds the app.I've been carrying this as a one-line fork of
universal_blefor a while. Everything else in that fork (the Windows shared-mode /hresult_errorfixes andaddServicesInScanResponse) has since landed upstream — this is the only patch left, so I'd love to upstream it and retire the fork.Open question — should this be opt-in? 🟡
I've marked this a draft because these keys have a user-visible side effect: when the app is suspended, the system may show the user an alert for these events (and
NotifyOnNotificationKeyfires per characteristic notification). That's exactly what I want for a reconnect-while-backgrounded use case, but it's a behavior change for existing consumers, so enabling it unconditionally for everyone is probably not the right default.I'd appreciate guidance on the API shape you'd prefer:
PeripheralAndroidOptionsadvertising pattern) threaded through the pigeonconnectsignature, iOS/macOS-only. I'm happy to do the full cross-platform plumbing + pigeon regen if you point me at the shape you'd like.Happy to iterate either way.
Testing
main(Apple side, SPM layout).🤖 Generated with Claude Code