无法使用 python 从 BLE 设备读取特定特征
Posted
技术标签:
【中文标题】无法使用 python 从 BLE 设备读取特定特征【英文标题】:Unable to Readspecific characteristic from BLE Device with python 【发布时间】:2021-12-21 10:13:51 【问题描述】:当我使用 python 扫描所有可用特征时,我得到:
INFO:__main__:[Service] 00001801-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Attribute
Profile
INFO:__main__: [Characteristic] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 2):
(indicate), Value: None
INFO:__main__: [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 4): Client
Characteristic Configuration) | Value: b'\x02\x00'
INFO:__main__:[Service] 00001800-0000-1000-8000-00805f9b34fb (Handle: 5): Generic Access
Profile
INFO:__main__: [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb (Handle: 6):
(read,write-without-response,write,authenticated-signed-writes), Value: b'HRSTM'
INFO:__main__: [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb (Handle: 8):
(read,write-without-response,write,authenticated-signed-writes), Value: b'@\x03'
INFO:__main__: [Characteristic] 00002a04-0000-1000-8000-00805f9b34fb (Handle: 10): (read),
Value: b'\xff\xff\xff\xff\x00\x00\xff\xff'
问题是我无法读取具有(指示)权限的特征。我可以从上面读取任何具有读取权限的特征。 为什么我不能阅读那些有指示权限的?
当我使用手机并连接到 BLE 时,我可以看到任何特征的值,即使是那些我无法通过电脑读取的特征。 (例如特征为句柄:1。
这是我的 Python 代码:
import sys
import platform
import asyncio
import logging
from bleak import BleakClient
logger = logging.getLogger(__name__)
UUID = "00002a04-0000-1000-8000-00805f9b34fb"
ADDRESS = (
"00:80:E1:26:C4:5E"
if platform.system() != "Darwin"
else "B9EA5233-37EF-4DD6-87A8-2A875E821C46"
)
async def main(address):
async with BleakClient(address) as client:
while True :
value = bytes(await client.read_gatt_char(UUID));
await asyncio.sleep(1, 0);
print(value)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(main(sys.argv[1] if len(sys.argv) == 2 else ADDRESS))
这是我在运行上面的代码时遇到的错误:
bleak.exc.BleakError: Could not read characteristic handle 2: Protocol Error 0x02: Read Not
Permitted
【问题讨论】:
你想看特征00002a05-0000-1000-8000-00805f9b34fb
?这就是服务改变特性。您的日志表明它只有指示权限,不允许读取或写入。为什么要读它?
感谢您的回复,我通过启用通知事件解决了问题,这就是为什么我收到错误,因为没有来自客户端的权限
这正是我试图暗示你的。恭喜你弄明白了!也许您想回答自己的问题并提供您的固定代码供其他人查找?
当然,现在就这样做。谢谢
【参考方案1】:
解决方案:
我发现我需要创建一些通知处理程序来允许我从选择的特征中读取日期
这是执行此操作的代码:
def notification_handler(sender, data):
"""Simple notification handler which prints the data received."""
output_numbers = list(data)
print(output_numbers)
async def main(address):
async with BleakClient(address) as client:
await client.start_notify(UUID, notification_handler)
await asyncio.sleep(10.0)
await client.stop_notify(UUID)
【讨论】:
以上是关于无法使用 python 从 BLE 设备读取特定特征的主要内容,如果未能解决你的问题,请参考以下文章
我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt)
特定特征的 BLE ReadValue 引发“资源不足错误”
samsung ble api 无法从多个 GATT 特征中获取通知