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.
"""Example:
There are the the following paired devices to your computer:
name: Mi 9T Phone -- address: 68:91:6D:DB:0E:14
name: Xbox Wireless Controller -- address: C4:7E:FF:A9:85:C7
name: BittleSPP-1883H2 -- address: 4D:AF:18:95:0D:1D
name: RaspberryPi -- address: DF:78:4A:40:EA:7E
name: BittleSPP-9833L7 -- address: 0F:C4:9B:59:41:67
name: MyCustomBittleName -- address: B4:B5:E5:43:30:CC
"""
btManager = BluetoothManager() # Create manager
# Obtaining first BittleSPP ocurrence's name and address:
name, addr = btManager.initialize_name_and_address()
# btManager.name = 'BittleSPP-1883H2'
# btManager.address = '4D:AF:18:95:0D:1D'
# name = 'BittleSPP-1883H2'
# addr = '4D:AF:18:95:0D:1D'
# Obtaining BittleSPP-9833L7 address:
btManager.name = 'BittleSPP-9833L7'
name, addr = btManager.initialize_name_and_address(False)
# btManager.name = 'BittleSPP-9833L7'
# btManager.address = '0F:C4:9B:59:41:67'
# name = 'BittleSPP-9833L7'
# addr = '0F:C4:9B:59:41:67'
# Obtaining MyCustomBittleName address:
btManager.name = 'MyCustomBittleName'
name, addr = btManager.initialize_name_and_address(False)
# btManager.name = 'MyCustomBittleName'
# btManager.address = 'B4:B5:E5:43:30:CC'
# name = 'MyCustomBittleName'
# addr = 'B4:B5:E5:43:30:CC'
get_paired_devices(): Returns a list with (MAC address (str), device name (str)) tuples.
"""Example:
There are the the following paired devices to your computer:
name: Mi 9T Phone -- address: 68:91:6D:DB:0E:14
name: RaspberryPi -- address: DF:78:4A:40:EA:7E
name: BittleSPP-1883H2 -- address: 4D:AF:18:95:0D:1D
"""
btManager = BluetoothManager() # Create manager
paired_devices = btManager.get_paired_devices()
# paired_devices = [
# ('68:91:6D:DB:0E:14', 'Mi 9T Phone'),
# ('DF:78:4A:40:EA:7E', 'RaspberryPi'),
# ('4D:AF:18:95:0D:1D', 'BittleSPP-1883H2')
# ]
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.
import time
from pyBittle import bluetoothManager
# Create BluetoothManager instance
btManager = bluetoothManager.BluetoothManager()
# Get Bittle's name and MAC address
name, addr = btManager.initialize_name_and_address()
# btManager.name has Bittle's name too
# btManager.address has Bittle's MAC address too
if name and addr: # If Bittle is found
# Connect to found address and default port
connected = btManager.connect()
if connected:
btManager.send_msg("khi") # Send GREETING command
received = btManager.recv_msg() # Received msg (bytes)
# Don't send commands too quickly, it could be
# dangerous for Bittle's servos
time.sleep(6) # Wait for GREETING action to finish
btManager.send_msg("d") # Send SHUTDOWN command
received = btManager.recv_msg()
btManager.close_connection()
Last updated
Was this helpful?