You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows环境下Bleak调用start_notify与write_gatt_char失败求解

Bleak启动Notify报错WinError -2147023673的解决方案

环境信息

  • 操作系统:Windows10
  • Python版本:3.12.0
  • Bleak版本:0.21.1

测试代码

import asyncio
from bleak import BleakClient

def notify_callback(sender: int, data: bytearray):
    print('sender: ', sender, 'data: ', data)

async def connect(address):
    async with BleakClient(address, timeout=5.0) as client:     
        print('connected')
        services = await client.get_services()
        for service in services:
            print('service uuid:', service.uuid)
            for characteristic in service.characteristics:
                print('  uuid:', characteristic.uuid)
                print('  handle:', characteristic.handle) 
                print('  properties: ', characteristic.properties)
                if 'notify' in characteristic.properties:                     
                    print('try to activate notify.')
                    await client.start_notify(characteristic, notify_callback)
        if client.is_connected:
            d = "5303e803ff45"
            data_to_write = bytearray.fromhex(d)
            await client.write_gatt_char(characteristic.uuid,data_to_write,response=True)
        
    print('disconnect')

if __name__ == "__main__":
    asyncio.run(connect(address2))

报错信息

File "C:\Python312\Lib\site-packages\bleak\backends\winrt\client.py", line 974, in start_notify
await winrt_char.write_client_characteristic_configuration_descriptor_async(
OSError: [WinError -2147023673] 用户取消了操作。 -> 该错误持续出现。

排查情况

  • 设备已与Windows蓝牙配对成功
  • 使用Nordic工具可正常完成数据收发,但Bleak库调用时触发错误
  • 尝试UWP方式时,也出现类似unreachable和OSError错误

解决方法

1. 修正特征操作的上下文逻辑

你的代码中遍历所有特征启动Notify后,最后写入操作使用的是循环最后一个特征的UUID,可能并非目标操作特征,导致上下文异常。建议明确指定目标特征UUID,将操作逻辑绑定到对应特征:

async def connect(address):
    # 替换为你实际需要操作的特征UUID
    target_char_uuid = "你的目标特征UUID"
    async with BleakClient(address, timeout=5.0) as client:     
        print('connected')
        services = await client.get_services()
        target_char = None
        
        for service in services:
            for characteristic in service.characteristics:
                print('service uuid:', service.uuid)
                print('  uuid:', characteristic.uuid)
                print('  handle:', characteristic.handle) 
                print('  properties: ', characteristic.properties)
                
                if characteristic.uuid == target_char_uuid:
                    target_char = characteristic
                    if 'notify' in characteristic.properties:                     
                        print('try to activate notify.')
                        await client.start_notify(characteristic, notify_callback)
        
        if client.is_connected and target_char:
            d = "5303e803ff45"
            data_to_write = bytearray.fromhex(d)
            await client.write_gatt_char(target_char.uuid, data_to_write, response=True)
        
    print('disconnect')

2. 增加Notify操作的重试机制

Windows下BLE操作偶尔会出现瞬时异常,给start_notify增加重试逻辑可规避此类问题:

async def safe_start_notify(client, characteristic, callback, retries=3):
    for attempt in range(retries):
        try:
            await client.start_notify(characteristic, callback)
            return True
        except OSError as e:
            if attempt < retries -1:
                await asyncio.sleep(0.5)
            else:
                raise e

在原代码中用此函数替代await client.start_notify(...)即可。

3. 调整Bleak客户端初始化参数

尝试禁用服务缓存或延长连接超时时间,避免因缓存或超时导致的异常:

async with BleakClient(address, timeout=10.0, cache_services=False) as client:
    # 后续业务逻辑

4. 检查Windows蓝牙服务与权限

  • 重启蓝牙服务:按下Win+R输入services.msc,找到Bluetooth Support Service右键重启
  • 确认Python权限:进入Windows设置-隐私和安全性-蓝牙,允许应用访问蓝牙权限

5. 降级Bleak版本

部分新版本Bleak在Windows10下存在兼容性问题,可尝试降级到稳定版本:

pip install bleak==0.20.2

内容的提问来源于stack exchange,提问作者armsosad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 08:05:16