Skip to content

Fix clear_peripherals on CoreBluetooth to clear internal map#458

Open
JosephBirks wants to merge 1 commit into
deviceplug:devfrom
JosephBirks:fix/clear-peripherals-corebluetooth
Open

Fix clear_peripherals on CoreBluetooth to clear internal map#458
JosephBirks wants to merge 1 commit into
deviceplug:devfrom
JosephBirks:fix/clear-peripherals-corebluetooth

Conversation

@JosephBirks

@JosephBirks JosephBirks commented Mar 29, 2026

Copy link
Copy Markdown

Summary

clear_peripherals() only clears the AdapterManager's public DashMap but not CoreBluetoothInternal's private HashMap. After clearing, re-discovered peripherals are treated as DeviceUpdated (already known internally) instead of DeviceDiscovered, so they are never re-added to the public map.

This adds a ClearPeripherals message to the CoreBluetooth thread so both maps are cleared in sync.

Context

I discovered this while adding clear_peripherals() to the scan loop in tauri-plugin-blec to prevent stale/duplicate devices accumulating across scan restarts. On macOS, devices disappeared after clearing and never came back. With a bit of help from Claude, I eventually worked out it's because the internal map is not being cleared, causing DeviceUpdated instead of DeviceDiscovered on re-discovery.

Details

Following the existing message-passing pattern used by StartScanning, StopScanning, etc:

  1. Add ClearPeripherals variant to CoreBluetoothMessage
  2. Handle it in the message loop by clearing self.peripherals
  3. Send the message from Adapter::clear_peripherals() after clearing the AdapterManager map

Testing

Tested in a Tauri BLE application on macOS. Called clear_peripherals() between scan restarts. Before this fix, devices vanished permanently after clearing. After this fix, devices are correctly re-discovered and returned by peripherals().

clear_peripherals() only cleared the AdapterManager's public DashMap
but not CoreBluetoothInternal's private HashMap. After clearing,
re-discovered peripherals were treated as DeviceUpdated (already known
internally) instead of DeviceDiscovered, so they were never re-added
to the public map.

Send a ClearPeripherals message to the CoreBluetooth thread so both
maps are cleared in sync. Windows and Android are unaffected as they
only have the single AdapterManager map.
@JosephBirks JosephBirks marked this pull request as ready for review March 29, 2026 10:55
@qwandor qwandor changed the base branch from master to dev March 29, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant