1 Star 0 Fork 0

kof98765 / pygatt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

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 (requires Linux), using the gatttool command-line utility.
  • Bluegiga's BGAPI, compatible with USB adapters like the BLED112.

Motivation

Despite the popularity 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 programmatically. BlueZ itself obviously works, but the interface leaves something to be desired and only works in Linux.

Requirements

  • Python 2.7.5 or greater, or Python 3.5 or greater
    • Python 2.7.3's struct library has a bug that will break PyGATT - 2.7.5
      or greater is recommended.
  • BlueZ 5.18 or greater (with gatttool) - required for the gatttool backend only.
    • Tested on 5.18, 5.21, 5.35 and 5.43
  • GATTToolBackend requires Linux (i.e. not Windows compatible)

Installation

Install pygatt with pip from PyPI:

$ pip install 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]"

Install the latest development version of pygatt with pip:

$ pip install git+https://github.com/peplin/pygatt

Documentation

The documentation for pygatt consists of:

  • This README
  • The code in the samples directory
  • The Python docstrings in the code itself.

The BLEDevice and BLEBackend base classes are the primary interfaces for users of the library.

Example Use

The primary API for users of this library is provided by pygatt.BLEBackend and pygatt.BLEDevice. After initializing an instance of the preferred backend (available implementations are found in pygatt.backends, use the BLEBackend.connect method to connect to a device and get an instance of BLEDevice.

import pygatt

# The BGAPI backend will attempt to auto-discover the serial device name of the
# attached BGAPI-compatible USB adapter.
adapter = pygatt.BGAPIBackend()

try:
    adapter.start()
    device = adapter.connect('01:23:45:67:89:ab')
    value = device.char_read("a1e8f5b1-696b-4e4c-87c6-69dfe0b0093b")
finally:
    adapter.stop()

Note that not all backends support connecting to more than 1 device at at time, so calling BLEBackend.connect again may terminate existing connections.

Here's the same example using the GATTTool backend. It's identical except for the initialization of the backend:

import pygatt

adapter = pygatt.GATTToolBackend()

try:
    adapter.start()
    device = adapter.connect('01:23:45:67:89:ab')
    value = device.char_read("a1e8f5b1-696b-4e4c-87c6-69dfe0b0093b")
finally:
    adapter.stop()

Notifications Example

This example uses the gatttool backend to connect to a device with a specific MAC address, subscribes for notifications on a characteristic, and prints the data returned in each notification.

import pygatt
from binascii import hexlify

adapter = pygatt.GATTToolBackend()

def handle_data(handle, value):
    """
    handle -- integer, characteristic read handle the data was received on
    value -- bytearray, the data returned in the notification
    """
    print("Received data: %s" % hexlify(value))

try:
    adapter.start()
    device = adapter.connect('01:23:45:67:89:ab')

    device.subscribe("a1e8f5b1-696b-4e4c-87c6-69dfe0b0093b",
                     callback=handle_data)
finally:
    adapter.stop()

Debugging

While debugging software using pygatt, it is often useful to see what's happening inside the library. You can enable debugging logging and have it printed to your terminal with this code:

import pygatt
import logging

logging.basicConfig()
logging.getLogger('pygatt').setLevel(logging.DEBUG)

Can't find BGAPI device in Windows

You may need to explicitly specify the COM port of your BGAPI-compatible device in windows, e.g.:

adapter = pygatt.BGAPIBackend(serial_port='COM9')

If you provide the COM port name, but still get an error such as WindowsError(2, 'The system cannot find the file specified.'), try changing the COM port of the device to a value under 10, e.g. COM9.

Authors

Releasing to PyPI

For the maintainers of the project, when you want to make a release:

  • Merge all of the changes into master.
  • Update the version in setup.py.
  • Update the CHANGELOG.mkd
  • Tag the commit and push to GitHub (will need to push to a separate branch of PR first since master is a protected branch).
  • Travis CI will take care of the rest - it will build and deploy tagged commits to PyPI automatically.

License

Copyright 2015 Stratos Inc. and Orion Labs

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

pygatt: Copyright 2015 Stratos Inc. Copyright 2015 Orion Labs, Inc. Copyright 2014 Christopher Peplin Copyright 2013 Michael Saunby Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. pygatt.backends.bgapi.bglib: Copyright 2014 Jeff Rowberg BGLib source code libraries are placed under the MIT license Copyright (c) 2014 Jeff Rowberg. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/98765kof/pygatt.git
git@gitee.com:98765kof/pygatt.git
98765kof
pygatt
pygatt
master

搜索帮助