0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use Bluetooth on Raspberry Pi: Setup and Example

To use Bluetooth on Raspberry Pi, first enable the Bluetooth service and install necessary tools like bluetoothctl. You can then scan for devices or connect using command line or Python libraries such as bluetooth or bluepy.
📐

Syntax

Using Bluetooth on Raspberry Pi involves these main commands and steps:

  • sudo systemctl start bluetooth - Starts the Bluetooth service.
  • bluetoothctl - Opens the Bluetooth control tool for scanning and pairing.
  • scan on - Command inside bluetoothctl to find nearby devices.
  • pair [device_mac] - Pairs with a device using its MAC address.
  • connect [device_mac] - Connects to a paired device.

For Python, use libraries like bluetooth to scan or connect programmatically.

bash
sudo systemctl start bluetooth
bluetoothctl
# Inside bluetoothctl prompt:
scan on
pair XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX
💻

Example

This Python example scans for nearby Bluetooth devices and prints their names and addresses.

python
import bluetooth

print("Scanning for Bluetooth devices...")
nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True)

for addr, name in nearby_devices:
    print(f"Found device {name} with address {addr}")
Output
Scanning for Bluetooth devices... Found device MyPhone with address 01:23:45:67:89:AB Found device Headphones with address 12:34:56:78:9A:BC
⚠️

Common Pitfalls

Common mistakes when using Bluetooth on Raspberry Pi include:

  • Not starting the Bluetooth service before scanning or pairing.
  • Trying to scan or pair without proper permissions (use sudo if needed).
  • Forgetting to install Bluetooth tools with sudo apt install bluetooth bluez python3-bluez.
  • Using outdated Python libraries or mixing Python 2 and 3 environments.

Always check your Raspberry Pi's Bluetooth adapter is recognized with hciconfig.

bash
hciconfig
# If no device shown, Bluetooth adapter may be off or missing
Output
hci0: Type: Primary Bus: USB BD Address: XX:XX:XX:XX:XX:XX ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING RX bytes:1234 acl:0 sco:0 events:78 errors:0 TX bytes:5678 acl:0 sco:0 commands:78 errors:0
📊

Quick Reference

CommandDescription
sudo systemctl start bluetoothStart Bluetooth service
bluetoothctlOpen Bluetooth control tool
scan onScan for nearby devices inside bluetoothctl
pair [MAC]Pair with device by MAC address
connect [MAC]Connect to paired device
hciconfigCheck Bluetooth adapter status
sudo apt install bluetooth bluez python3-bluezInstall Bluetooth tools and Python library

Key Takeaways

Always start the Bluetooth service before using Bluetooth commands on Raspberry Pi.
Use bluetoothctl to scan, pair, and connect devices via command line.
Python's bluetooth library can scan devices programmatically with discover_devices().
Check your Bluetooth adapter status with hciconfig to troubleshoot issues.
Install required packages like bluetooth, bluez, and python3-bluez for full functionality.