BluetoothManager()
Main class to establish and manage Bluetooth connection with Bittle.
Attributes
name (str): Bittle device name. Physical device name is BittleSPP-XXXXXX by default. This attribute is used to search for Bittle among paired devices, if your Bittle has a different name than BittleSPP-XXXXXX, you must set this attribute with it's name.
address (str): Bittle's pyhsical address. This attribute is used to stablish Bluetooth communication with Bittle.
port (int): RFCOMM port. This attribute is used to stablish Bluetooth communication with Bittle.
discovery_timeout (int): Timeout for discovering paired devices in seconds.
recv_timeout (int): Timeout for receiving messages from Bittle once the connection has been established.
socket (bluetooth.BkuetoothSocket): Bluetoth RFCOMM connection socket.
Methods
initialize_name_and_address(get_first_bittle=True): Search for Bittle's name among paired devices; if its found, saves the name and address in class attributes and return (name, address); otherwise will return (None, None). If get_first_bittle is set to True, will search for BittleSPP and return first found device; if its set to False, will search for name class attribute.
get_paired_devices(): Returns a list with (MAC address (str), device name (str)) tuples.
connect(): Connects to Bittle using address and port class attributes, for connecting to a different port than default one, port class attribute value must be set. Once connection is established, listens to Bittle response messages and after finishing handshake ("Finished!" is received from Bittle), returns True. If handshake fails (due to timeout or connection lost), returns False.
send_msg(msg): Sends passed msg parameter (str) to Bittle.
recv_msg(buffer_size=1024): Returns received message from Bittle.
close_connection(): Closes established connection with Bittle.
Example
The following example shows the complete process of searching, connecting and sending messages to Bittle through Bluetooth connection.
Last updated