Class BaseBluetoothLocalCharacteristic
- Namespace
- Bluetooth.Core.Broadcasting
- Assembly
- Bluetooth.Core.Broadcasting.dll
Base class for Bluetooth broadcast characteristics.
public abstract class BaseBluetoothLocalCharacteristic : BaseBindableObject, IBluetoothLocalCharacteristic, INotifyPropertyChanged, IAsyncDisposable
- Inheritance
-
BaseBluetoothLocalCharacteristic
- Implements
- Derived
- Inherited Members
Constructors
BaseBluetoothLocalCharacteristic(IBluetoothLocalService, BluetoothLocalCharacteristicSpec, IBluetoothLocalDescriptorFactory, ILogger<IBluetoothLocalCharacteristic>?)
Initializes a new instance using a factory spec.
protected BaseBluetoothLocalCharacteristic(IBluetoothLocalService service, IBluetoothLocalCharacteristicFactory.BluetoothLocalCharacteristicSpec spec, IBluetoothLocalDescriptorFactory descriptorFactory, ILogger<IBluetoothLocalCharacteristic>? logger = null)
Parameters
serviceIBluetoothLocalServiceThe local service this characteristic belongs to.
specIBluetoothLocalCharacteristicFactory.BluetoothLocalCharacteristicSpecThe factory spec containing characteristic information.
descriptorFactoryIBluetoothLocalDescriptorFactoryThe factory for creating local descriptors.
loggerILogger<IBluetoothLocalCharacteristic>The logger instance to use for logging (optional).
BaseBluetoothLocalCharacteristic(IBluetoothLocalService, Guid, BluetoothCharacteristicProperties, BluetoothCharacteristicPermissions, ReadOnlyMemory<byte>?, string?, ILogger<IBluetoothLocalCharacteristic>?)
Initializes a new instance of the BaseBluetoothLocalCharacteristic class.
protected BaseBluetoothLocalCharacteristic(IBluetoothLocalService service, Guid id, BluetoothCharacteristicProperties properties, BluetoothCharacteristicPermissions permissions, ReadOnlyMemory<byte>? initialValue = null, string? name = null, ILogger<IBluetoothLocalCharacteristic>? logger = null)
Parameters
serviceIBluetoothLocalServiceidGuidpropertiesBluetoothCharacteristicPropertiespermissionsBluetoothCharacteristicPermissionsinitialValueReadOnlyMemory<byte>?namestringloggerILogger<IBluetoothLocalCharacteristic>
Properties
DescriptorFactory
Gets the factory for creating local Bluetooth descriptors.
protected IBluetoothLocalDescriptorFactory? DescriptorFactory { get; }
Property Value
Id
Gets the unique identifier of the characteristic.
public Guid Id { get; }
Property Value
Name
Gets the name of the characteristic.
public string Name { get; }
Property Value
Permissions
The permissions of the characteristic.
public BluetoothCharacteristicPermissions Permissions { get; init; }
Property Value
Properties
Gets the GATT properties supported by this characteristic (Read/Write/Notify/Indicate, etc.).
public BluetoothCharacteristicProperties Properties { get; init; }
Property Value
Service
Gets the Bluetooth service hosting this characteristic.
public IBluetoothLocalService Service { get; }
Property Value
SubscribedDevices
Gets the list of client devices that are currently subscribed to notifications/indications from this characteristic.
public IReadOnlyList<IBluetoothConnectedDevice> SubscribedDevices { get; }
Property Value
Value
Gets the value of the characteristic as a read-only memory segment. Useful for asynchronous operations.
public ReadOnlyMemory<byte> Value { get; }
Property Value
ValueSpan
Gets the value of the characteristic as a read-only span. Useful for high-performance scenarios.
public ReadOnlySpan<byte> ValueSpan { get; }
Property Value
Methods
CreateDescriptorAsync(Guid, string?, TimeSpan?, CancellationToken)
Adds a descriptor to this characteristic.
public ValueTask<IBluetoothLocalDescriptor> CreateDescriptorAsync(Guid id, string? name = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
idGuidThe ID of the descriptor to add.
namestringAn optional name for the descriptor. If not provided, a default name may be assigned based on the ID or other heuristics.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask<IBluetoothLocalDescriptor>
A task that represents the asynchronous operation. The task result contains the added descriptor.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
DisposeAsyncCore()
Disposes the resources asynchronously.
protected virtual ValueTask DisposeAsyncCore()
Returns
GetDescriptor(Func<IBluetoothLocalDescriptor, bool>)
Gets a descriptor that matches the specified filter.
public IBluetoothLocalDescriptor GetDescriptor(Func<IBluetoothLocalDescriptor, bool> filter)
Parameters
filterFunc<IBluetoothLocalDescriptor, bool>The filter to apply to the descriptors.
Returns
- IBluetoothLocalDescriptor
The descriptor that matches the filter.
GetDescriptor(Guid)
Gets a descriptor by its ID.
public IBluetoothLocalDescriptor GetDescriptor(Guid id)
Parameters
idGuidThe ID of the descriptor to get.
Returns
- IBluetoothLocalDescriptor
The descriptor with the specified ID.
GetDescriptorOrDefault(Func<IBluetoothLocalDescriptor, bool>)
Gets a descriptor that matches the specified filter.
public IBluetoothLocalDescriptor? GetDescriptorOrDefault(Func<IBluetoothLocalDescriptor, bool> filter)
Parameters
filterFunc<IBluetoothLocalDescriptor, bool>The filter to apply to the descriptors.
Returns
- IBluetoothLocalDescriptor
The descriptor that matches the filter, or null if not found.
GetDescriptorOrDefault(Guid)
Gets a descriptor by its ID.
public IBluetoothLocalDescriptor? GetDescriptorOrDefault(Guid id)
Parameters
idGuidThe ID of the descriptor to get.
Returns
- IBluetoothLocalDescriptor
The descriptor with the specified ID, or null if not found.
GetDescriptors(Func<IBluetoothLocalDescriptor, bool>?)
Gets all descriptors for this characteristic.
public IEnumerable<IBluetoothLocalDescriptor> GetDescriptors(Func<IBluetoothLocalDescriptor, bool>? filter = null)
Parameters
filterFunc<IBluetoothLocalDescriptor, bool>An optional filter to apply to the descriptors.
Returns
- IEnumerable<IBluetoothLocalDescriptor>
A collection of descriptors.
HasDescriptor(Func<IBluetoothLocalDescriptor, bool>)
Checks if a descriptor that matches the specified filter exists.
public bool HasDescriptor(Func<IBluetoothLocalDescriptor, bool> filter)
Parameters
filterFunc<IBluetoothLocalDescriptor, bool>The filter to apply to the descriptors.
Returns
- bool
True if a descriptor that matches the specified filter exists; otherwise, false.
HasDescriptor(Guid)
Checks if a descriptor with the specified ID exists.
public bool HasDescriptor(Guid id)
Parameters
idGuidThe ID of the descriptor to check for.
Returns
- bool
True if a descriptor with the specified ID exists; otherwise, false.
NativeCreateDescriptorAsync(Guid, string?, TimeSpan?, CancellationToken)
Creates a new local Bluetooth descriptor with the specified parameters.
protected abstract ValueTask<IBluetoothLocalDescriptor> NativeCreateDescriptorAsync(Guid id, string? name = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
idGuidnamestringtimeoutTimeSpan?cancellationTokenCancellationToken
Returns
NativeUpdateValueAsync(ReadOnlyMemory<byte>, bool, TimeSpan?, CancellationToken)
Updates the value of the characteristic in the native Bluetooth stack.
protected abstract ValueTask NativeUpdateValueAsync(ReadOnlyMemory<byte> value, bool notifyClients, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
valueReadOnlyMemory<byte>The new value to set for the characteristic.
notifyClientsboolIndicates whether to notify subscribed clients of the value change.
timeoutTimeSpan?An optional timeout for the operation.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- ValueTask
A task that represents the asynchronous update operation.
OnCharacteristicSubscribed(IBluetoothConnectedDevice)
Called when a client device subscribes to the characteristic.
protected void OnCharacteristicSubscribed(IBluetoothConnectedDevice device)
Parameters
deviceIBluetoothConnectedDeviceThe client device that subscribed.
OnCharacteristicUnsubscribed(IBluetoothConnectedDevice)
Called when a client device unsubscribes from the characteristic.
protected void OnCharacteristicUnsubscribed(IBluetoothConnectedDevice device)
Parameters
deviceIBluetoothConnectedDeviceThe client device that unsubscribed.
OnReadRequestReceived(IBluetoothConnectedDevice)
Called when a read request is received from a client device.
protected ReadOnlySpan<byte> OnReadRequestReceived(IBluetoothConnectedDevice device)
Parameters
deviceIBluetoothConnectedDeviceThe client device that sent the read request.
Returns
- ReadOnlySpan<byte>
The data to be sent in response to the read request.
OnWriteRequestReceivedAsync(IBluetoothConnectedDevice, byte[], TimeSpan?, CancellationToken)
Called when a write request is received from a client device.
protected ValueTask OnWriteRequestReceivedAsync(IBluetoothConnectedDevice device, byte[] data, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
deviceIBluetoothConnectedDeviceThe client device that sent the write request.
databyte[]The data sent in the write request.
timeoutTimeSpan?An optional timeout for the operation.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- ValueTask
A task that represents the asynchronous write operation.
RemoveAllDescriptorsAsync(TimeSpan?, CancellationToken)
Removes all descriptors from this characteristic.
public ValueTask RemoveAllDescriptorsAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
RemoveDescriptorAsync(IBluetoothLocalDescriptor, TimeSpan?, CancellationToken)
Removes a descriptor from this characteristic.
public ValueTask RemoveDescriptorAsync(IBluetoothLocalDescriptor localDescriptor, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
localDescriptorIBluetoothLocalDescriptorThe descriptor to remove.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
RemoveDescriptorAsync(Guid, TimeSpan?, CancellationToken)
Removes a descriptor from this characteristic.
public ValueTask RemoveDescriptorAsync(Guid id, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
idGuidThe ID of the descriptor to remove.
timeoutTimeSpan?The timeout for this operation.
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
UpdateValueAsync(ReadOnlyMemory<byte>, bool, TimeSpan?, CancellationToken)
Updates the value of a hosted characteristic and optionally notifies/indicates subscribed clients.
public ValueTask UpdateValueAsync(ReadOnlyMemory<byte> value, bool notifyClients = true, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
valueReadOnlyMemory<byte>The new value.
notifyClientsboolWhether to notify subscribed clients of the value change.
timeoutTimeSpan?The timeout for this operation
cancellationTokenCancellationTokenA cancellation token to cancel this operation.
Returns
- ValueTask
A task that represents the asynchronous operation.
Events
ClientSubscribed
Event raised when a client device subscribes to this characteristic.
public event EventHandler<CharacteristicSubscriptionChangedEventArgs>? ClientSubscribed
Event Type
ClientUnsubscribed
Event raised when a client device unsubscribes from this characteristic.
public event EventHandler<CharacteristicSubscriptionChangedEventArgs>? ClientUnsubscribed
Event Type
ReadRequested
Event raised when a client device requests to read this characteristic.
public event EventHandler<CharacteristicReadRequestEventArgs>? ReadRequested
Event Type
WriteRequested
Event raised when a client device requests to write to this characteristic.
public event EventHandler<CharacteristicWriteRequestEventArgs>? WriteRequested