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

123 lines
3.7 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.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
本地验证用 WebSocket 回显服务器(仅用于在没有眼镜时测试浏览器控制器)。
用法:
python3 test_ws_server.py [port]
默认端口 8080。
然后在电脑浏览器打开 controller.html,IP 填 127.0.0.1,点连接。
你在触摸板上滑动/点击,这里会实时打印收到的指令,确认浏览器→WS 链路通了。
眼镜端 Kotlin App 接的就是同款协议。
"""
import base64
import hashlib
import os
import struct
import sys
import threading
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
HOST = "0.0.0.0"
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
def make_accept(key: str) -> str:
return base64.b64encode(
hashlib.sha1((key + GUID).encode()).digest()
).decode()
def send_text(conn, text: str):
payload = text.encode()
header = bytes([0x81, 0x80 | len(payload)]) # FIN+text, MASK set
mask = os.urandom(4)
masked = bytes(payload[i] ^ mask[i % 4] for i in range(len(payload)))
conn.sendall(header + mask + masked)
def read_frame(conn):
"""读取一个客户端帧,返回 (opcode, payload)。仅处理未分片的小帧。"""
hdr = conn.recv(2)
if len(hdr) < 2:
return None, None
opcode = hdr[0] & 0x0F
masked = hdr[1] & 0x80
length = hdr[1] & 0x7F
if length == 126:
length = struct.unpack(">H", conn.recv(2))[0]
elif length == 127:
length = struct.unpack(">Q", conn.recv(8))[0]
if masked:
mask = conn.recv(4)
payload = b""
while len(payload) < length:
chunk = conn.recv(length - len(payload))
if not chunk:
return None, None
payload += chunk
if masked:
payload = bytes(payload[i] ^ mask[i % 4] for i in range(len(payload)))
return opcode, payload
def handle(conn, addr):
print(f"[+] 客户端接入 {addr}")
# 1) 读取 HTTP 升级请求,拿到 Sec-WebSocket-Key
buf = b""
while b"\r\n\r\n" not in buf:
buf += conn.recv(4096)
key = ""
for line in buf.split(b"\r\n"):
if line.lower().startswith(b"sec-websocket-key:"):
key = line.split(b":", 1)[1].strip().decode()
conn.sendall(
(
"HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
f"Sec-WebSocket-Accept: {make_accept(key)}\r\n\r\n"
).encode()
)
send_text(conn, '{"type":"hello","role":"glasses(test)"}')
# 2) 循环读取指令
while True:
try:
opcode, payload = read_frame(conn)
except Exception:
break
if opcode is None:
break
if opcode == 0x8: # close
print("[-] 客户端断开")
break
if opcode == 0x9: # ping -> pong
send_text(conn, "")
continue
if opcode == 0x1: # text
msg = payload.decode(errors="replace")
print(f" 收到指令: {msg}")
elif opcode == 0x2: # binary
print(f" 收到二进制: {len(payload)} 字节")
def main():
srv = socket(AF_INET, SOCK_STREAM)
srv.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
srv.bind((HOST, PORT))
srv.listen(5)
print(f"=== 测试 WS 服务器监听 ws://0.0.0.0:{PORT} ===")
print("=== 打开 controller.html,IP 填 127.0.0.1,点连接,滑动即可看到指令 ===\n")
try:
while True:
conn, addr = srv.accept()
threading.Thread(target=handle, args=(conn, addr), daemon=True).start()
except KeyboardInterrupt:
print("\n已停止")
if __name__ == "__main__":
main()