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 make
  • mkdir -p work/bluetooth
  • cd work/bluetooth
  • wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.32.tar.xz
  • tar xvf bluez-5.32.tar.xz
  • cd bluez-5.32
  • ./configure --disable-systemd --enable-library
  • make
  • sudo 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.32
  • cd /usr/include
  • sudo mv bluetooth bluetooth.4.99
  • sudo 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-pip
  • sudo apt-get install python-dev libbluetooth-dev libboost-all-dev
  • sudo pip install pybluez
  • sudo pip install pybluez[ble]

A sample python script with pybluez.

  • cd work/bluetooth
  • touch scan_ble.py
  • vim 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/bluetooth
  • git clone https://github.com/switchdoclabs/iBeacon-Scanner-.git
  • cd iBeacon-Scanner-
  • sudo python testblescan.py

Comments

Popular posts from this blog

What I did until many slimes spawn on slime chunk

Major Maven goals