Detect Bluetooth LE Device with BlueZ on RaspberryPi
BlueZ is a project to control Bluetooth Device on Linux.
BlueZ is also required when controlling BLE with RaspberryPi.
In this article, how to install BlueZ and I tried to run a simple Python script sample with BlueZ.
Versions
- Raspberry Pi Type B Single Board Computer 512MB
- Raspbian 7.8
- Python 2.7
- BlueZ 5.32
- pybluez 0.22
- Boost 1.49
Install BlueZ into RPi
sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev makemkdir -p work/bluetoothcd work/bluetoothwget https://www.kernel.org/pub/linux/bluetooth/bluez-5.32.tar.xztar xvf bluez-5.32.tar.xzcd bluez-5.32./configure --disable-systemd --enable-librarymakesudo make install
Manually install commands that are not installed by make.
sudo cp attrib/gatttool /usr/local/bin/sudo cp -ipr lib/ /usr/include/bluetooth.5.32cd /usr/includesudo mv bluetooth bluetooth.4.99sudo ln -s bluetooth.5.32/ bluetooth
Detecting test with hcitool command.
sudo hcitool lescan
If there are BLE devices nearby, hcitool displays the Mac address and device name.
If you run hcidump & you can check the detailed information.
Install pybluez into RPi
sudo apt-get -y install python-pipsudo apt-get install python-dev libbluetooth-dev libboost-all-devsudo pip install pybluezsudo pip install pybluez[ble]
A sample python script with pybluez.
cd work/bluetoothtouch scan_ble.pyvim scan_ble.py
# 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))
sudo python scan_ble.py
If you get an error (Error: connect: Connection refused (111)) please update Raspbian version (to 8.0 (Jessie)) and reinstall BlueZ.
Or have another sample script so try it.
For example:
cd work/bluetoothgit clone https://github.com/switchdoclabs/iBeacon-Scanner-.gitcd iBeacon-Scanner-sudo python testblescan.py
Comments
Post a Comment