Fix clear_peripherals on CoreBluetooth to clear internal map#458
Open
JosephBirks wants to merge 1 commit intodeviceplug:devfrom
Open
Fix clear_peripherals on CoreBluetooth to clear internal map#458JosephBirks wants to merge 1 commit intodeviceplug:devfrom
JosephBirks wants to merge 1 commit intodeviceplug:devfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
clear_peripherals()only clears theAdapterManager's publicDashMapbut notCoreBluetoothInternal's privateHashMap. After clearing, re-discovered peripherals are treated asDeviceUpdated(already known internally) instead ofDeviceDiscovered, so they are never re-added to the public map.This adds a
ClearPeripheralsmessage 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, causingDeviceUpdatedinstead ofDeviceDiscoveredon re-discovery.Details
Following the existing message-passing pattern used by
StartScanning,StopScanning, etc:ClearPeripheralsvariant toCoreBluetoothMessageself.peripheralsAdapter::clear_peripherals()after clearing theAdapterManagermapTesting
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 byperipherals().