hub.BT_VCP – BT_VCP

This class allows you to send and receive data from another device using Bluetooth.

class hub.BT_VCP(id=0)

Create a stream-like object representing the Bluetooth virtual comm port. It can be used to read and write data over Bluetooth to the connected host.

Only id=0 can be used.

setinterrupt(chr: int)None

Sets the character which interrupts running Python code. This is set to 3 (CTRL-C) by default. When this character is received over the VCP port, a KeyboardInterrupt exception is raised.

Choose -1 to disable this interrupt. This is useful when you want to send raw bytes over the VCP port.

isconnected()bool

Checks if there is an active connection.

Returns

True if connected, False if not.

any()bool

Checks if characters are waiting to be read.

Returns

True if any characters are waiting, False if not.

close()None

This method does nothing. It exists so that this object can act as a file.

read()Optional[bytes]
read(nbytes: int)Optional[bytes]

Reads data from the port in a non-blocking way.

Parameters

nbytes – How many bytes to read at most. If no value is given, it reads all available data.

Returns

The received bytes, or None if no pending data is available.

readinto(buf)Optional[int]
readinto(buf, maxlen: int)Optional[int]

Reads data from the port into an existing buffer in a non-blocking way.

Parameters

maxlen – How many bytes to read. At most min(maxlen, len(buf)) bytes will be read.

Returns

The number of bytes stored into buf, or None if no pending data is available.

readline()Optional[bytes]

Reads a whole line from the serial device.

Returns

The data including the trailing newline character, or None if no pending data is available.

readlines()list

Reads as much data as possible from the serial device and breaks it into lines.

Returns

Returns a list of bytes objects, each object being one of the lines. Each line includes the newline character. Returns an empty list if no pending data is available.

write(buf: bytes)int

Writes the given bytes to the serial device.

Returns

The number of bytes written.

recv(data: Union[int, bytearray], timeout=5000)int

Receives data on the bus.

Parameters
  • data – Either an integer specifying the number of bytes to receive, or a mutable buffer, to be be filled with received bytes.

  • timeout – Timeout in milliseconds to wait for the incoming data.

Returns

A new buffer with received bytes if data was an integer, or the number of bytes read into the buffer if a buffer was given.

send(data: Union[int, bytes], timeout=5000)int

Send data on the bus.

Parameters
  • data – An integer or buffer object to write.

  • timeout – Timeout in milliseconds to wait for the data to be sent.

Returns

The number of bytes sent.

callback(function: Callable[[int], None])None
callback()Callable[[int], None]

Sets the callback function that is called when the connection status changes. The function must accept one argument, which indicates the new status: 1 means connected, while 0 means disconnected.

Parameters

function – Callable function that takes one argument. Choose None to disable the callback.

Returns

If no argument is given, this returns the previously set callback function, or None if it was not set.