Transports¶
A transport represents a channel over which libpebble2 can communicate with a Pebble. Transports can also support sending and receiving messages not destined for the Pebble — for instance, the WebSocket transport can install a JavaScript app in the phone app. Two transports are currently provided, but it would be easy to add more.
Transports are usually only accessed directly by PebbleConnection
, but it can be useful to use them
directly to interact with the transport instead of a Pebble. The transport can be accessed using the transport
attribute of the PebbleConnection
.
The origin or destination of a message is indicated using a “message target”. Messages to or from the watch use
MessageTargetWatch
; other transports may define additional targets of their own, which they can use to route
messages elsewhere.
BaseTransport¶
BaseTransport
defines the functionality expected of any transport. All transports should inherit from
BaseTransport
.
-
class
libpebble2.communication.transports.
BaseTransport
¶ -
connect
()¶ Synchronously connect to the Pebble. Once this method returns, libpebble2 should be able to safely send messages to the connected Pebble.
Ordinarily, this method should only be called by
PebbleConnection
.
-
connected
¶ Returns: True
if the transport is currently connected; otherwiseFalse
.
-
must_initialise
¶ Returns: True
if libpebble2 is responsible for negotiating the connection; otherwiseFalse
.
-
read_packet
()¶ Synchronously read a message. This message could be from the Pebble(in which case it will be a
PebblePacket
), or it could be from the transport, in which case the result is transport-defined. The origin of the result is indicated by the returnedMessageTarget
.Returns: ( MessageTarget
,libpebble2.protocol.base.PebblePacket
)
-
send_packet
(message, target=MessageTargetWatch())¶ Send a message. This message could be to the Pebble (in which case it must be a
PebblePacket
), or to the transport (in which case the message type is transport-defined).Parameters: - message (PebblePacket) – Message to send.
- target (MessageTarget) – Target for the message
-
-
class
libpebble2.communication.transports.
MessageTargetWatch
¶
-
class
libpebble2.communication.transports.
MessageTarget
¶
WebSocket transport¶
The WebSocket transport connects to a phone running the Pebble mobile app using the “Developer Connection”, which exposes a WebSocket server on the phone. By default it runs on port 9000.
>>> pebble = PebbleConnection(WebsocketTransport("ws://192.168.204:9000/"))
-
class
libpebble2.communication.transports.websocket.
WebsocketTransport
(url)¶ Bases:
libpebble2.communication.transports.BaseTransport
Represents a connection via WebSocket to a phone running the Pebble mobile app, which is in turn connected to a Pebble over Bluetooth.
Parameters: url – The WebSocket URL to connect to, in standard format (e.g. ws://localhost:9000/
)
-
class
libpebble2.communication.transports.websocket.
MessageTargetPhone
¶ Bases:
libpebble2.communication.transports.MessageTarget
Indicates that the message is directed at a connected phone running the Pebble mobile app. For this purpose, pypkjs counts as a phone.
QEMU transport¶
The QEMU transport connects to an instance of Pebble QEMU via Pebble QEMU Protocol. Note that, due to how QEMU is implemented, the watch will not necessarily notice connections or disconnections over this transport.
Messages directed at the emulator itself, rather than the firmware running on it, can be sent using
MessageTargetQemu
.
>>> pebble = PebbleConnection(QemuTransport("localhost", 12344))
-
class
libpebble2.communication.transports.qemu.
QemuTransport
(host='127.0.0.1', port=12344)¶ Bases:
libpebble2.communication.transports.BaseTransport
Represents a connection to a Pebble QEMU instance.
Parameters: -
BUFFER_SIZE
= 2048¶ Number of bytes read from the socket at a time.
-
-
class
libpebble2.communication.transports.qemu.
MessageTargetQemu
(protocol=None, raw=False)¶ Bases:
libpebble2.communication.transports.MessageTarget
Indicates that a message is directed at QEMU, rather than the firmware running on it. If
raw
isTrue
, the message should be a binary message (without framing), with QEMU protocol indicated byprotocol
. Otherwise, the message should be aPebblePacket
fromprotocol
.Parameters:
Serial transport¶
It is possible to connect directly to the Pebble using SerialTransport
. This transport uses the operating
system’s built-in Bluetooth serial support to communicate with the watch using
pyserial. Using this transport requires the Pebble to already be paired
with the computer. Recall that the Pebble may only connect to one device at a time; disconnect any connected phones
(e.g. by disabling Bluetooth) before attempting to pair with your computer or use this transport.
Since this transport connects directly to the watch, it does not define any other message targets.
-
class
libpebble2.communication.transports.serial.
SerialTransport
(device)¶ Bases:
libpebble2.communication.transports.BaseTransport
Represents a direct connection to a physical Pebble paired to the computer via Bluetooth serial. This transport expects to be given a device file over which it can communicate with the watch via Bluetooth.
Warning
Using this transport may cause occasional kernel panics on some versions of OS X.
Parameters: device (str) – The path to the device file (on OS X, often of the form /dev/cu.PebbleTimeXXXX-SerialPo
or/dev/cu.PebbleXXXX-SerialPortSe
).