Table of Contents

Class BaseBluetoothPairingManager

Namespace
Bluetooth.Core.Scanning
Assembly
Bluetooth.Core.Scanning.dll

Interface for managing Bluetooth pairing and bonding operations. Pairing establishes a secure connection between devices, while bonding stores the keys for future connections.

public abstract class BaseBluetoothPairingManager : BaseBindableObject, INotifyPropertyChanged, IBluetoothPairingManager
Inheritance
BaseBluetoothPairingManager
Implements
Inherited Members

Constructors

BaseBluetoothPairingManager(IBluetoothAdapter, ILogger?)

Initializes a new instance of the BaseBluetoothPairingManager class.

protected BaseBluetoothPairingManager(IBluetoothAdapter adapter, ILogger? logger = null)

Parameters

adapter IBluetoothAdapter

The Bluetooth adapter associated with this pairing manager.

logger ILogger

The logger instance to use for logging.

Properties

Adapter

Gets the Bluetooth adapter associated with this pairing manager.

public IBluetoothAdapter Adapter { get; }

Property Value

IBluetoothAdapter

IsPairing

Gets a value indicating whether a pairing operation is currently in progress.

public bool IsPairing { get; }

Property Value

bool

IsUnpairing

Gets a value indicating whether an unpairing operation is currently in progress.

public bool IsUnpairing { get; }

Property Value

bool

Methods

GetClosestPairedDeviceOrDefault(Func<IBluetoothRemoteDevice, bool>?)

Returns the closest paired Bluetooth device currently available.

public IBluetoothRemoteDevice? GetClosestPairedDeviceOrDefault(Func<IBluetoothRemoteDevice, bool>? filter = null)

Parameters

filter Func<IBluetoothRemoteDevice, bool>

An optional function to filter devices. Defaults to null for all devices.

Returns

IBluetoothRemoteDevice

The closest paired IBluetoothRemoteDevice, or null if none are found.

GetPairedDeviceOrDefault(Func<IBluetoothRemoteDevice, bool>?)

Returns the first paired Bluetooth device that matches the specified filter.

public IBluetoothRemoteDevice? GetPairedDeviceOrDefault(Func<IBluetoothRemoteDevice, bool>? filter = null)

Parameters

filter Func<IBluetoothRemoteDevice, bool>

A function to filter devices. Should return true for matching devices.

Returns

IBluetoothRemoteDevice

The matching paired IBluetoothRemoteDevice, or null if none are found.

GetPairedDeviceOrDefault(string)

Returns a paired Bluetooth device with the specified ID.

public IBluetoothRemoteDevice? GetPairedDeviceOrDefault(string id)

Parameters

id string

The ID of the device to retrieve.

Returns

IBluetoothRemoteDevice

The matching paired IBluetoothRemoteDevice, or null if none are found.

GetPairedDeviceOrWaitForDeviceToBePairedAsync(Func<IBluetoothRemoteDevice, bool>?, TimeSpan?, CancellationToken)

Returns a paired device immediately if it exists, otherwise waits for it to be paired.

public ValueTask<IBluetoothRemoteDevice> GetPairedDeviceOrWaitForDeviceToBePairedAsync(Func<IBluetoothRemoteDevice, bool>? filter = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

filter Func<IBluetoothRemoteDevice, bool>

An optional function to filter devices. Defaults to null for all devices.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask<IBluetoothRemoteDevice>

The matching paired IBluetoothRemoteDevice.

GetPairedDeviceOrWaitForDeviceToBePairedAsync(string, TimeSpan?, CancellationToken)

Returns a paired device by ID immediately if it exists, otherwise waits for it to be paired.

public ValueTask<IBluetoothRemoteDevice> GetPairedDeviceOrWaitForDeviceToBePairedAsync(string id, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

id string

The ID of the device to retrieve.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask<IBluetoothRemoteDevice>

The matching paired IBluetoothRemoteDevice.

GetPairedDevices(Func<IBluetoothRemoteDevice, bool>?)

Returns all paired Bluetooth devices that match the specified filter.

public IEnumerable<IBluetoothRemoteDevice> GetPairedDevices(Func<IBluetoothRemoteDevice, bool>? filter = null)

Parameters

filter Func<IBluetoothRemoteDevice, bool>

An optional function to filter devices. Defaults to null for all devices.

Returns

IEnumerable<IBluetoothRemoteDevice>

A collection of paired IBluetoothRemoteDevice that match the filter.

IsPaired(IBluetoothRemoteDevice)

Checks if the specified device is currently paired (bonded).

public virtual bool IsPaired(IBluetoothRemoteDevice device)

Parameters

device IBluetoothRemoteDevice

The device to check.

Returns

bool

True if the device is paired, false otherwise.

IsPaired(string)

Checks if the specified device is currently paired (bonded) by its identifier.

public virtual bool IsPaired(string deviceId)

Parameters

deviceId string

The identifier of the device to check.

Returns

bool

True if the device is paired, false otherwise.

NativePairAsync(IBluetoothRemoteDevice, TimeSpan?, CancellationToken)

Platform-specific implementation to initiate a pairing operation with the device.

protected abstract ValueTask NativePairAsync(IBluetoothRemoteDevice device, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The device to pair with.

timeout TimeSpan?

Optional timeout for the operation.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

NativeUnpairAsync(IBluetoothRemoteDevice, TimeSpan?, CancellationToken)

Platform-specific implementation to initiate an unpairing operation with the device.

protected abstract ValueTask NativeUnpairAsync(IBluetoothRemoteDevice device, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The device to unpair.

timeout TimeSpan?

Optional timeout for the operation.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

OnPairFailed(Exception)

Called when a pairing attempt fails. Completes the pairing task with an exception.

protected void OnPairFailed(Exception e)

Parameters

e Exception

The exception that occurred during the pairing attempt.

OnPairSucceeded(IBluetoothRemoteDevice)

Called when a pairing attempt succeeds. Completes the pairing task with success result.

protected void OnPairSucceeded(IBluetoothRemoteDevice device)

Parameters

device IBluetoothRemoteDevice

The device that was successfully paired.

OnPairingStateChanged(IBluetoothRemoteDevice, bool)

Raises the PairingStateChanged event.

protected virtual void OnPairingStateChanged(IBluetoothRemoteDevice device, bool isPaired)

Parameters

device IBluetoothRemoteDevice

The device whose pairing state changed.

isPaired bool

Indicates whether the device is now paired.

OnUnpairFailed(Exception)

Called when an unpairing attempt fails. Completes the unpairing task with an exception.

protected void OnUnpairFailed(Exception e)

Parameters

e Exception

The exception that occurred during the unpairing attempt.

OnUnpairSucceeded(IBluetoothRemoteDevice)

Called when an unpairing attempt succeeds. Completes the unpairing task.

protected void OnUnpairSucceeded(IBluetoothRemoteDevice device)

Parameters

device IBluetoothRemoteDevice

The device that was successfully unpaired.

PairAsync(IBluetoothRemoteDevice, TimeSpan?, CancellationToken)

Pairs with the specified device asynchronously.

public virtual ValueTask<bool> PairAsync(IBluetoothRemoteDevice device, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The device to pair with.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask<bool>

A task that represents the asynchronous pairing operation. The task result indicates whether pairing was successful.

Exceptions

ArgumentNullException

Thrown when device is null.

InvalidOperationException

Thrown when the device is already paired or pairing is not supported.

TimeoutException

Thrown when the operation times out.

OperationCanceledException

Thrown when the operation is cancelled.

UnpairAsync(IBluetoothRemoteDevice, TimeSpan?, CancellationToken)

Unpairs (removes bonding information) from the specified device asynchronously.

public virtual ValueTask UnpairAsync(IBluetoothRemoteDevice device, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The device to unpair.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask

A task that represents the asynchronous unpairing operation.

Exceptions

ArgumentNullException

Thrown when device is null.

InvalidOperationException

Thrown when the device is not paired or unpairing is not supported.

TimeoutException

Thrown when the operation times out.

OperationCanceledException

Thrown when the operation is cancelled.

WaitForDeviceToBePairedAsync(Func<IBluetoothRemoteDevice, bool>?, TimeSpan?, CancellationToken)

Waits for a device matching the filter to be paired.

public ValueTask<IBluetoothRemoteDevice> WaitForDeviceToBePairedAsync(Func<IBluetoothRemoteDevice, bool>? filter = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

filter Func<IBluetoothRemoteDevice, bool>

An optional function to filter devices. Defaults to null for all devices.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask<IBluetoothRemoteDevice>

The paired IBluetoothRemoteDevice.

WaitForDeviceToBePairedAsync(string, TimeSpan?, CancellationToken)

Waits for a device with the specified ID to be paired.

public ValueTask<IBluetoothRemoteDevice> WaitForDeviceToBePairedAsync(string id, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

id string

The ID of the device to wait for.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask<IBluetoothRemoteDevice>

The paired IBluetoothRemoteDevice.

Events

Paired

Event raised when a pairing operation completes successfully.

public event EventHandler? Paired

Event Type

EventHandler

PairedDeviceListChanged

Event raised when the list of paired devices changes.

public event EventHandler<DeviceListChangedEventArgs>? PairedDeviceListChanged

Event Type

EventHandler<DeviceListChangedEventArgs>

PairedDevicesAdded

Event raised when devices are added to the paired devices list.

public event EventHandler<DevicesAddedEventArgs>? PairedDevicesAdded

Event Type

EventHandler<DevicesAddedEventArgs>

PairedDevicesRemoved

Event raised when devices are removed from the paired devices list.

public event EventHandler<DevicesRemovedEventArgs>? PairedDevicesRemoved

Event Type

EventHandler<DevicesRemovedEventArgs>

Pairing

Event raised when a pairing operation starts.

public event EventHandler? Pairing

Event Type

EventHandler

PairingStateChanged

Event raised when the pairing state changes for a device.

public event EventHandler<PairingStateChangedEventArgs>? PairingStateChanged

Event Type

EventHandler<PairingStateChangedEventArgs>

Unpaired

Event raised when an unpairing operation completes successfully.

public event EventHandler? Unpaired

Event Type

EventHandler

Unpairing

Event raised when an unpairing operation starts.

public event EventHandler? Unpairing

Event Type

EventHandler