{"id":18421901,"url":"https://github.com/getsenic/gatt-python","last_synced_at":"2025-04-04T09:07:06.685Z","repository":{"id":38708290,"uuid":"83462108","full_name":"getsenic/gatt-python","owner":"getsenic","description":"Bluetooth GATT SDK for Python","archived":false,"fork":false,"pushed_at":"2022-06-23T15:06:31.000Z","size":47,"stargazers_count":324,"open_issues_count":49,"forks_count":85,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-28T08:06:32.680Z","etag":null,"topics":["bluetooth","bluetooth-le","bluetooth-low-energy","bluez","bluez-dbus","gatt"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/getsenic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-28T17:45:12.000Z","updated_at":"2025-02-18T22:55:16.000Z","dependencies_parsed_at":"2022-08-09T05:30:45.084Z","dependency_job_id":null,"html_url":"https://github.com/getsenic/gatt-python","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getsenic%2Fgatt-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getsenic%2Fgatt-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getsenic%2Fgatt-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/getsenic%2Fgatt-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/getsenic","download_url":"https://codeload.github.com/getsenic/gatt-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247149500,"owners_count":20891954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bluetooth","bluetooth-le","bluetooth-low-energy","bluez","bluez-dbus","gatt"],"created_at":"2024-11-06T04:27:13.853Z","updated_at":"2025-04-04T09:07:06.667Z","avatar_url":"https://github.com/getsenic.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bluetooth GATT SDK for Python\nThe Bluetooth GATT SDK for Python helps you implementing and communicating with any Bluetooth Low Energy device that has a GATT profile. As of now it supports:\n\n* Discovering nearby Bluetooth Low Energy devices\n* Connecting and disconnecting devices\n* Implementing your custom GATT profile\n* Accessing all GATT services\n* Accessing all GATT characteristics\n* Reading characteristic values\n* Writing characteristic values\n* Subscribing for characteristic value change notifications\n\nCurrently Linux is the only platform supported by this library. Unlike other libraries this GATT SDK is based directly on the mature and stable D-Bus API of BlueZ to interact with Bluetooth devices. In the future we would like to make this library platform-independent by integrating with more Bluetooth APIs of other operating systems such as MacOS and Windows.\n\n## Prerequisites\nThe GATT SDK requires [Python 3.4+](https://www.python.org). Currently Linux is the only supported operating system and therefor it needs a recent installation of [BlueZ](http://www.bluez.org/). It is tested to work fine with BlueZ 5.44, any release \u003e= 5.38 should however work, too.\n\n## Installation\nThese instructions assume a Debian-based Linux.\n\nOn Linux the [BlueZ](http://www.bluez.org/) library is necessary to access your built-in Bluetooth controller or Bluetooth USB dongle. Some Linux distributions provide a more up-to-date BlueZ package, some other distributions only install older versions that don't implement all Bluetooth features needed for this SDK. In those cases you want to either update BlueZ or build it from sources.\n\n### Updating/installing BlueZ via apt-get\n\n1. `bluetoothd --version` Obtains the version of the pre-installed BlueZ. `bluetoothd` daemon must run at startup to expose the Bluetooth API via D-Bus.\n2. `sudo apt-get install --no-install-recommends bluetooth` Installs BlueZ\n3. If the installed version is too old, proceed with next step: [Installing BlueZ from sources](#installing-bluez-from-sources)\n\n### Installing BlueZ from sources\n\nThe `bluetoothd` daemon provides BlueZ's D-Bus interfaces that is accessed by the GATT SDK to communicate with Bluetooth devices. The following commands download BlueZ 5.44 sources, built them and replace any pre-installed `bluetoothd` daemon. It's not suggested to remove any pre-installed BlueZ package as its deinstallation might remove necessary Bluetooth drivers as well.\n\n1. `sudo systemctl stop bluetooth`\n2. `sudo apt-get update`\n3. `sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev libdbus-glib-1-dev unzip`\n4. `cd`\n5. `mkdir bluez`\n6. `cd bluez`\n7. `wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.44.tar.xz`\n8. `tar xf bluez-5.44.tar.xz`\n9. `cd bluez-5.44`\n10. `./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-library`\n11. `make`\n12. `sudo make install`\n13. `sudo ln -svf /usr/libexec/bluetooth/bluetoothd /usr/sbin/`\n14. `sudo install -v -dm755 /etc/bluetooth`\n15. `sudo install -v -m644 src/main.conf /etc/bluetooth/main.conf`\n16. `sudo systemctl daemon-reload`\n17. `sudo systemctl start bluetooth`\n18. `bluetoothd --version` # should now print 5.44\n\nPlease note that some distributions might use a different directory for system deamons, apply step 13 only as needed.\n\n### Enabling your Bluetooth adapter\n\n1. `echo \"power on\" | sudo bluetoothctl` Enables your built-in Bluetooth adapter or external Bluetooth USB dongle\n\n### Using BlueZ commandline tools\nBlueZ also provides an interactive commandline tool to interact with Bluetooth devices. You know that your BlueZ installation is working fine if it discovers any Bluetooth devices nearby.\n\n`sudo bluetoothctl` Starts an interactive mode to talk to BlueZ\n  * `power on` Enables the Bluetooth adapter\n  * `scan on` Start Bluetooth device scanning and lists all found devices with MAC addresses\n  * `connect AA:BB:CC:DD:EE:FF` Connects to a Bluetooth device with specified MAC address\n  * `exit` Quits the interactive mode\n\n### Installing GATT SDK for Python\n\nTo install this GATT module and the Python3 D-Bus dependency globally, run:\n\n```\nsudo pip3 install gatt\nsudo apt-get install python3-dbus\n```\n\n#### Running the GATT control script\n\nTo test if your setup is working, run the `gattctl` tool that is part of this SDK. Note that it must be run as root because on Linux, Bluetooth discovery by default is a restricted operation.\n\n```\nsudo gattctl --discover\nsudo gattctl --connect AA:BB:CC:DD:EE:FF # Replace the MAC address with your Bluetooth device's MAC address\nsudo gattctl --help # To list all available commands\n```\n\n## SDK Usage\n\nThis SDK requires you to create subclasses of `gatt.DeviceManager` and `gatt.Device`. The other two classes `gatt.Service` and `gatt.Characteristic` are not supposed to be subclassed.\n\n`gatt.DeviceManager` manages all known Bluetooth devices and provides a device discovery to discover nearby Bluetooth Low Energy devices. You want to subclass this manager to access Bluetooth devices as they are discovered as well as to restrict the set of devices to those that you actually want to support by your manager implementation. By default `gatt.DeviceManager` discovers and returns all Bluetooth devices but you can restrict that by overriding `gatt.DeviceManager.make_device()`.\n\n`gatt.Device` is the base class for your Bluetooth device. You will need to subclass it to implement the Bluetooth GATT profile of your choice. Override `gatt.Device.services_resolved()` to interact with the GATT profile, i.e. start reading from and writing to characteristics or subscribe to characteristic value change notifications.\n\n### Discovering nearby Bluetooth Low Energy devices\n\nThe SDK entry point is the `DeviceManager` class. Check the following example to dicover any Bluetooth Low Energy device nearby.\n\n```python\nimport gatt\n\nclass AnyDeviceManager(gatt.DeviceManager):\n    def device_discovered(self, device):\n        print(\"Discovered [%s] %s\" % (device.mac_address, device.alias()))\n\nmanager = AnyDeviceManager(adapter_name='hci0')\nmanager.start_discovery()\nmanager.run()\n```\n\nPlease note that communication with your Bluetooth adapter happens over BlueZ's D-Bus API, hence an event loop needs to be run in order to receive all Bluetooth related events. You can start and stop the event loop via `run()` and `stop()` calls to your `DeviceManager` instance.\n\n### Connecting to a Bluetooth Low Energy device and printing all its information\n\nOnce `gatt.DeviceManager` has discovered a Bluetooth device you can use the `gatt.Device` instance that you retrieved from `gatt.DeviceManager.device_discovered()` to connect to it. Alternatively you can create a new instance of `gatt.Device` using the name of your Bluetooth adapter (typically `hci0`) and the device's MAC address.\n\nThe following implementation of `gatt.Device` connects to any Bluetooth device and prints all relevant events:\n\n```python\nimport gatt\n\nmanager = gatt.DeviceManager(adapter_name='hci0')\n\nclass AnyDevice(gatt.Device):\n    def connect_succeeded(self):\n        super().connect_succeeded()\n        print(\"[%s] Connected\" % (self.mac_address))\n\n    def connect_failed(self, error):\n        super().connect_failed(error)\n        print(\"[%s] Connection failed: %s\" % (self.mac_address, str(error)))\n\n    def disconnect_succeeded(self):\n        super().disconnect_succeeded()\n        print(\"[%s] Disconnected\" % (self.mac_address))\n\n    def services_resolved(self):\n        super().services_resolved()\n\n        print(\"[%s] Resolved services\" % (self.mac_address))\n        for service in self.services:\n            print(\"[%s]  Service [%s]\" % (self.mac_address, service.uuid))\n            for characteristic in service.characteristics:\n                print(\"[%s]    Characteristic [%s]\" % (self.mac_address, characteristic.uuid))\n\n\ndevice = AnyDevice(mac_address='AA:BB:CC:DD:EE:FF', manager=manager)\ndevice.connect()\n\nmanager.run()\n```\n\nAs with device discovery, remember to start the Bluetooth event loop with `gatt.DeviceManager.run()`.\n\n### Reading and writing characteristic values\n\nAs soon as `gatt.Device.services_resolved()` has been called by the SDK, you can access all GATT services and characteristics. Services are stored in the `services` attribute of `gatt.Device` and each `gatt.Service` instance has a `characteristics` attribute.\n\nTo read a characteristic value first get the characteristic and then call `read_value()`. `gatt.Device.characteristic_value_updated()` will be called when the value has been retrieved.\n\nThe following example reads the device's firmware version after all services and characteristics have been resolved:\n\n```python\nimport gatt\n\nmanager = gatt.DeviceManager(adapter_name='hci0')\n\nclass AnyDevice(gatt.Device):\n    def services_resolved(self):\n        super().services_resolved()\n\n        device_information_service = next(\n            s for s in self.services\n            if s.uuid == '0000180a-0000-1000-8000-00805f9b34fb')\n\n        firmware_version_characteristic = next(\n            c for c in device_information_service.characteristics\n            if c.uuid == '00002a26-0000-1000-8000-00805f9b34fb')\n\n        firmware_version_characteristic.read_value()\n\n    def characteristic_value_updated(self, characteristic, value):\n        print(\"Firmware version:\", value.decode(\"utf-8\"))\n\n\ndevice = AnyDevice(mac_address='AA:BB:CC:DD:EE:FF', manager=manager)\ndevice.connect()\n\nmanager.run()\n```\n\nTo write a characteristic value simply call `write_value(value)` on the characteristic with `value` being an array of bytes. Then `characteristic_write_value_succeeded()` or `characteristic_write_value_failed(error)` will be called on your `gatt.Device` instance.\n\n### Subscribing for characteristic value changes\n\nTo subscribe for characteristic value change notifications call `enable_notifications()` on the characteristic. Then, on your `gatt.Device` instance, `characteristic_enable_notification_succeeded()` or `characteristic_enable_notification_failed()` will be called. Every time the Bluetooth device sends a new value, `characteristic_value_updated()` will be called.\n\n## Support\n\nPlease [open an issue](https://github.com/getsenic/gatt-python/issues) for this repository.\n\n## Contributing\n\nContributions are welcome via pull requests. Please open an issue first in case you want to discus your possible improvements to this SDK.\n\n## License\n\nThe GATT SDK for Python is available under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgetsenic%2Fgatt-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgetsenic%2Fgatt-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgetsenic%2Fgatt-python/lists"}