Table of Contents

Class CharacteristicAccessor<TRead, TWrite>

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

Typed accessor for reading and writing a known Bluetooth characteristic.

public class CharacteristicAccessor<TRead, TWrite> : IBluetoothCharacteristicAccessor<TRead, TWrite>, IBluetoothCharacteristicAccessor

Type Parameters

TRead

The typed value produced when reading the characteristic.

TWrite

The typed value accepted when writing the characteristic.

Inheritance
CharacteristicAccessor<TRead, TWrite>
Implements
Inherited Members
Extension Methods

Constructors

CharacteristicAccessor(Guid, Guid, ICharacteristicCodec<TRead, TWrite>, string?, string?)

Initializes a new instance of the CharacteristicAccessor<TRead, TWrite> class.

public CharacteristicAccessor(Guid serviceId, Guid characteristicId, ICharacteristicCodec<TRead, TWrite> codec, string? serviceName = null, string? characteristicName = null)

Parameters

serviceId Guid

The UUID of the service that owns the target characteristic.

characteristicId Guid

The UUID of the target characteristic.

codec ICharacteristicCodec<TRead, TWrite>

The codec used to decode reads and encode writes.

serviceName string

Optional known service name used for diagnostics.

characteristicName string

Optional known characteristic name used for diagnostics.

Properties

CharacteristicId

Gets the UUID of the target characteristic.

public Guid CharacteristicId { get; }

Property Value

Guid

CharacteristicName

Gets the known characteristic name, if available.

public string CharacteristicName { get; }

Property Value

string

ServiceId

Gets the UUID of the service that owns the target characteristic.

public Guid ServiceId { get; }

Property Value

Guid

ServiceName

Gets the known service name, if available.

public string ServiceName { get; }

Property Value

string

Methods

CanListen(IBluetoothRemoteDevice)

Determines whether the resolved characteristic supports notifications/indications.

public bool CanListen(IBluetoothRemoteDevice device)

Parameters

device IBluetoothRemoteDevice

The connected remote device that owns the characteristic.

Returns

bool

true when the characteristic is already resolved and listenable; otherwise false.

CanRead(IBluetoothRemoteDevice)

Determines whether the resolved characteristic can be read.

public bool CanRead(IBluetoothRemoteDevice device)

Parameters

device IBluetoothRemoteDevice

The connected remote device that owns the characteristic.

Returns

bool

true when the characteristic is already resolved and readable; otherwise false.

CanWrite(IBluetoothRemoteDevice)

Determines whether the resolved characteristic can be written.

public bool CanWrite(IBluetoothRemoteDevice device)

Parameters

device IBluetoothRemoteDevice

The connected remote device that owns the characteristic.

Returns

bool

true when the characteristic is already resolved and writable; otherwise false.

ReadAsync(IBluetoothRemoteDevice, bool, TimeSpan?, CancellationToken)

Reads and decodes the typed value from the characteristic.

public ValueTask<TRead> ReadAsync(IBluetoothRemoteDevice device, bool skipIfPreviouslyRead = false, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The connected remote device that owns the characteristic.

skipIfPreviouslyRead bool

When true, skips the native read if a previous value is already cached.

timeout TimeSpan?

Optional timeout for exploration and read operations.

cancellationToken CancellationToken

Token used to cancel the operation.

Returns

ValueTask<TRead>

The decoded typed value.

ResolveCharacteristicAsync(IBluetoothRemoteDevice, TimeSpan?, CancellationToken)

Resolves the remote characteristic on the specified device.

public ValueTask<IBluetoothRemoteCharacteristic> ResolveCharacteristicAsync(IBluetoothRemoteDevice device, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The connected remote device that owns the characteristic.

timeout TimeSpan?

Optional timeout for exploration operations.

cancellationToken CancellationToken

Token used to cancel the operation.

Returns

ValueTask<IBluetoothRemoteCharacteristic>

The resolved remote characteristic.

Exceptions

ArgumentNullException

Thrown when device is null.

CharacteristicAccessorResolutionException

Thrown when the service or characteristic cannot be resolved.

WriteAsync(IBluetoothRemoteDevice, TWrite, bool, TimeSpan?, CancellationToken)

Encodes and writes the typed value to the characteristic.

public ValueTask WriteAsync(IBluetoothRemoteDevice device, TWrite value, bool skipIfOldValueMatchesNewValue = false, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

device IBluetoothRemoteDevice

The connected remote device that owns the characteristic.

value TWrite

The typed value to encode and write.

skipIfOldValueMatchesNewValue bool

When true, skips the native write if current and new values match.

timeout TimeSpan?

Optional timeout for exploration and write operations.

cancellationToken CancellationToken

Token used to cancel the operation.

Returns

ValueTask