Skip to content

Ensure idempotent wrapper identity for CBPeripheral across retrieve / connect paths #9

@Linqa2

Description

@Linqa2

Description

In real-world multi-device scenarios (automatic reconnects, background operation, periodic retrieval of known devices), the current implementation can end up creating multiple logical wrappers for the same CBPeripheral.identifier. When this happens, delegate ownership may be overwritten, leading to “zombie” sessions where: writes may still succeed, but notifications / delegate callbacks are no longer delivered to the original wrapper.

This issue was surfaced by a real production-like use case involving multiple devices connecting and disconnecting without direct user interaction.

Current behavior

  • APIs such as retrieveConnectedPeripherals (and related connect paths) may create a new wrapper for a CBPeripheral even if an existing wrapper for the same identifier is already active.
  • Creating a new wrapper can overwrite cbPeripheral.delegate, breaking an existing logical session.
  • Repeated retrieval calls are therefore not idempotent and may disrupt active connections.

Desired behavior

  • Within a single BluetoothCentral instance, there must be at most one live logical wrapper per CBPeripheral.identifier at any given time.
  • Retrieval-style APIs should be idempotent.
  • Repeated calls must not create duplicate wrappers for the same peripheral.
  • Active sessions must not be disrupted by retrieval or reconnect paths.
  • Wrapper identity and delegate ownership should be guaranteed consistently across: scan-based discovery, retrieve APIs, explicit connect flows.

The goal is to address this at the central/identity level, rather than patching individual call sites.

A related PR helped surface this problem in a concrete scenario and is linked here for context: #8

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions