LEGO Wireless Protocol v3 device

../_images/hub-lwp3.png
class LWP3Device(hub_kind, name=None, timeout=10000, pair=False, num_notifications=8, connect=True)

Connects to a hub running official LEGO firmware using the LEGO Wireless Protocol v3.

Parameters:
  • hub_kind (int) – The hub type identifier of the hub to connect to.

  • name (str) – The name of the hub to connect to or None to connect to any hub.

  • timeout (int) – The time, in milliseconds, to wait for a connection before raising an exception.

  • pair (bool) – Whether to attempt pairing for a secure connection. This is required for some newer hubs.

  • num_notifications (int) – Number of incoming messages from the remote hub to store before discarding older messages.

  • connect (bool) – Choose False to skip connecting. connect() can be called later to connect.

Changed in version 3.6: Added pair parameter.

Changed in version 3.7: Added num_notifications parameter.

awaitconnect()

Connects to the device. Only needed if you disconnected or initialized with connect=False.

Raises:

OSError – If the connection attempt fails or times out.

name(name)
name() str

Sets or gets the Bluetooth name of the device.

Parameters:

name (str) – New Bluetooth name of the device. If no name is given, this method returns the current name.

Raises:

OSError – If the device is not connected.

awaitwrite(buf)

Sends a message to the remote hub.

Parameters:

buf (bytes) – The raw binary message to send. Maximum 20 bytes.

Raises:
  • ValueError – If the message exceeds 20 bytes.

  • OSError – If the device is not connected or the write fails.

read() bytes | None

Retrieves the oldest buffered message received from the remote hub.

If all buffered messages have already been read, this returns None.

Returns:

The oldest raw binary message or None if there are no more messages.

Changed in version 3.7: Now supports reading multiple buffered messages instead of blocking until one new message was received.

awaitdisconnect()

Disconnects the device.

Raises:

OSError – If disconnecting fails.