Table of Contents

Class BaseBluetoothRemoteL2CapChannel

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

Abstract base class for platform-specific L2CAP channel implementations. Provides common functionality for opening, closing, reading, and writing to L2CAP channels.

public abstract class BaseBluetoothRemoteL2CapChannel : BaseBindableObject, IBluetoothRemoteL2CapChannel, INotifyPropertyChanged, IAsyncDisposable
Inheritance
BaseBluetoothRemoteL2CapChannel
Implements
Inherited Members

Constructors

BaseBluetoothRemoteL2CapChannel(IBluetoothRemoteDevice, int, L2CapChannelOptions?, ILogger?)

Initializes a new instance of the BaseBluetoothRemoteL2CapChannel class with options.

protected BaseBluetoothRemoteL2CapChannel(IBluetoothRemoteDevice parentDevice, int psm, L2CapChannelOptions? options, ILogger? logger = null)

Parameters

parentDevice IBluetoothRemoteDevice

The Bluetooth device this channel belongs to.

psm int

The Protocol/Service Multiplexer (PSM) for this channel.

options L2CapChannelOptions

Optional L2CAP channel configuration options.

logger ILogger

Optional logger for logging channel operations.

Exceptions

ArgumentNullException

Thrown when device is null.

ArgumentOutOfRangeException

Thrown when PSM is not positive.

BaseBluetoothRemoteL2CapChannel(IBluetoothRemoteDevice, int, ILogger?)

Initializes a new instance of the BaseBluetoothRemoteL2CapChannel class.

protected BaseBluetoothRemoteL2CapChannel(IBluetoothRemoteDevice parentDevice, int psm, ILogger? logger = null)

Parameters

parentDevice IBluetoothRemoteDevice

The Bluetooth device this channel belongs to.

psm int

The Protocol/Service Multiplexer (PSM) for this channel.

logger ILogger

Optional logger for logging channel operations.

Exceptions

ArgumentNullException

Thrown when device is null.

ArgumentOutOfRangeException

Thrown when PSM is not positive.

Properties

Device

Gets the device associated with this L2CAP channel.

public IBluetoothRemoteDevice Device { get; }

Property Value

IBluetoothRemoteDevice

IsOpen

Gets a value indicating whether the channel is currently open.

public bool IsOpen { get; protected set; }

Property Value

bool

Mtu

Gets or sets the negotiated MTU for this channel. Updated after the channel is opened.

public int Mtu { get; protected set; }

Property Value

int

Options

Gets the L2CAP channel options.

protected L2CapChannelOptions Options { get; }

Property Value

L2CapChannelOptions

Psm

Gets the Protocol Service Multiplexer (PSM) value for this channel.

public int Psm { get; }

Property Value

int

Methods

CloseAsync(TimeSpan?, CancellationToken)

Closes the L2CAP channel asynchronously.

public ValueTask CloseAsync(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 close operation.

Exceptions

InvalidOperationException

Thrown when the channel is not open.

TimeoutException

Thrown when the operation times out.

OperationCanceledException

Thrown when the operation is cancelled.

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()

Performs asynchronous cleanup of resources used by this channel.

protected virtual ValueTask DisposeAsyncCore()

Returns

ValueTask

A task representing the asynchronous dispose operation.

NativeCloseAsync()

Platform-specific implementation for closing the L2CAP channel.

protected abstract ValueTask NativeCloseAsync()

Returns

ValueTask

A task representing the asynchronous operation.

NativeOpenAsync()

Platform-specific implementation for opening the L2CAP channel.

protected abstract ValueTask NativeOpenAsync()

Returns

ValueTask

A task representing the asynchronous operation.

NativeReadAsync(Memory<byte>)

Platform-specific implementation for reading data from the L2CAP channel.

protected abstract ValueTask<int> NativeReadAsync(Memory<byte> buffer)

Parameters

buffer Memory<byte>

The buffer to read data into.

Returns

ValueTask<int>

A task representing the asynchronous operation, with the number of bytes read.

NativeWriteAsync(ReadOnlyMemory<byte>)

Platform-specific implementation for writing data to the L2CAP channel.

protected abstract ValueTask NativeWriteAsync(ReadOnlyMemory<byte> data)

Parameters

data ReadOnlyMemory<byte>

The data to write.

Returns

ValueTask

A task representing the asynchronous operation.

OnDataReceived(ReadOnlyMemory<byte>)

Raises the DataReceived event when data is received on the channel.

protected void OnDataReceived(ReadOnlyMemory<byte> data)

Parameters

data ReadOnlyMemory<byte>

The data that was received.

OpenAsync(TimeSpan?, CancellationToken)

Opens the L2CAP channel asynchronously.

public ValueTask OpenAsync(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 open operation.

Exceptions

InvalidOperationException

Thrown when the channel is already open.

TimeoutException

Thrown when the operation times out.

OperationCanceledException

Thrown when the operation is cancelled.

ReadAsync(Memory<byte>, TimeSpan?, CancellationToken)

Reads data from the L2CAP channel asynchronously.

public ValueTask<int> ReadAsync(Memory<byte> buffer, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

buffer Memory<byte>

The buffer to read data into.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask<int>

A task that represents the asynchronous read operation. The task result contains the number of bytes read.

WriteAsync(ReadOnlyMemory<byte>, TimeSpan?, CancellationToken)

Writes data to the L2CAP channel asynchronously.

public ValueTask WriteAsync(ReadOnlyMemory<byte> data, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

data ReadOnlyMemory<byte>

The data to write.

timeout TimeSpan?

The timeout for this operation.

cancellationToken CancellationToken

A cancellation token to cancel this operation.

Returns

ValueTask

A task that represents the asynchronous write operation.

Exceptions

ArgumentNullException

Thrown when data is null.

InvalidOperationException

Thrown when the channel is not open.

TimeoutException

Thrown when the operation times out.

OperationCanceledException

Thrown when the operation is cancelled.

Events

DataReceived

Event raised when data is received on the channel.

public event EventHandler<L2CapDataReceivedEventArgs>? DataReceived

Event Type

EventHandler<L2CapDataReceivedEventArgs>