Skip to content

Channels

aio_rfcomm.RfcommChannel

An open RFCOMM channel.

It carries a stream of bytes, not separate messages. One send on the other end may arrive in several pieces, or joined to the next.

When the connection ends, receive first returns any bytes that arrived before the end. After that, every call to receive or send raises the same error: ChannelClosedError, or BluetoothUnavailableError if Bluetooth stopped being usable.

One task can send while another receives. Two tasks receiving at once, or two sending, raises BusyResourceError.

send async

send(data: bytes) -> None

Send all of data.

It returns once the data is on its way, which does not mean the other device has it yet. If it is canceled, the data is still sent, unless the connection ends first.

Parameters:

  • data (bytes) –

    The bytes to send.

Raises:

receive async

receive(max_bytes: int | None = None) -> bytes

Wait for bytes to arrive and return them.

Parameters:

  • max_bytes (int | None, default: None ) –

    Return at most this many bytes. None returns everything that has arrived. It does not wait for this many.

Returns:

  • bytes –

    At least one byte, and at most max_bytes.

Raises:

  • ChannelClosedError –

    The connection has ended, and every byte that arrived before the end has been returned.

  • BluetoothUnavailableError –

    Bluetooth stopped being usable, and every byte that arrived before then has been returned.

  • ValueError –

    max_bytes is less than 1.

receive_exactly async

receive_exactly(count: int) -> bytes

Wait for exactly count bytes and return them.

If it is canceled, the bytes it has read so far are kept for the next call.

Parameters:

  • count (int) –

    How many bytes, at least 1.

Returns:

  • bytes –

    Exactly count bytes.

Raises:

receive_until async

receive_until(delimiter: bytes, *, max_bytes: int) -> bytes

Read up to a delimiter, and return the bytes before it.

The delimiter is read too, but not returned. To read a line:

line = await channel.receive_until(b"\n", max_bytes=1024)

If it is canceled, the bytes it has read so far are kept for the next call.

Parameters:

  • delimiter (bytes) –

    The bytes that end each message. Not empty.

  • max_bytes (int) –

    The most to read, delimiter included. This stops a device that never sends a delimiter from using up memory. At least the length of the delimiter.

Returns:

  • bytes –

    The bytes before the delimiter.

Raises:

  • DelimiterNotFoundError –

    max_bytes arrived with no delimiter. None of them are read, so calling this again raises again. Read them with receive, or end the connection.

  • ChannelClosedError –

    The connection ended first. receive returns the bytes that did arrive.

  • BluetoothUnavailableError –

    Bluetooth stopped being usable first. receive returns the bytes that did arrive.

  • ValueError –

    delimiter is empty, or longer than max_bytes.