【问题标题】:Read and notification issues with gattlib BLE?gattlib BLE 的读取和通知问题?
【发布时间】:2021-08-03 21:37:21
【问题描述】:

我正在编写一个 Linux 应用程序,使用 python3 中的 gattlib 库在 BlueSnap DB9 BLE 适配器和我的 Linux 设备之间发送和接收用户输入的数据。我已经能够从我的设备成功地向适配器发送String 的数据,并在适配器的终端上看到了输出,但是我在从适配器接收数据时遇到了问题。

我正在按照这个示例使用gattlib library 读取和写入数据。我可以使用write_cmdwrite_by_handle 函数写入数据,但我无法使用此处提到的任何读取函数通过gattlib 读取数据或启用通知。使用 gattlib 时似乎没有启用通知,因为我重写的 on_notification 函数没有打印出我在那里添加的打印语句。

我已经确定写入和读取数据的句柄分别是0x00430x0046。以下是 serialio 提供给我的用于写入和读取的 UUID:UUIDs

当使用bluetoothctl时,选择特性后,我可以向适配器写入数据。只有在bluetoothctl 上启用通知后,我才能读取数据。一旦我禁用通知,尝试手动读取会打印出全 0,而不是我想要读取的数据。在 python3 中使用gattlib 选择特征并启用通知的正确方法是什么?

更新:我能够启用通知。我在bluetoothctl 和我的python 代码上运行hcidump,并确定我用于启用通知的句柄不正确。启用通知的正确句柄是0x0047。意识到这个错误后,我使用正确的句柄运行enable_notifications,并将两个参数都设置为True,当我在适配器的终端上键入它时,我能够启用通知并在我的设备终端上查看传入数据。

【问题讨论】:

  • 有一个如何使用gattlib接收通知的例子:github.com/oscaracena/pygattlib/blob/master/examples/…
  • 我运行了这个例子,但我不清楚它应该如何工作,因为在运行这个例子时,它只指定了一个 MAC 地址,而不是你可以启用通知的特定特征。就像我对bluetoothctl 所说的那样,我必须先选择一个特征并为该特征启用通知,然后才能接收任何数据。我试图模仿我在bluetoothctl 上所做的事情,但在 python3 应用程序上。

标签: python-3.x bluetooth-lowenergy bluesnap gattlib


【解决方案1】:

不使用gattlib,但这里有一个使用 Python3 D-Bus 绑定和 GLib 事件循环来读取、写入和从 GATT 特征中获取通知的示例。

from time import sleep
import pydbus
from gi.repository import GLib

# Setup of device specific values
dev_id = 'DE:82:35:E7:43:BE'
adapter_path = '/org/bluez/hci0'
device_path = f"{adapter_path}/dev_{dev_id.replace(':', '_')}"
temp_reading_uuid = 'e95d9250-251d-470a-a062-fa1922dfa9a8'
temp_period_uuid = 'e95d1b25-251d-470a-a062-fa1922dfa9a8'

# Setup DBus informaton for adapter and remote device
bus = pydbus.SystemBus()
mngr = bus.get('org.bluez', '/')
adapter = bus.get('org.bluez', adapter_path)
device = bus.get('org.bluez', device_path)
# Connect to device (needs to have already been paired via bluetoothctl)
device.Connect()

# wait for GATT services to be discovered
while not device.ServicesResolved:
    sleep(0.5)

# Some helper functions
def get_characteristic_path(device_path, uuid):
    """Find DBus path for UUID on a device"""
    mng_objs = mngr.GetManagedObjects()
    for path in mng_objs:
        chr_uuid = mng_objs[path].get('org.bluez.GattCharacteristic1', {}).get('UUID')
        if path.startswith(device_path) and chr_uuid == uuid:
           return path

def as_int(value):
    """Create integer from bytes"""
    return int.from_bytes(value, byteorder='little')

# Get a couple of characteristics on the device we are connected to
temp_reading_path = get_characteristic_path(device._path, temp_reading_uuid)
temp_period_path = get_characteristic_path(device._path, temp_period_uuid)
temp = bus.get('org.bluez', temp_reading_path)
period = bus.get('org.bluez', temp_period_path)
# Read value of characteristics
print(temp.ReadValue({}))
# [0]
print(period.ReadValue({}))
# [232, 3]
print(as_int(period.ReadValue({})))
# 1000

# Write a new value to one of the characteristics
new_value = int(1500).to_bytes(2, byteorder='little')
period.WriteValue(new_value, {})

# Enable eventloop for notifications
def temp_handler(iface, prop_changed, prop_removed):
    """Notify event handler for temperature"""
    if 'Value' in prop_changed:
        print(f"Temp value: {as_int(prop_changed['Value'])} \u00B0C")

mainloop = GLib.MainLoop()
temp.onPropertiesChanged = temp_handler
temp.StartNotify()
try:
    mainloop.run()
except KeyboardInterrupt:
    mainloop.quit()
    temp.StopNotify()
    device.Disconnect()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2020-12-08
    • 2013-12-02
    相关资源
    最近更新 更多