Skip to content
/ pygatt Public
forked from peplin/pygatt

Python wrapper for gatttool (from BlueZ) and the BGAPI for access Bluetooth LE Devices

License

Notifications You must be signed in to change notification settings

lchish/pygatt

Repository files navigation

pygatt - Python Module for Bluetooth LE Generic Attribute Profile (GATT).

This Module allows reading and writing to GATT descriptors on devices such as fitness trackers, sensors, and anything implementing standard GATT Descriptor behavior.

pygatt provides a Pythonic API by wrapping two different backends:

  • BlueZ's gatttool command-line utility.
  • Bluegiga's BGAPI, compatble with dongles like the BLED112.

Requires Python 2.7.

Motivation

Despite the popularilty of BLE, we have yet to find a good programming interface for it on desktop computers. Since most peripherals are designed to work with smartphones, this space is neglected. One interactive interface, BlueZ's gatttool, is functional but difficult to use programatically. BlueZ itself obviously works, but the interface leaves something to be desired only works in Linux.

pygatt consists of a front end that provies an API and two interchangable backends that implement the Bluetooth communication using differently. The backend can either use gatttool/BlueZ or a Bluegiga BGAPI compatible inteface. gatttool/BlueZ is Linux only whereas the BGAPI is cross platform.

Front end

The front end class pygatt.pygatt.BLEDevice represents a remote BLE device. The API provides the methods -- connect, char_read, char_write, subscribe -- that you need to interact with the device. When a BluetoothLEDevice object is created, there is an optional argument "backend" that allows for selection of the backend used. The default is gattool but the BGAPI can be used by setting the optional bled112 arguement to an instance of a BGAPIBackend. Note that there are optional arguments for some of the methods of BluetoothLEDevice that must be specified when using one backend and not the other.

BGAPI Backend

This backend uses a minimalist implementation of Bluegiga's BGAPI to execute the Bluetooth communication. In this case, the class used by BluetoothLEDevice is pygatt.backends.BGAPIBackend.

BGAPIBackend in turn uses pygatt.backends.bgapi.bglib to communicate with the BLED112 dongle. BGLib's job is to construct comands for the dongle and parse the bytes received from the dongle into packets. BGAPIBackend's job is to manage the timing of commands, handling of the data, and keep track of the state of the dongle and connection to the remote device.

Dependencies

  • The BGAPI backend should work on Linux, Windows, and Mac OS and has no other external dependencies.

GATTTool Backend

This backend uses gatttool/BlueZ on Linux to execute the Bluetooth communication. In this case, the class used by BluetoothLEDevice is pygatt.gatttool_classes.GATTToolBackend. GATTToolBackend uses the python module pexpect to execute gatttool commannds as if a user were entering them on the comand line.

Dependencies

  • Currently the gatttool backend is currently only tested under Linux as it requires gatttool which is included with BlueZ which is a Linux library.
  • BlueZ >= 5.5
    • Tested on 5.18 and 5.21
    • Ubuntu is stuck on BlueZ 4.x and does not work - you need to build BlueZ from source.

Installation

Install pygatt with pip:

$ pip install https://github.com/stratosinc/pygatt

The BlueZ backend is not supported by default as it requires pexpect, which can only be installed in a UNIX-based environment. If you wish to use that backend, install the optional dependencies with:

$ pip install pygatt[GATTTOOL]

Authors

Derived from the work of several others, see NOTICE.

License

Copyright 2015 Stratos Inc. and Orion Labs

Apache License, Version 2.0 and MIT License. See LICENSE.

About

Python wrapper for gatttool (from BlueZ) and the BGAPI for access Bluetooth LE Devices

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.6%
  • Makefile 0.4%