Python 低功耗蓝牙扫描/连接/读写工具,已在 Windows 本机安装
Python 3.12.9 · Windows 11 · 已通过 BLE 扫描验证
0000fee0)pip install bleak — 已安装,免代理需用 HTTP_PROXY="" HTTPS_PROXY=""| 能力 | 状态 | 说明 |
|---|---|---|
| 扫描附近 BLE 设备 | ✅ | 发现设备名称、地址、信号强度、广播服务 UUID |
| 读取设备特征值 | ✅ | 已成功读取华米手环数据 |
| 连接已配对设备 | ✅ | 需先经 Windows 蓝牙配对后使用 |
| 未配对设备直连 | ⚠️ 受限 | BLE 隐私地址轮换太快,扫描到连接之间地址失效 |
| 订阅 notify 实时数据 | 待测试 | 需要已配对的 BLE 设备配合测试 |
pip install bleak
注:本机需设代理环境变量为空 HTTP_PROXY="" HTTPS_PROXY="" 绕过代理超时
python -c "
import asyncio
from bleak import BleakScanner
async def scan():
devices = {}
def cb(d, ad):
if d.address not in devices:
devices[d.address] = (d.name or '(未命名)', ad.rssi if ad else -999)
print(f' [{ad.rssi:>4}] {d.name or chr(40)+chr(26410)+chr(21629)+chr(21517)+chr(41):<35} {d.address}')
s = BleakScanner(detection_callback=cb)
await s.start()
await asyncio.sleep(30)
await s.stop()
print(f'\n发现 {len(devices)} 个设备')
asyncio.run(scan())
"
python -c "
import asyncio
from bleak import BleakClient
async def main():
async with BleakClient('MAC地址', timeout=10.0) as c:
print(f'已连接: {await c.get_device_name()}')
for svc in c.services:
for ch in svc.characteristics:
if 'read' in ch.properties:
v = await c.read_gatt_char(ch.uuid)
print(f' {ch.uuid}: {v.hex()[:60]}')
asyncio.run(main())
"
| 文件 | 路径 |
|---|---|
| Python 包 | C:\Users\64998\AppData\Local\Programs\Python\Python312\Lib\site-packages\bleak\ |
| 扫描脚本 | F:\codex\tools\ble-scan\ (待创建) |