-
Notifications
You must be signed in to change notification settings - Fork 5
Python Bluetooth Setup
Nerdboy_Q edited this page Aug 1, 2020
·
1 revision
NOTE : This has been tested and validated w/ the Raspberry Pi 3 B+ model, specifically using python3.7
- Install pybluez python 3.7 -m pip install pybluez
NOTE : This will only allow simple python programming for bluetooth classic, but not BLE (bluetooth Low Energy Devices)
- Run the following install command:
sudo apt-get install libbluetooth-dev
NOTE : As of the current date (July 30, 2020) there is an issue with the BLE portion of the pybluez library, so this must be installed before the necessary additional python libraries can be installed.
- Install gattlib:
pip3 download gattlib
NOTE : this next few lines will depend on what version is downladed after the previous command. Be sure to do a quic
lsto make sure you enter the correct version in the commands that follow.
tar xvzf ./gattlib-0.20200122.tar.gz
cd gattlib-0.20200122/
sed -ie 's/boost_python-py37/boost_python-py3/' setup.py
NOTE: If you are using a different version of python 3, be sure to specify it correctly in the path seen in the command above.
pip3 install .
- Normally accessing the Bluetooth stack is reserved for root; to allow non-root access to the Bluetooth stack we can give Python 3 and hcitool the missing capabilities to access the Bluetooth stack.:
sudo apt-get install libcap2-bin
sudo setcap 'cap_net_raw,cap_net_admin+eip' `readlink -f \`which python3\``
sudo setcap 'cap_net_raw+ep' `readlink -f \`which hcitool\`
- You should now be able to run the following script as a test:
# simple inquiry example - non-bluetooth LE enabled devices only
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("Found {} devices.".format(len(nearby_devices)))
for addr, name in nearby_devices:
print(" {} - {}".format(addr, name))
# bluetooth low energy scan
from bluetooth.ble import DiscoveryService
service = DiscoveryService()
devices = service.discover(2)
for address, name in devices.items():
print("name: {}, address: {}".format(name, address))