Repository navigation
Expose a device's bond state and raise an event when it changes - #39
Merged
Merged
Conversation
Device.BondState reports the bond (pairing) state as the platform sees it: read from the system on Android, from the pairing information on Windows, and Unknown on Apple platforms, where CoreBluetooth does not expose bonding. DeviceBondState gains Unknown, declared last so the existing values do not move. It is also reported where the state cannot be read: Android 12 and later without BLUETOOTH_CONNECT, the platform-neutral targets, stand-in devices and disposed devices. Reading the property never throws. The Android adapter now registers BondStatusBroadcastReceiver for its lifetime and raises Adapter.DeviceBondStateChanged, resolving the device to the instance it already tracks so PropertyChanged fires for BondState on that instance too. The receiver previously could not have worked: it built a Device with a null adapter, which throws. It keeps its public surface, and handles its own failures because OnReceive runs on the main looper. Registration uses RECEIVER_NOT_EXPORTED from API 33 and is undone when the adapter is disposed. The README gains a Bonding section describing each platform, including how pairing works on Apple platforms. Fixes #34
This was referenced Oct 15, 2026
This was referenced Oct 15, 2026
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.
Fixes #34.
Merges first. #40 (issue #35) is stacked on this PR, and #41 (issue #36) on #40. Merge this one, then #40, then #41.
Public API (additive only)
Device.BondState(DeviceBondState). Never throws.Adapter.DeviceBondStateChanged(EventHandler<DeviceBondStateChangedEventArgs>), raised on Android.DeviceBondState.Unknown, declared last soNotBonded,BondingandBondedkeep their values (0, 1, 2).Per platform
BondStateBluetoothDevice.BondStateon every readACTION_BOND_STATE_CHANGEDbroadcastUnknownBonded/NotBondedfrom the device's pairing informationnet10.0,netstandard2.0UnknownUnknownis also reported where the state cannot be read: Android 12+ withoutBLUETOOTH_CONNECT(theSecurityExceptionis traced and swallowed), a stand-in device built through the protected constructor, and a disposed device.Android receiver
BondStatusBroadcastReceiverin its constructor and unregisters it inDispose. From API 33 it registers withRECEIVER_NOT_EXPORTED, which still receives the protected system broadcast. A registration failure is traced, not thrown: bond events are an addition to the adapter, not a condition of it working.PropertyChangedforBondStatefires on that instance too. An unknown device gets a newDevice.Devicewith a null adapter, which throwsArgumentNullExceptionsince 4.0. Its public surface (type, parameterless constructor,BondStateChangedevent) is unchanged; the parameterless constructor resolves devices throughBluetoothLE.Current.Adapter.OnReceivenow catches its own failures, because it runs on the main looper.README
New "Bonding" section with the table above, the Apple behaviour (the OS pairs on demand when a protected characteristic is first accessed; the app cannot query or request a bond; a stale bond is removed by the user in Settings), and the Android permission note.
Verification
Unknown, still does afterDispose, and that the existing enum values are unchanged.Notes for review
4.0.yyMM.n. Consider whether this release should move to 4.1.PackageReleaseNotesgains an "Unreleased" paragraph to be given its version at release.