Starting with Raspberry Pi Bluetooth & Python

March 25, 2020

I recently started doing a small fun project with a RPi and a BT enable remote controlled car. (Dagu Racer 1) when I hit some problems with writing Python code to connect to the RPi’s BT.

After some web browsing and a bit of trial and error I found the following application updates and library installs got things working.

sudo apt-get update
sudo apt-get install bluetooth bluez libbluetooth-dev
sudo python3 -m pip install pybluez

For the Python code

import bluetooth

# Bluetooth stuff
bd_addr = “20:13:05:30:01:14”
port = 1
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))

# 0x1X for straight forward and 0x11 for very slow to 0x1F for fastest