Table of Contents

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

service IBluetoothLocalService

The local service this characteristic belongs to.

spec IBluetoothLocalCharacteristicFactory.BluetoothLocalCharacteristicSpec

The factory spec containing characteristic information.

descriptorFactory IBluetoothLocalDescriptorFactory

The factory for creating local descriptors.

logger ILogger<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

service IBluetoothLocalService
id Guid
properties BluetoothCharacteristicProperties
permissions BluetoothCharacteristicPermissions
initialValue ReadOnlyMemory<byte>?
name string
logger ILogger<IBluetoothLocalCharacteristic>

Properties

DescriptorFactory

Gets the factory for creating local Bluetooth descriptors.

protected IBluetoothLocalDescriptorFactory? DescriptorFactory { get; }

Property Value

IBluetoothLocalDescriptorFactory

Id

Gets the unique identifier of the characteristic.

public Guid Id { get; }

Property Value

Guid

Name

Gets the name of the characteristic.

public string Name { get; }

Property Value

string

Permissions

The permissions of the characteristic.

public BluetoothCharacteristicPermissions Permissions { get; init; }

Property Value

BluetoothCharacteristicPermissions

Properties

Gets the GATT properties supported by this characteristic (Read/Write/Notify/Indicate, etc.).

public BluetoothCharacteristicProperties Properties { get; init; }

Property Value

BluetoothCharacteristicProperties

Service

Gets the Bluetooth service hosting this characteristic.

public IBluetoothLocalService Service { get; }

Property Value

IBluetoothLocalService

SubscribedDevices

Gets the list of client devices that are currently subscribed to notifications/indications from this characteristic.

public IReadOnlyList<IBluetoothConnectedDevice> SubscribedDevices { get; }

Property Value

IReadOnlyList<IBluetoothConnectedDevice>

Value

Gets the value of the characteristic as a read-only memory segment. Useful for asynchronous operations.

public ReadOnlyMemory<byte> Value { get; }

Property Value

ReadOnlyMemory<byte>

ValueSpan

Gets the value of the characteristic as a read-only span. Useful for high-performance scenarios.

public ReadOnlySpan<byte> ValueSpan { get; }

Property Value

ReadOnlySpan<byte>

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

id Guid

The ID of the descriptor to add.

name string

An optional name for the descriptor. If not provided, a default name may be assigned based on the ID or other heuristics.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A 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

ValueTask

GetDescriptor(Func<IBluetoothLocalDescriptor, bool>)

Gets a descriptor that matches the specified filter.

public IBluetoothLocalDescriptor GetDescriptor(Func<IBluetoothLocalDescriptor, bool> filter)

Parameters

filter Func<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

id Guid

The 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

filter Func<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

id Guid

The 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

filter Func<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

filter Func<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

id Guid

The 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

id Guid
name string
timeout TimeSpan?
cancellationToken CancellationToken

Returns

ValueTask<IBluetoothLocalDescriptor>

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

value ReadOnlyMemory<byte>

The new value to set for the characteristic.

notifyClients bool

Indicates whether to notify subscribed clients of the value change.

timeout TimeSpan?

An optional timeout for the operation.

cancellationToken CancellationToken

A 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

device IBluetoothConnectedDevice

The client device that subscribed.

OnCharacteristicUnsubscribed(IBluetoothConnectedDevice)

Called when a client device unsubscribes from the characteristic.

protected void OnCharacteristicUnsubscribed(IBluetoothConnectedDevice device)

Parameters

device IBluetoothConnectedDevice

The client device that unsubscribed.

OnReadRequestReceived(IBluetoothConnectedDevice)

Called when a read request is received from a client device.

protected ReadOnlySpan<byte> OnReadRequestReceived(IBluetoothConnectedDevice device)

Parameters

device IBluetoothConnectedDevice

The 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

device IBluetoothConnectedDevice

The client device that sent the write request.

data byte[]

The data sent in the write request.

timeout TimeSpan?

An optional timeout for the operation.

cancellationToken CancellationToken

A 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

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A 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

localDescriptor IBluetoothLocalDescriptor

The descriptor to remove.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A 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

id Guid

The ID of the descriptor to remove.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A 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

string

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

value ReadOnlyMemory<byte>

The new value.

notifyClients bool

Whether to notify subscribed clients of the value change.

timeout TimeSpan?

The timeout for this operation

cancellationToken CancellationToken

A 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

EventHandler<CharacteristicSubscriptionChangedEventArgs>

ClientUnsubscribed

Event raised when a client device unsubscribes from this characteristic.

public event EventHandler<CharacteristicSubscriptionChangedEventArgs>? ClientUnsubscribed

Event Type

EventHandler<CharacteristicSubscriptionChangedEventArgs>

ReadRequested

Event raised when a client device requests to read this characteristic.

public event EventHandler<CharacteristicReadRequestEventArgs>? ReadRequested

Event Type

EventHandler<CharacteristicReadRequestEventArgs>

WriteRequested

Event raised when a client device requests to write to this characteristic.

public event EventHandler<CharacteristicWriteRequestEventArgs>? WriteRequested

Event Type

EventHandler<CharacteristicWriteRequestEventArgs>