Files
admin 2840e82685 Initial commit: Android GlassMouse + iOS GlassRemote 智能眼镜遥控项目
包含本次会话修复:
- iOS clickReady 警告 bug(默认 false 永远显示)
- BLE 'handle is invalid' 排查(stale handle auto-retry 实验 + 回退)

项目结构:
- glass-mouse/: 眼镜端原生 Kotlin App,前台服务 MouseService + 自定义 BLE GATT 桥接 ProxyBleServer + 无障碍 GestureDescription 注入
- ios/GlassRemote/: iPhone 端 SwiftUI + CoreBluetooth,通过 BLE 直连眼镜
- HTTP 投屏服务(:8000) 接 PPT/WPS 备注宏 POST 推送

技术栈: Android Kotlin (gradle 8.14.4, compileSdk 34), iOS Swift 5.10 (Xcode 26.5)
2026-08-26 10:34:23 +08:00

566 lines
22 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
INMO AIR3 控制通道 BLE 探测 / 发送脚本(在用户自有 Mac 上跑,需 bleak)
前置: pip install bleak
用法(所有子命令都可在最后加一个 UUID 直连,跳过扫描):
python3 ble_cmd.py bind [UUID] # 发 BleConnectBind 查询/解绑的多种包裹,监听回传(确定包裹格式+是否绑定)
python3 ble_cmd.py mode [UUID] # 用多种 aa55 包裹假设测『进入鼠标模式+点击』,你盯光标/点击(最快定位包裹格式)
python3 ble_cmd.py selftest [UUID] # 发每帧后听回传,自动判定哪帧被眼镜接受(需系统蓝牙先配对)
python3 ble_cmd.py watch [UUID] [秒] # 连上后只订阅通知,打印眼镜自发数据(看协议线索)
python3 ble_cmd.py send <hex> [UUID] [-q] # 往 0x2021 写任意 hex(如 aa55001700010000030100071011a201024200e154)
python3 ble_cmd.py click [UUID] [-q] # 发 mouse_click(裸 protobuf,inmo_proto)
python3 ble_cmd.py move [UUID] [-q] # 发 mouse_move(裸 protobuf)
python3 ble_cmd.py home [UUID] [-q] # 发 HOME 键(裸 protobuf)
说明:
- 控制服务 = 00002020…,写特征 = 00002021…,通知 = 00002022/00002023。
- GATT 已确认开放(无加密/配对),直接连。
- 已知 0x2021 的 read 返回 aa55 帧,但命令精确格式尚未逆向,故 probe 盲测。
- 若设备已被系统蓝牙“连接/配对”,它不再广播,扫描会找不到;此时用之前探索得到的
CoreBluetooth UUID 直连(形如 04A97452-3FC8-6688-807B-D43EF13CE803),或在系统蓝牙里
“忽略此设备”后再扫。
"""
import asyncio
import sys
import re
import binascii
try:
from bleak import BleakScanner, BleakClient
except ImportError:
sys.exit("请先安装 bleak: pip install bleak")
try:
from inmo_proto import mouse_move, mouse_click, key_press, switch_mouse_mode, KEY_HOME
HAVE_PROTO = True
except Exception:
HAVE_PROTO = False
SVC = "00002020"
WRITE = "00002021"
NOTIFY = ["00002022", "00002023"]
# -q / --no-notify:只发指令、不订阅通知(通知通道需配对,且发指令本不需要它)
NO_NOTIFY = False
# 真机枚举时 0x2021 读到的合法帧(直接回发看反应)
OBSERVED_FRAME = bytes.fromhex("aa55001700010000030100071011a201024200e154")
# === 依据 ControlCommand.proto 重写的【正确】命令字节(之前 inmo_proto.switch_mouse_mode 是错的) ===
# 进入鼠标模式:ControlCommand { type=MOUSE_CTRL(23) } → field1 varint=23
MOUSE_MODE_ENTER = bytes([0x08, 0x17])
# 鼠标点击 DOWN / UP(屏幕中心 0.5,0.5):
# ControlCommand { type=MOUSE_CTRL(23), mouse_event{ action, x=0.5f, y=0.5f } }
# field1=08 17 ; field16(0x82 01 len) ; action(08 00/01) ; x(15 0000003f) ; y(1d 0000003f)
CLICK_DOWN = bytes([0x08,0x17, 0x82,0x01,0x0c, 0x08,0x00, 0x15,0x00,0x00,0x00,0x3f, 0x1d,0x00,0x00,0x00,0x3f])
CLICK_UP = bytes([0x08,0x17, 0x82,0x01,0x0c, 0x08,0x01, 0x15,0x00,0x00,0x00,0x3f, 0x1d,0x00,0x00,0x00,0x3f])
# === 依据 BleConnectBind.proto 的绑定/查询命令字节 ===
# 查询绑定状态:BleConnectBind { msg_type=BLECONNECT_QUERY_BIND_STATE(0) } → field1 varint=0
BIND_QUERY = bytes([0x08, 0x00])
# 解绑:BleConnectBind { msg_type=BLECONNECT_UNBIND(6) } → field1 varint=6(userAccount 可选)
BIND_UNBIND = bytes([0x08, 0x06])
def _wrappers(p: bytes):
"""对某个 protobuf 载荷 p 生成多种 aa55 包裹假设,逐一实测哪种被眼镜接受。"""
w = []
w.append(("raw(无包裹)", p))
w.append(("aa55+crc8_0x8C(payload)", b"\xaa\x55" + p + bytes([_crc8(p, 0x8C)])))
w.append(("aa55+sum(payload)", b"\xaa\x55" + p + bytes([(sum(p)) & 0xff])))
w.append(("aa55+comp(payload)", b"\xaa\x55" + p + bytes([(0x100 - sum(p)) & 0xff])))
w.append(("aa55+crc8_0x8C(aa55+payload)", b"\xaa\x55" + p + bytes([_crc8(b"\xaa\x55" + p, 0x8C)])))
w.append(("aa55+len+crc8(payload)", b"\xaa\x55" + bytes([len(p)]) + p + bytes([_crc8(p, 0x8C)])))
w.append(("aa55+00+len+crc8(all)", b"\xaa\x55" + bytes([0x00, len(p)]) + p + bytes([_crc8(bytes([0x00, len(p)]) + p, 0x8C)])))
return w
def _name(d):
return getattr(d, "name", None) or "(无名称)"
def _rssi(d):
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"]
adv = getattr(d, "advertisement_data", None)
if adv is not None and getattr(adv, "rssi", None) is not None:
return adv.rssi
return "?"
def _clean_args():
# 剔除漏进来的 shell 注释(# 开头)和空串
out = []
for a in sys.argv[1:]:
if a.startswith("#"):
continue
if a.strip():
out.append(a)
return out
_UUID_RE = re.compile(r"^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$")
_MAC_RE = re.compile(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$")
def _extract_uuid(args):
for a in args:
if _UUID_RE.match(a) or _MAC_RE.match(a):
return a
return None
async def _connect(mac=None):
if mac:
print(f"直接连: {mac}")
client = BleakClient(mac)
try:
await client.connect(timeout=15)
except Exception as e:
print("连接失败:", e)
print("提示:若设备已被系统蓝牙占用,先在系统蓝牙『忽略此设备』并重启眼镜蓝牙,再试。")
return None
print("已连接:", client.is_connected)
return client
print("扫描 8s 找 INMO AIR3 ...")
devs = await BleakScanner.discover(timeout=8)
target = None
for d in devs:
if "INMO" in _name(d).upper():
target = d.address
print(f"找到: {target} {_name(d)} RSSI={_rssi(d)}")
break
if not target:
print("没扫到 INMO AIR3。可能:①已用系统蓝牙连上→不再广播;②不在范围内。")
if devs:
print("附近设备:")
for d in devs:
print(f" {d.address} {_name(d)} RSSI={_rssi(d)}")
print("→ 用之前探索得到的 CoreBluetooth UUID 直连,例如:")
print(" python3 ble_cmd.py probe 04A97452-3FC8-6688-807B-D43EF13CE803")
return None
client = BleakClient(target)
await client.connect()
print("已连接:", client.is_connected)
return client
def _match_char(client, needle):
for svc in client.services:
for ch in svc.characteristics:
if needle in ch.uuid.lower():
return ch.uuid
return None
async def _notify_loop(client, secs):
got = []
if NO_NOTIFY:
return got
def cb(uuid, data):
line = data.hex()
got.append(line)
print(f" [notify {uuid.split('-')[0]}] {line}")
subs = []
for n in NOTIFY:
u = _match_char(client, n)
if not u:
continue
try:
await client.start_notify(u, cb)
subs.append(u)
print("订阅通知:", u)
except Exception as e:
print(f"订阅 {u} 失败: {e}")
print(" → 通知(回传)通道需要配对/加密。请先在系统蓝牙对该设备『配对/绑定』后再试读回传;")
print(" 控件写入不受影响,可继续观察眼镜对命令的反应。")
if not subs:
print("(未订阅到任何通知,跳过监听。不影响向眼镜发送命令)")
await asyncio.sleep(min(secs, 2))
return got
await asyncio.sleep(secs)
for u in subs:
try:
await client.stop_notify(u)
except Exception:
pass
return got
async def _write(client, data: bytes):
wu = _match_char(client, WRITE)
if not wu:
print("找不到 0x2021 写特征!")
return False
try:
await client.write_gatt_char(wu, bytes(data), response=False)
print(f" → 写 0x2021 ({len(data)}B): {bytes(data).hex()}")
return True
except Exception as e:
print(" 写失败:", e)
return False
def _aa55(payload: bytes, type_byte: int = 0x00, checksum: str = "comp") -> bytes:
body = bytes([type_byte, len(payload)]) + payload
if checksum == "sum":
chk = sum(body) & 0xff
elif checksum == "xor":
chk = 0
for b in body:
chk ^= b
else: # comp
chk = (0x100 - sum(body)) & 0xff
return b"\xaa\x55" + body + bytes([chk])
def _crc8(data: bytes, poly: int = 0x8C) -> int:
# _crack.py 离线发现:真机帧末字节疑似 CRC8(poly=0x8C)。单样本未确证,仅作候选测试。
crc = 0
for b in data:
crc ^= b
for _ in range(8):
crc = ((crc << 1) ^ poly) & 0xff if (crc & 0x80) else (crc << 1) & 0xff
return crc
def _build_candidates():
"""返回 [(标签, bytes), ...] 候选帧,供 probe / selftest 共用。"""
c = []
if HAVE_PROTO:
c.append(("1 裸protobuf switch_mouse_mode", switch_mouse_mode()))
for i, pb in enumerate(mouse_click(0.5, 0.5)):
c.append((f"2 裸protobuf click {'DOWN' if i == 0 else 'UP'}", pb))
c.append(("3 aa55裹protobuf(type00,comp)", _aa55(mouse_click(0.5, 0.5)[0], 0x00, "comp")))
c.append(("4 aa55裹protobuf(type01,sum)", _aa55(mouse_click(0.5, 0.5)[0], 0x01, "sum")))
c.append(("6 aa55+裸pb click+crc8(0x8c)", b"\xaa\x55" + mouse_click(0.5, 0.5)[0] + bytes([_crc8(mouse_click(0.5, 0.5)[0], 0x8C)])))
c.append(("7 aa55+裸pb switch+crc8(0x8c)", b"\xaa\x55" + switch_mouse_mode() + bytes([_crc8(switch_mouse_mode(), 0x8C)])))
c.append(("8 aa55+裸pb move+crc8(0x8c)", b"\xaa\x55" + mouse_move(0.5, 0.5) + bytes([_crc8(mouse_move(0.5, 0.5), 0x8C)])))
c.append(("5 真机读到的合法帧原样", OBSERVED_FRAME))
return c
async def watch(secs=20, mac=None):
client = await _connect(mac)
if not client:
return
try:
await _notify_loop(client, secs)
finally:
await client.disconnect()
async def send_hex(hexstr: str, mac=None):
try:
data = binascii.unhexlify(hexstr.replace(" ", "").replace("-", ""))
except binascii.Error:
print(f"hex 非法(需偶数长度、仅 0-9a-f): {hexstr!r}")
print("示例: python3 ble_cmd.py send aa55001700010000030100071011a201024200e154")
return
if len(data) == 0:
print("hex 为空")
return
client = await _connect(mac)
if not client:
return
try:
await _write(client, data)
await _notify_loop(client, 5)
finally:
await client.disconnect()
async def raw_proto(kind: str, mac=None):
if not HAVE_PROTO:
print("inmo_proto 不可用,跳过裸 protobuf 候选")
return
if kind == "click":
cmds = mouse_click(0.5, 0.5)
elif kind == "move":
cmds = [mouse_move(0.5, 0.5)]
elif kind == "home":
cmds = key_press(KEY_HOME)
else:
cmds = [switch_mouse_mode()]
client = await _connect(mac)
if not client:
return
try:
for c in cmds:
await _write(client, c)
await asyncio.sleep(0.1)
print("(已发裸 protobuf,观察眼镜是否反应)")
await _notify_loop(client, 5)
finally:
await client.disconnect()
async def mode_probe(mac=None):
"""用正确的『进入鼠标模式』命令 + 多种 aa55 包裹假设实测,找出眼镜接受的包裹格式。
每组发送:先进入鼠标模式,再同包裹发一次点击。用户紧盯眼镜,记录【第一个】有反应的组号。"""
client = await _connect(mac)
if not client:
return
try:
print("\n== 依次用各种包裹假设发送『进入鼠标模式 + 点击』 ==")
print(" 请紧盯眼镜:每发一组后看【光标是否出现】、是否发生【点击/tap】。")
print(" 记下【第一个】有反应的组号(W1..W7)告诉我即可;没反应就试下一组。\n")
for idx, (wname, wrapped) in enumerate(_wrappers(MOUSE_MODE_ENTER), 1):
print(f">>> [W{idx}] 包裹={wname} 先进入鼠标模式")
await _write(client, wrapped)
await asyncio.sleep(1.0)
cn1 = _wrappers(CLICK_DOWN)[idx - 1][1]
cn2 = _wrappers(CLICK_UP)[idx - 1][1]
print(f" 同包裹发点击(屏幕中心) ...")
await _write(client, cn1)
await asyncio.sleep(0.08)
await _write(client, cn2)
await asyncio.sleep(1.3)
print("\n哪一组(W1..W7)让眼镜出现光标/点击?把编号告诉我(没反应就说『全无』)。")
finally:
await client.disconnect()
async def probe(mac=None):
client = await _connect(mac)
if not client:
return
try:
print("\n== 接下来依次发送候选命令,请紧盯眼镜屏幕观察 ==")
print(" 看:是否出现/移动蓝色光标、是否发生“点击”、是否进入鼠标模式。")
print(" 每组发送后会停 1 秒,发送期间即可观察,不必等结尾。\n")
for label, data in _build_candidates():
print(f"--- 候选 {label} ---")
await _write(client, data)
await asyncio.sleep(1)
print("\n观察眼镜:哪一下让光标动了/点了?把结果告诉我。继续监听 6s 通知...")
await _notify_loop(client, 6)
finally:
await client.disconnect()
async def selftest(mac=None):
"""自动判定:发每帧后听回传(notify),眼镜对“正确格式”的帧通常会回传状态/ACK。
前提:系统蓝牙已对该设备『配对/绑定』(否则 notify 会被拒、无法判定)。
用户只需跑一条命令并贴回输出,不必肉眼盯屏幕。"""
client = await _connect(mac)
if not client:
return
try:
got = []
loop = asyncio.get_event_loop()
def cb(uuid, data):
got.append((loop.time(), data.hex()))
print(f" [notify {uuid.split('-')[0]}] {data.hex()}")
subs = []
if not NO_NOTIFY:
for n in NOTIFY:
u = _match_char(client, n)
if not u:
continue
try:
await client.start_notify(u, cb)
subs.append(u)
print("订阅通知:", u)
except Exception as e:
print(f"订阅 {u} 失败(需先在系统蓝牙配对): {e}")
if not subs:
print("⚠️ 未订阅到通知:无法自动判定。请先在系统蓝牙『配对』INMO,再跑 selftest;")
print(" 或改用 `probe <UUID> -q` 肉眼观察哪组让眼镜有反应。")
elif NO_NOTIFY:
print("(已 -q,跳过通知订阅,selftest 无意义;去掉 -q 重跑)")
await client.disconnect()
return
cands = _build_candidates()
print(f"\n== 依次发送 {len(cands)} 个候选并检测回传 ==")
results = []
for label, data in cands:
before = len(got)
await _write(client, data)
await asyncio.sleep(0.9)
n_resp = len(got) - before
mark = "✅ 有回传" if n_resp > 0 else "— 无回传"
print(f" [{label}] {mark} ({n_resp} 条)")
results.append((label, n_resp))
for u in subs:
try:
await client.stop_notify(u)
except Exception:
pass
print("\n== 汇总:获得回传的候选(格式很可能正确)==")
hits = [r for r in results if r[1] > 0]
if hits:
for label, n in hits:
print(f" ✅ {label} ({n} 条回传)")
else:
print(" 无候选获得回传。可能:未配对 / 眼镜不回传 / 格式全错。")
print(" 建议:①系统蓝牙配对后重跑;②或直接 `probe <UUID> -q` 肉眼看哪组让眼镜反应。")
finally:
await client.disconnect()
def _try_parse_bind_status(got_hex):
"""从回传 hex 列表里尝试解析 BleBindStatusResponse 的 is_bound。启发式,失败则原样返回。"""
out = []
for h in got_hex:
b = bytes.fromhex(h)
i = b.find(b"\x08\x03") # BLECONNECT_BIND_STATUS_RESPONSE=3
if i < 0:
out.append((h, "(非 bind_status 帧,含 " + str(len(b)) + " 字节)"))
continue
sub = b[i:]
j00 = sub.find(b"\x08\x00")
j01 = sub.find(b"\x08\x01")
# 取 08 00/01 中属于 is_bound 的(紧跟 response tag 后、在 bound_user_id/error_code 之前)
k = -1
if j01 >= 0 and (j00 < 0 or j01 < j00):
k = 1
elif j00 >= 0:
k = 0
if k == 1:
out.append((h, "is_bound=1(已绑定其他账号!陌生设备命令会被拒)"))
elif k == 0:
out.append((h, "is_bound=0(未绑定,陌生设备应可直接控制)"))
else:
out.append((h, "bind_status 响应,但无法解析 is_bound(贴回我分析)"))
return out
async def bind_probe(mac=None):
"""探测业务层绑定:发 BleConnectBind.QUERY_BIND_STATE / UNBIND 的多种包裹,监听回传。
价值(一箭双雕):
- BLECONNECT_QUERY_BIND_STATE 是只读握手,眼镜【必须】回传状态 → 用哪个包裹收到回传,
哪个包裹格式就正确(间接锁定 ControlCommand 的包裹格式)。
- 回传里的 is_bound 告诉我们眼镜是否已绑、绑了谁:若已绑陌生账号,发命令必被拒,
需先 UNBIND 解绑。
前提:系统蓝牙已对该设备『配对』(否则 notify 被拒、读不到回传)。"""
client = await _connect(mac)
if not client:
return
try:
got = []
loop = asyncio.get_event_loop()
def cb(uuid, data):
got.append(data.hex())
print(f" [notify {uuid.split('-')[0]}] {data.hex()}")
subs = []
if not NO_NOTIFY:
for n in NOTIFY:
u = _match_char(client, n)
if u:
try:
await client.start_notify(u, cb)
subs.append(u)
print("订阅通知:", u)
except Exception as e:
print(f"订阅 {u} 失败(需先在系统蓝牙配对): {e}")
if not subs:
print("⚠️ 未订阅到通知:无法读回传。请先在系统蓝牙『配对』INMO,再跑 bind。")
print("\n== 1) 发送 BleConnectBind.QUERY_BIND_STATE(msg_type=0)监听回传 ==")
print(" (这是只读握手,眼镜若认得包裹格式【必】回传绑定状态)\n")
q_hits = []
for idx, (wname, wrapped) in enumerate(_wrappers(BIND_QUERY), 1):
before = len(got)
print(f">>> [Q{idx}] 包裹={wname}")
await _write(client, wrapped)
await asyncio.sleep(0.8)
if len(got) > before:
q_hits.append((idx, wname, got[-1]))
print(f" ✅ 收到回传!此包裹格式正确 → {got[-1]}")
if not q_hits:
print("\n❌ QUERY 全部包裹都无回传。可能:包裹格式全错,或眼镜对未授权设备静默丢弃。")
print("\n== 2) 发送 BleConnectBind.UNBIND(msg_type=6)监听回传 ==")
u_hits = []
for idx, (wname, wrapped) in enumerate(_wrappers(BIND_UNBIND), 1):
before = len(got)
print(f">>> [U{idx}] 包裹={wname}")
await _write(client, wrapped)
await asyncio.sleep(0.8)
if len(got) > before:
u_hits.append((idx, wname, got[-1]))
print(f" ✅ 收到回传!→ {got[-1]}")
for u in subs:
try:
await client.stop_notify(u)
except Exception:
pass
print("\n== 汇总 ==")
print(f" QUERY 命中包裹: {[f'Q{q[0]}({q[1]})' for q in q_hits] or '无'}")
print(f" UNBIND 命中包裹: {[f'U{q[0]}({q[1]})' for q in u_hits] or '无'}")
if got:
print(" 回传帧解析:")
for h, info in _try_parse_bind_status(got):
print(f" {info}\n {h}")
print("\n如何判读:")
print(" - 若 Q 命中 → 该包裹格式正确!告诉我编号,我立刻用同包裹重写 ControlCommand 发送逻辑发 mouse。")
print(" - 回传显示 is_bound=1 → 眼镜已绑其他账号,陌生设备命令被拒;需先成功 UNBIND(看 U 是否命中/回传)。")
print(" - 若 Q/U 全无回传 → 包裹格式全错或绑定需加密 userAccount(dex 有 HMAC-SHA256 线索),BLE 直连成本陡增。")
finally:
await client.disconnect()
def main():
global NO_NOTIFY
args = []
for a in _clean_args():
if a in ("-q", "--no-notify"):
NO_NOTIFY = True
else:
args.append(a)
if not args:
print(__doc__)
return
sub = args[0]
rest = args[1:]
mac = _extract_uuid(rest)
if mac:
rest = [a for a in rest if a != mac]
if sub == "watch":
secs = 20
for a in rest:
if a.isdigit():
secs = int(a)
asyncio.run(watch(secs, mac))
elif sub == "send":
if not rest:
print("用法: python3 ble_cmd.py send <hex> [UUID]")
return
asyncio.run(send_hex(rest[0], mac))
elif sub == "mode":
asyncio.run(mode_probe(mac))
elif sub == "bind":
asyncio.run(bind_probe(mac))
elif sub in ("click", "move", "home"):
asyncio.run(raw_proto(sub, mac))
elif sub == "probe":
asyncio.run(probe(mac))
elif sub == "selftest":
asyncio.run(selftest(mac))
else:
print(__doc__)
if __name__ == "__main__":
main()