Class BaseBluetoothPairingManager
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
adapterIBluetoothAdapterThe Bluetooth adapter associated with this pairing manager.
loggerILoggerThe logger instance to use for logging.
Properties
Adapter
Gets the Bluetooth adapter associated with this pairing manager.
public IBluetoothAdapter Adapter { get; }
Property Value
IsPairing
Gets a value indicating whether a pairing operation is currently in progress.
public bool IsPairing { get; }
Property Value
IsUnpairing
Gets a value indicating whether an unpairing operation is currently in progress.
public bool IsUnpairing { get; }
Property Value
Methods
GetClosestPairedDeviceOrDefault(Func<IBluetoothRemoteDevice, bool>?)
Returns the closest paired Bluetooth device currently available.
public IBluetoothRemoteDevice? GetClosestPairedDeviceOrDefault(Func<IBluetoothRemoteDevice, bool>? filter = null)
Parameters
filterFunc<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
filterFunc<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
idstringThe 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
filterFunc<IBluetoothRemoteDevice, bool>An optional function to filter devices. Defaults to null for all devices.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA 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
idstringThe ID of the device to retrieve.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA 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
filterFunc<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
deviceIBluetoothRemoteDeviceThe 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
deviceIdstringThe 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
deviceIBluetoothRemoteDeviceThe device to pair with.
timeoutTimeSpan?Optional timeout for the operation.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
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
deviceIBluetoothRemoteDeviceThe device to unpair.
timeoutTimeSpan?Optional timeout for the operation.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
OnPairFailed(Exception)
Called when a pairing attempt fails. Completes the pairing task with an exception.
protected void OnPairFailed(Exception e)
Parameters
eExceptionThe 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
deviceIBluetoothRemoteDeviceThe device that was successfully paired.
OnPairingStateChanged(IBluetoothRemoteDevice, bool)
Raises the PairingStateChanged event.
protected virtual void OnPairingStateChanged(IBluetoothRemoteDevice device, bool isPaired)
Parameters
deviceIBluetoothRemoteDeviceThe device whose pairing state changed.
isPairedboolIndicates 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
eExceptionThe 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
deviceIBluetoothRemoteDeviceThe 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
deviceIBluetoothRemoteDeviceThe device to pair with.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA 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
deviceis 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
deviceIBluetoothRemoteDeviceThe device to unpair.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous unpairing operation.
Exceptions
- ArgumentNullException
Thrown when
deviceis 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
filterFunc<IBluetoothRemoteDevice, bool>An optional function to filter devices. Defaults to null for all devices.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA 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
idstringThe ID of the device to wait for.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA 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
PairedDeviceListChanged
Event raised when the list of paired devices changes.
public event EventHandler<DeviceListChangedEventArgs>? PairedDeviceListChanged
Event Type
PairedDevicesAdded
Event raised when devices are added to the paired devices list.
public event EventHandler<DevicesAddedEventArgs>? PairedDevicesAdded
Event Type
PairedDevicesRemoved
Event raised when devices are removed from the paired devices list.
public event EventHandler<DevicesRemovedEventArgs>? PairedDevicesRemoved
Event Type
Pairing
Event raised when a pairing operation starts.
public event EventHandler? Pairing
Event Type
PairingStateChanged
Event raised when the pairing state changes for a device.
public event EventHandler<PairingStateChangedEventArgs>? PairingStateChanged
Event Type
Unpaired
Event raised when an unpairing operation completes successfully.
public event EventHandler? Unpaired
Event Type
Unpairing
Event raised when an unpairing operation starts.
public event EventHandler? Unpairing