Skip to main content

A library for building BLE peripherals using GATT and bluez

Project description

bluez-peripheral

Documentation

PyPi

GitHub

A bluez-peripheral is a library for building Bluetooth Low Energy (BLE) peripherals using the Bluez GATT API.

Who this Library is For

  • Developers using Python and Linux (and Bluez).
  • Wishing to develop a bluetooth compatible peripheral (ie. something that other devices connect to).
  • With low bandwidth requirements (ie. not streaming audio).
  • Where performance is not a priority.

Installation

pip install bluez-peripheral

GATT Overview

GATT is a BLE protocol that allows you to offer services to other devices. You can find a list of existing services on the Bluetooth SIG website (you can largely ignore profiles when working with BLE). You should refer to the "Service Characteristics" in these specifications for the purposes of this library.

Peripheral Hierarchy Diagram

Courtesey of Qt documentation (GNU Free Documentation License)

A peripheral defines a list of services that it provides. Services are a collection of characteristics which expose particular data (eg. a heart rate or mouse position). Characteristics may also have descriptors that contain metadata (eg. the units of a characteristic). Services can optionally include other services. All BLE attributes (Services, Characterisics and Descriptors) are identified by a 16-bit number assigned by the Bluetooth SIG.

Characteristics may operate in a number of modes depending on their purpose. By default characteristics are read-only in this library however they may also be writable and provide notification (like an event system) when their value changes. Additionally some characteristics require security protection. You can read more about BLE on the Bluetooth SIG blog.

Usage

Note: Do not attempt to create the Generic Access Service or a Client Characteristic Configuration descriptor. These are both handled automatically by Bluez and attempting to define them will result in errors.

The easiest way to use the library is to create a class describing the service that you wish to provide.

from bluez_peripheral.gatt.service import Service
from bluez_peripheral.gatt.characteristic import characteristic, CharacteristicFlags as CharFlags

import struct

class HeartRateService(Service):
    def __init__(self):
        # Base 16 service UUID, This should be a primary service.
        super().__init__("180D", True)

    @characteristic("2A37", CharFlags.NOTIFY)
    def heart_rate_measurement(self, options):
        # This function is called when the characteristic is read.
        # Since this characteristic is notify only this function is a placeholder.
        # You don't need this function Python 3.9+ (See PEP 614).
        # You can generally ignore the options argument 
        # (see Advanced Characteristics and Descriptors Documentation).
        pass

    def update_heart_rate(self, new_rate):
        # Call this when you get a new heartrate reading.
        flags = 0

        # Bluetooth data is little endian.
        rate = struct.pack("<BB", flags, new_rate)
        self.heart_rate_measurement.changed(rate)

Bluez interfaces with bluez-peripheral using dbus for inter-process communication. For Bluez to start offering your service it needs to be registered on this bus. Additionally if you want devices to pair with your device you need to register an agent to decide how pairing should be completed. Finally you also need to advertise the service to nearby devices.

from bluez_peripheral.util import *
from bluez_peripheral.advert import Advertisement
from bluez_peripheral.agent import NoIoAgent
import asyncio

async def main():
    # Alternativly you can request this bus directly from dbus_next.
    bus = await get_message_bus()

    service = HeartRateService()
    await service.register(bus)

    # An agent is required to handle pairing 
    agent = NoIoAgent()
    # This script needs superuser for this to work.
    await agent.register(bus)

    adapter = await Adapter.get_first(bus)

    # Start an advert that will last for 60 seconds.
    advert = Advertisement("Heart Monitor", ["180D"], 0x0340, 60)
    await advert.register(bus, adapter)

    # Handle any dbus requests.
    await bus.wait_for_disconnect()

if __name__ == "__main__":
    asyncio.run(main())

To communicate with bluez the default dbus configuration requires that you be in the bluetooth user group. For more examples please read the documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bluez_peripheral-0.1.1.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

bluez_peripheral-0.1.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file bluez_peripheral-0.1.1.tar.gz.

File metadata

  • Download URL: bluez_peripheral-0.1.1.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for bluez_peripheral-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a4f4738e9c347ffaa0d008ca2ee8953ae12ef8a31ac1e2d7f6a126df35c38aed
MD5 e5e2377886f7a427e0186b92c4078ad8
BLAKE2b-256 f6f9f1ad1ca5ebab17d246e01729db3a19dbd47ad27856af6d2e11cd24a5bad3

See more details on using hashes here.

File details

Details for the file bluez_peripheral-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bluez_peripheral-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for bluez_peripheral-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b43bb440e03a9c2349663f5d98284239e60909d6a663de96a1395ab95f394473
MD5 2798e5eb72b0eb1f89e65afcb719fb0b
BLAKE2b-256 9c9e69d21902f377e720557b7def7bd74f68514f041a6c7727db51d2aaae791c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page