Class CharacteristicAccessor<TRead, TWrite>
Typed accessor for reading and writing a known Bluetooth characteristic.
public class CharacteristicAccessor<TRead, TWrite> : IBluetoothCharacteristicAccessor<TRead, TWrite>, IBluetoothCharacteristicAccessor
Type Parameters
TReadThe typed value produced when reading the characteristic.
TWriteThe typed value accepted when writing the characteristic.
- Inheritance
-
CharacteristicAccessor<TRead, TWrite>
- Implements
-
IBluetoothCharacteristicAccessor<TRead, TWrite>
- 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
serviceIdGuidThe UUID of the service that owns the target characteristic.
characteristicIdGuidThe UUID of the target characteristic.
codecICharacteristicCodec<TRead, TWrite>The codec used to decode reads and encode writes.
serviceNamestringOptional known service name used for diagnostics.
characteristicNamestringOptional known characteristic name used for diagnostics.
Properties
CharacteristicId
Gets the UUID of the target characteristic.
public Guid CharacteristicId { get; }
Property Value
CharacteristicName
Gets the known characteristic name, if available.
public string CharacteristicName { get; }
Property Value
ServiceId
Gets the UUID of the service that owns the target characteristic.
public Guid ServiceId { get; }
Property Value
ServiceName
Gets the known service name, if available.
public string ServiceName { get; }
Property Value
Methods
CanListen(IBluetoothRemoteDevice)
Determines whether the resolved characteristic supports notifications/indications.
public bool CanListen(IBluetoothRemoteDevice device)
Parameters
deviceIBluetoothRemoteDeviceThe connected remote device that owns the characteristic.
Returns
- bool
truewhen the characteristic is already resolved and listenable; otherwisefalse.
CanRead(IBluetoothRemoteDevice)
Determines whether the resolved characteristic can be read.
public bool CanRead(IBluetoothRemoteDevice device)
Parameters
deviceIBluetoothRemoteDeviceThe connected remote device that owns the characteristic.
Returns
- bool
truewhen the characteristic is already resolved and readable; otherwisefalse.
CanWrite(IBluetoothRemoteDevice)
Determines whether the resolved characteristic can be written.
public bool CanWrite(IBluetoothRemoteDevice device)
Parameters
deviceIBluetoothRemoteDeviceThe connected remote device that owns the characteristic.
Returns
- bool
truewhen the characteristic is already resolved and writable; otherwisefalse.
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
deviceIBluetoothRemoteDeviceThe connected remote device that owns the characteristic.
skipIfPreviouslyReadboolWhen true, skips the native read if a previous value is already cached.
timeoutTimeSpan?Optional timeout for exploration and read operations.
cancellationTokenCancellationTokenToken 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
deviceIBluetoothRemoteDeviceThe connected remote device that owns the characteristic.
timeoutTimeSpan?Optional timeout for exploration operations.
cancellationTokenCancellationTokenToken used to cancel the operation.
Returns
- ValueTask<IBluetoothRemoteCharacteristic>
The resolved remote characteristic.
Exceptions
- ArgumentNullException
Thrown when
deviceis 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
deviceIBluetoothRemoteDeviceThe connected remote device that owns the characteristic.
valueTWriteThe typed value to encode and write.
skipIfOldValueMatchesNewValueboolWhen true, skips the native write if current and new values match.
timeoutTimeSpan?Optional timeout for exploration and write operations.
cancellationTokenCancellationTokenToken used to cancel the operation.