/python-gatt-server

Primary LanguagePythonOtherNOASSERTION

Python BLE GATT Server (peripheral)

GATT is constructed out of one or more server devices (BLE peripherals) and a client device (BLE central).

A GATT server is usually a small device such as a sensor, but for some use cases you might want to have a Linux computer such as a RPi used as a GATT server. This example is meant to demonstrate how this can be done.

Setup

The instructions in this document were tested on Ubuntu 16.04.

Bluez Installation

On most distributions, you will need to upgrade Bluez in order to make this work.

Install dependencies:

sudo apt-get update
sudo apt-get install libudev-dev libical-dev libreadline-dev libglib2.0-dev libdbus-1-dev

Download and install Bluez

wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.45.tar.xz
tar xvf bluez-5.45.tar.xz
cd bluez-5.45/
./configure
make
sudo make install

Added by Rida: Fixes required in bluez code for successful compilation (in case it fails):

'SIOCGSTAMP' undeclared (first use in this function); did you mean 'SIOCSRARP’ resolution: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=e62786ddceb7376cb9abf97d0b89fdc62ea7dcb3

add #include<linux/sockios.h> to tools/rctest.c
add #include<linux/sockios.h> to tools/l2test.c

Enable experimental features for the bluetooth driver:

  • sudo nano /lib/systemd/system/bluetooth.service

  • Add --experimental to ExecStart. The line should look like this:

    ExecStart=/usr/local/libexec/bluetooth/bluetoothd --experimental

Reload the service:

systemctl daemon-reload
sudo service bluetooth restart

virtualenv

Install dependencies: sudo apt-get install virtualenv python-dev libdbus-1-dev libdbus-glib-1-dev python-gi

cd to the the root of this repository and:

virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
[most probably not required!] ln -s /usr/lib/python2.7/dist-packages/gi venv/lib/python2.7/site-packages/

Installing some Ubuntu and Python packages

sudo apt-get install pkg-config libcairo2-dev gcc python3-dev libgirepository1.0-dev
sudo apt-get install python-dbus
pip install gobject PyGObject
pip install dbus-python

Usage

Important note: I noticed that whenever I try to connect to GATT simulator from a client - that I built on Windows OS - then I had to pair Windows and Linux machine first and then I can find this simulated device.

  • To start the sample BLE GATT server: python gatt_server_example.py

  • To start UI-based BLE GATT server: python3 ./simulator.py

You can use a smartphone as a GATT client. I used the GATT-IP app, here it is on the Google Play Store and on the App Store

Changing the name of Ubunuto OS to "LE-N**I-SIMULATOR"

  • This ensures the simulated BLE device will have "LE-NI-SIMULATOR" name. And thus "NI Hub" tool reports without the need to apply any hacks in the SDK.
  • It can be accomplied by going to "Settings" -> "About" -> and then editing "Device Name"
  • You may need to reboot Ubunuto OS to take the effect.

License

The code in this repository is based on code taken from the BlueZ project. It is licensed under GPL 2.0