# -*- coding: utf-8 -*- """ INMO 眼镜 BLE 探测脚本(在用户自有 Mac 上运行,不要在沙箱里跑) 前置: pip install bleak 用法: python3 ble_scan.py scan # 扫描 8 秒,列出附近 BLE 设备 + 广播 UUID python3 ble_scan.py explore # 自动按名字找 INMO AIR3 并枚举 GATT(推荐) python3 ble_scan.py explore # 指定 MAC 枚举 GATT 服务/特征及属性(read/write/notify) 目的:确认 ble_protocol.md 里那三道坎 —— 1. 哪个 UUID 是 service / WRITE 特征 / READ(NOTIFY) 特征 2. 连接/枚举时是否要求配对或返回加密错误(dex 有 HMAC-SHA256 线索) 3. 写指令后眼镜是否反应(这一步在 nRF Connect 里手动测更直观,见 ble_probe_guide.md) """ import asyncio import sys from bleak import BleakScanner, BleakClient def _name(d): return getattr(d, "name", None) or "(无名称)" def _rssi(d): # 老版本 bleak: d.rssi # 新版本(0.19+): 挪进 d.metadata['rssi'] if hasattr(d, "rssi") and d.rssi is not None: return d.rssi md = getattr(d, "metadata", None) or {} if isinstance(md, dict) and md.get("rssi") is not None: return md["rssi"] # 极少数版本放在 advertisement_data 上 adv = getattr(d, "advertisement_data", None) if adv is not None and getattr(adv, "rssi", None) is not None: return adv.rssi return "?" def _uuids(d): md = getattr(d, "metadata", None) or {} if isinstance(md, dict) and md.get("uuids"): return md["uuids"] adv = getattr(d, "advertisement_data", None) if adv is not None and getattr(adv, "service_uuids", None): return adv.service_uuids return [] async def scan(): print("== 扫描 8 秒附近 BLE 设备 ==") devs = await BleakScanner.discover(timeout=8) if not devs: print("没扫到任何设备。确认:眼镜已开机 + 蓝牙可发现;Mac 蓝牙已开;脚本用的是真机不是沙箱。") return for d in devs: print(f"{d.address} {_name(d)} RSSI={_rssi(d)}") uuids = _uuids(d) if uuids: print(f" advertised uuids: {uuids}") async def explore(mac: str = None): target = mac if not target: print("== 未给 MAC,自动扫描找 INMO AIR3 ==") devs = await BleakScanner.discover(timeout=8) for d in devs: n = _name(d).upper() if "INMO" in n or "AIR3" in n: target = d.address print(f"找到: {target} {_name(d)} RSSI={_rssi(d)}") break if not target: print("没找到 INMO AIR3。请手动指定 MAC:python3 ble_scan.py explore ") return print(f"== 连接 {target} 并枚举 GATT ==") try: async with BleakClient(target) as client: print("已连接:", client.is_connected) for svc in client.services: print(f"\n[Service] {svc.uuid}") if "e49a25e0" in svc.uuid or "e49a25f8" in svc.uuid or "e49a28e1" in svc.uuid: print(" ^^^ 命中候选 INMO UUID(ble_protocol.md)") for ch in svc.characteristics: props = ",".join(ch.properties) print(f" [Char] {ch.uuid} props={props}") if "read" in ch.properties: try: val = await client.read_gatt_char(ch.uuid) print(f" read -> {val.hex()}") except Exception as e: print(f" read -> 失败: {e}") except Exception as e: print("连接/枚举失败:", e) print("提示:") print(" 1) 若报 'not paired'/'encryption'/'insufficient authentication' —— 说明有配对/加密鉴权,记下。") print(" 2) 若报 'device not found'/'already connected'/'XPC' 类 —— 多半是 macOS 系统已占用该设备,") print(" 请到系统蓝牙里『忽略此设备』后再跑本脚本(枚举 GATT 不需要系统配对)。") def main(): if len(sys.argv) < 2 or sys.argv[1] == "scan": asyncio.run(scan()) elif sys.argv[1] == "explore": mac = sys.argv[2] if len(sys.argv) >= 3 else None asyncio.run(explore(mac)) else: print(__doc__) if __name__ == "__main__": main()