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

Python打包生成的BLE程序exe无法找到BLE设备求助

问题排查与解决方法

1. 修正Windows下BLE设备地址的使用逻辑

Windows平台上,Bleak有时无法通过MAC地址直接定位设备,建议改用设备的蓝牙名称或**Windows专属设备ID(GUID格式)**进行连接。你可以先运行以下代码获取目标设备的正确标识:

from bleak import discover
import asyncio

async def scan_devices():
    devices = await discover()
    for dev in devices:
        print(f"设备名称: {dev.name}, 标识地址: {dev.address}, 元数据: {dev.metadata}")

asyncio.run(scan_devices())

找到目标设备对应的address(可能是一串GUID)或name,替换代码中的ADDRESS变量。

2. 修复PyInstaller打包时的依赖缺失

如果使用PyInstaller打包,需确保Bleak的WinRT后端依赖被完整包含。创建一个bleeeeeak.spec文件,添加以下配置:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(
    ['bleeeeeak.py'],
    pathex=[],
    binaries=[],
    datas=[
        # 包含Bleak的WinRT相关文件
        (r'C:\你的Python路径\Lib\site-packages\bleak\backends\winrt', 'bleak/backends/winrt'),
    ],
    hiddenimports=[
        'bleak.backends.winrt.client',
        'bleak.backends.winrt.utils',
        'winrt.windows.devices.bluetooth',
        'winrt.windows.devices.bluetooth.genericattributeprofile',
        'winrt.windows.devices.enumeration',
    ],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='bleeeeeak',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

替换其中的Python路径后,执行pyinstaller bleeeeeak.spec重新打包。

3. 消除asyncio事件循环冲突

你的代码中同时手动创建事件循环又调用asyncio.run,这会导致打包后运行时的循环异常。修改入口代码:

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    # 移除手动创建循环的冗余代码
    asyncio.run(main(ADDRESS))

4. 检查运行权限

右键点击生成的exe文件,选择以管理员身份运行。Windows下蓝牙设备访问可能需要管理员权限,打包后的程序默认权限可能不足。

5. 确认设备可发现性

确保BLE设备处于可发现模式,且Windows系统蓝牙设置中能正常识别该设备。同时检查设备管理器中的蓝牙驱动状态,确认驱动无异常。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 19:40:36