# -*- coding: utf-8 -*- """ Mac 端 BLE 代理(桥接模式) 思路(与用户和眼镜端约定一致): - 搭车系统已建立的 INMO 连接(已配对 → 链路已加密) - 发现眼镜 App 自定义 GATT 服务 A9E0ABCD-0000-1000-8000-00805F9B34FB - 把和 WebSocket 完全一致的 JSON 命令写进 WRITE 特征 - 眼镜 App(ProxyBleServer)收到后转发给原有控制引擎 MouseService.handleCommand → 蓝牙只做传输,控制逻辑完全复用,绕开 INMO 绑定墙 用法: python3 ble_proxy.py --demo # 自动演示:眼镜显示光标并移动/点击 python3 ble_proxy.py # stdin 手动指令(move/click/home/...) python3 ble_proxy.py --mouse # 捕获 Mac 真实鼠标(需系统设置→输入监控授权) """ import sys, time, binascii, json, threading, os from Foundation import NSObject, NSRunLoop, NSDate, NSData import CoreBluetooth from CoreBluetooth import CBCentralManager, CBUUID # 眼镜端自定义服务(与 ProxyBleServer.kt 完全一致) SVC_UUID = "A9E0ABCD-0000-1000-8000-00805F9B34FB" WRITE_UUID = "A9E0ABCD-0001-1000-8000-00805F9B34FB" NOTIFY_UUID = "A9E0ABCD-0002-1000-8000-00805F9B34FB" # 用 INMO 的 0x2020 服务来「定位」系统里已连接的外设(眼镜本身一直在广播/连接它) LOOKUP_UUID = "00002020-0000-1000-8000-00805F9B34FB" WRITE_WITH_RESPONSE = 0 WRITE_WITHOUT_RESPONSE = 1 class D(NSObject): def centralManagerDidUpdateState_(self, mgr): self.manager = mgr def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(self, mgr, perp, adv, rssi): name = perp.name() # 定向扫描(scanForPeripheralsWithServices_ 带我们的服务 UUID)命中即收; # 泛扫则只在名字含 INMO 时收 if getattr(self, "_targeted_scan", False): if self._scan_result is None: self._scan_result = perp print("扫描发现眼镜(定向):", name, "RSSI=%d" % int(rssi)) else: if name and "INMO" in name.upper() and self._scan_result is None: self._scan_result = perp def centralManager_didConnectPeripheral_(self, mgr, perp): print("didConnect", perp.name()) perp.discoverServices_([CBUUID.UUIDWithString_(SVC_UUID)]) print("discoverServices(%s) called" % SVC_UUID) def centralManager_didFailToConnectPeripheral_error_(self, mgr, perp, err): print("didFailToConnect", err) def centralManager_didDisconnectPeripheral_error_(self, mgr, perp, err): print("didDisconnect", err) self.ready = False self.write_char = None self.notify_char = None def peripheral_didDiscoverServices_(self, perp, err): try: if err: print("discoverServices err:", err) return for svc in perp.services(): su = svc.UUID().UUIDString().lower() if su == SVC_UUID.lower(): svc.retain() self.svc = svc self.services_discovered = True print("found svc", su) except Exception: import traceback traceback.print_exc() def peripheral_didDiscoverCharacteristicsForService_error_(self, perp, svc, err): try: if err: print("discoverChars err:", err) return for ch in svc.characteristics(): u = ch.UUID().UUIDString().lower() if u == WRITE_UUID.lower(): self.write_char = ch print("write char:", u) elif u == NOTIFY_UUID.lower(): self.notify_char = ch print("notify char:", u) if self.write_char and self.notify_char: self.ready = True except Exception: import traceback traceback.print_exc() def peripheral_didUpdateNotificationStateForCharacteristic_error_(self, perp, ch, err): if err: print("notify state err:", err) else: print("subscribed notify:", ch.UUID().UUIDString()) def peripheral_didUpdateValueForCharacteristic_error_(self, perp, ch, err): if err: print("value err:", err) return v = ch.value() if v is None: return try: print("[notify/ack] %s" % bytes(v).decode("utf-8")) except Exception: print("[notify] %s" % binascii.hexlify(bytes(v)).decode()) class BleProxy: def __init__(self): self.d = D.alloc().init() self.d.manager = None self.d.peripheral = None self.d.write_char = None self.d.notify_char = None self.d.svc = None self.d.ready = False self.d.services_discovered = False self.d._scan_result = None self.loop = NSRunLoop.currentRunLoop() self.mgr = CBCentralManager.alloc().initWithDelegate_queue_options_(self.d, None, None) self.lock = threading.Lock() self.queue = [] def enqueue(self, obj): with self.lock: self.queue.append(obj) def wait_powered(self, timeout=10): t0 = time.time() while self.d.manager is None or self.d.manager.state() != 5: # 5 = poweredOn self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1)) if time.time() - t0 > timeout: print("蓝牙未开启 / 未就绪") return False return True def _find_peripheral(self): # 用多个服务 UUID 定位已连接外设:INMO 控制服务、我们自定义的桥接服务、设备信息服务(0x180A) svc_list = [LOOKUP_UUID, SVC_UUID, "0000180A-0000-1000-8000-00805F9B34FB"] connected = self.mgr.retrieveConnectedPeripheralsWithServices_( [CBUUID.UUIDWithString_(s) for s in svc_list]) for c in connected: name = c.name() if name and "INMO" in name.upper(): return c if connected: return connected[0] # 定向扫描我们的自定义服务(眼镜已开启 BLE 广播) print("未在已连接列表找到 INMO,尝试定向扫描 A9E0ABCD…") self.d._scan_result = None self.d._targeted_scan = True self.mgr.scanForPeripheralsWithServices_options_( [CBUUID.UUIDWithString_(SVC_UUID)], None) t0 = time.time() while self.d._scan_result is None and time.time() - t0 < 15: self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1)) self.mgr.stopScan() self.d._targeted_scan = False return self.d._scan_result def connect(self): perp = self._find_peripheral() if perp is None: print("未找到 INMO 眼镜,请确认系统蓝牙已配对并连接") return False self.d.peripheral = perp perp.setDelegate_(self.d) self.d.ready = False self.d.services_discovered = False self.d.write_char = None self.d.notify_char = None self.d.svc = None self.mgr.connectPeripheral_options_(perp, None) t0 = time.time() while not self.d.services_discovered: self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1)) if time.time() - t0 > 15: print("services not discovered") return False char_uuids = [CBUUID.UUIDWithString_(WRITE_UUID), CBUUID.UUIDWithString_(NOTIFY_UUID)] perp.discoverCharacteristics_forService_(char_uuids, self.d.svc) t0 = time.time() while not self.d.ready: self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1)) if time.time() - t0 > 15: print("not ready; write_char?", self.d.write_char, "notify?", self.d.notify_char) return False perp.setNotifyValue_forCharacteristic_(True, self.d.notify_char) self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.5)) print("BLE 桥接已就绪:可发送命令") return True def _reconnect(self): print("尝试重新连接…") try: self.connect() except Exception as e: print("reconnect error:", e) def send(self, obj, wtype=WRITE_WITH_RESPONSE): if not self.d.ready or self.d.write_char is None: print("未就绪,丢弃命令:", obj) return data = json.dumps(obj).encode("utf-8") self.d.peripheral.writeValue_forCharacteristic_type_( NSData.alloc().initWithBytes_length_(data, len(data)), self.d.write_char, wtype) def run(self, demo=False, mouse=False): if not self.wait_powered(): return if not self.connect(): return if demo: self._demo() if mouse: self._start_mouse() while True: with self.lock: batch = self.queue[:] self.queue[:] = [] for cmd in batch: self.send(cmd) if not self.d.ready: print("连接丢失,3 秒后重连…") time.sleep(3) self._reconnect() continue self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.1)) def _demo(self): print("== demo:让眼镜显示光标并移动/点击(请观察眼镜屏幕)==") self.send({"type": "cursor", "visible": True}, WRITE_WITHOUT_RESPONSE) self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.4)) for _ in range(12): self.send({"type": "mouse", "action": "move", "dx": 22, "dy": 0}, WRITE_WITHOUT_RESPONSE) self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.12)) for _ in range(6): self.send({"type": "mouse", "action": "move", "dx": 0, "dy": 22}, WRITE_WITHOUT_RESPONSE) self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.12)) self.send({"type": "mouse", "action": "click"}, WRITE_WITH_RESPONSE) self.loop.runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.6)) print("== demo 完成 ==") def _start_mouse(self): try: from Quartz import (CGEventTapCreate, kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventMaskMouseMoved, kCGEventMaskLeftMouseDown, kCGEventMaskLeftMouseUp, kCGEventMouseMoved, kCGEventLeftMouseDown, kCGEventLeftMouseUp, CGEventTapEnable, CFRunLoopAddSource, kCFRunLoopCommonModes, CFMachPortCreateRunLoopSource) except Exception as e: print("无法导入 Quartz(鼠标捕获不可用):", e) return self._last = None def cb(proxy, ev, ref): etype = ev.type() loc = ev.location() if etype == kCGEventMouseMoved: if self._last is not None: dx = int(loc.x - self._last[0]) dy = int(loc.y - self._last[1]) if dx or dy: self.enqueue({"type": "mouse", "action": "move", "dx": dx, "dy": dy}) self._last = (loc.x, loc.y) elif etype == kCGEventLeftMouseDown: self.enqueue({"type": "mouse", "action": "click"}) return ev mask = (kCGEventMaskMouseMoved | kCGEventMaskLeftMouseDown | kCGEventMaskLeftMouseUp) tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, mask, cb, None) if not tap: print("⚠️ 无法创建鼠标事件监听:请在 系统设置→隐私与安全性→输入监控 中授权本程序(终端/Python)") return src = CFMachPortCreateRunLoopSource(None, tap, 0) CFRunLoopAddSource(self.loop, src, kCFRunLoopCommonModes) CGEventTapEnable(tap, True) print("== 鼠标捕获已开启:移动 Mac 鼠标=移动眼镜光标,点击=点击 ==") def stdin_thread(proxy): print("输入命令:move dx dy | click | home | back | recent | cursor on|off | scroll dy | text 内容 | quit") while True: try: line = sys.stdin.readline() except Exception: break if not line: break line = line.strip() if not line: continue parts = line.split() cmd = parts[0].lower() try: if cmd == "quit": print("退出") os._exit(0) elif cmd == "move" and len(parts) >= 3: proxy.enqueue({"type": "mouse", "action": "move", "dx": int(parts[1]), "dy": int(parts[2])}) elif cmd == "click": proxy.enqueue({"type": "mouse", "action": "click"}) elif cmd in ("home", "back", "recent"): proxy.enqueue({"type": "key", "action": cmd}) elif cmd == "cursor" and len(parts) >= 2: proxy.enqueue({"type": "cursor", "visible": parts[1].lower() in ("on", "true", "1")}) elif cmd == "scroll" and len(parts) >= 2: proxy.enqueue({"type": "mouse", "action": "scroll", "dy": int(parts[1])}) elif cmd == "text": proxy.enqueue({"type": "text", "content": line[4:].strip()}) else: print("未知命令") except ValueError: print("参数格式错误") def main(): import argparse ap = argparse.ArgumentParser() ap.add_argument("--demo", action="store_true", help="自动演示:显示光标并移动/点击") ap.add_argument("--mouse", action="store_true", help="捕获 Mac 真实鼠标(需输入监控授权)") args = ap.parse_args() proxy = BleProxy() t = threading.Thread(target=stdin_thread, args=(proxy,), daemon=True) t.start() proxy.run(demo=args.demo, mouse=args.mouse) if __name__ == "__main__": main()