Skip to content

Request and remove a bond on Android - #40

Closed
newky2k wants to merge 1 commit into
feature/bond-statefrom
feature/bond-request
Closed

newky2k wants to merge 1 commit into
feature/bond-statefrom
feature/bond-request

Conversation

@newky2k

@newky2k newky2k commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Fixes #35.

Stacked on #39 (feature/bond-state, issue #34). This PR merges after #39; GitHub retargets it to development once #39 is merged. The diff here shows only the #35 change.

Public API (additive only)

  • Task<bool> Device.CreateBondAsync(CancellationToken cancellationToken = default)
  • Task<bool> Device.RemoveBondAsync(CancellationToken cancellationToken = default)

Android

  • CreateBondAsync calls BluetoothDevice.createBond(), which shows the system pairing prompt where the peripheral asks for one. It completes true when the adapter reports Bonded, and false when it reports NotBonded (bonding failed or was refused) or when createBond refuses to start. An already-bonded device returns true immediately. A device already Bonding is waited on instead of being asked again.
  • RemoveBondAsync calls the hidden BluetoothDevice.removeBond() by reflection and completes true when the device reaches NotBonded. Best effort, and documented as such: newer Android versions restrict it, and every refusal (method missing, non-SDK interface blocked, permission denied) is returned as false, never thrown. A device that is not bonded returns true.
  • Both observe the outcome through Adapter.DeviceBondStateChanged from Expose a device's bond state and raise an event when it changes #39, subscribing before starting. Only the success state is re-checked after starting, because Android can still report NotBonded for a moment straight after createBond.
  • The cancellation token stops the wait (OperationCanceledException). It does not abort bonding the system has already started. Disposing the device cancels the wait, through the existing combined token.
  • On Android 12+ without BLUETOOTH_CONNECT, CreateBondAsync throws the platform's SecurityException, which is documented.

Other platforms

  • Apple platforms and Windows: both return false, documented. CoreBluetooth offers no bond API. Windows has pairing APIs, but supporting them was outside this issue.
  • Stand-in devices: false.
  • net10.0 / netstandard2.0: PlatformNotSupportedException, like every other native member there.

README / release notes

The README Bonding section gains a paragraph and an example on requesting and removing a bond, with the best-effort caveat. PackageReleaseNotes extends the "Unreleased" paragraph from #39.

Verification

  • Builds with 0 errors for netstandard2.0, net10.0, net10.0-android, net10.0-ios, net10.0-maccatalyst, net10.0-windows10.0.19041.0 and net481. No new warnings. macos and tvos were not built locally (workloads not installed).
  • Not tested on hardware. Neither the pairing prompt nor bond removal has been run on an Android device, so the acceptance criteria need a manual check.
  • There is no test project in the repository, so no automated tests were added.

Device.CreateBondAsync calls BluetoothDevice.createBond and completes when
the adapter reports the device Bonded (true) or back to NotBonded (false).
A device that is already bonded returns true at once, and one that is
already bonding is waited on rather than asked again. The wait subscribes
before starting so a fast outcome is not missed, and only the success
state is re-checked afterwards, because straight after createBond Android
can still report NotBonded.

Device.RemoveBondAsync calls the hidden BluetoothDevice.removeBond by
reflection and waits for NotBonded. It is best effort: any refusal,
including the method being blocked as a non-SDK interface, is returned
as false rather than thrown, and the documentation says so.

Both return false on Apple platforms, Windows and stand-in devices, and
throw PlatformNotSupportedException on the platform-neutral targets as
other native members do. Cancellation stops the wait but not bonding the
system has already started.

Fixes #35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant