无法从具有 python 的 BLE 设备读取特定特征

Unable to Readspecific characteristic from BLE Device with python

当我使用 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'

问题是我无法读取具有(指示)权限的特征。我可以从上面读取任何具有读取权限的特征。 为什么我不能阅读那些有指示权限的?

当我使用我的 phone 并连接到 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

解决方案:

我发现我需要创建一些通知处理程序,允许我从选择的特征中读取日期

这是执行此操作的代码:

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)