commit 2840e82685e4ab86fa66fc55997d7baf9ea629f6 Author: admin Date: Wed Aug 26 10:34:23 2026 +0800 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) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bda6b2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# macOS +.DS_Store + +# WorkBuddy 元数据(workspace 内部状态) +.workbuddy/ + +# Android 构建产物 +glass-mouse/android/.gradle/ +glass-mouse/android/build/ +glass-mouse/android/app/build/ +glass-mouse/android/local.properties +glass-mouse/android/.idea/ +glass-mouse/android/captures/ +glass-mouse/android/.cxx/ +glass-mouse/android/app/release/ + +# iOS 构建产物 + 用户特定文件 +ios/GlassRemote/build/ +ios/GlassRemote/DerivedData/ +ios/GlassRemote/.build/ +ios/GlassRemote/Pods/ +**/xcuserdata/ +**/*.xcuserstate +**/*.xcuserdatad/ + +# 临时分发目录(APK + QR code 由 HTTP server 现场生成) +download/ + +# 历史归档(290MB INMO 固件包等) +archive/ + +# 临时构建产物(部署/编译产生) +/tmp/dd_* diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..5e3f03b --- /dev/null +++ b/README.MD @@ -0,0 +1,56 @@ +这是一个通过 websockect 将ppt演示时备注信息推送到智能眼镜的程序 + +# 对应ppt脚本示例 +' 声明全局变量:接口地址、推送) +Dim apiUrl As String + +Sub OnSlideShowPageChange() + ' 获取当前幻灯片编号 + Dim currentSlide As Integer + currentSlide = ActivePresentation.SlideShowWindow.View.CurrentShowPosition + remarkText = ActivePresentation.SlideShowWindow.View.Slide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text + + ' 显示提醒(例如:显示当前页数) + ' MsgBox "现在是第 " & currentSlide & " 页是第 " & remarkText & " 页" + + Dim postData As String + Dim curlCommand As String + Dim escapedPostData As String + apiUrl = "http://192.168.1.18/ppt/remark" + + ' ========== 关键修正:分步转义特殊字符 ========== + ' 1. 构造原始JSON(先不转义,统一处理) + postData = "{""pageNum"":" & currentSlide & ",""remark"":""" & remarkText & """}" + + 'apiUrl = "https://rest-hz.goeasy.io/v2/pubsub/publish" + 'postData = "{""appkey"":""BC-671cf99a73a942748b856a6bd0d5ea7e"",""channel"":""test_channel"",""content"":""" & remarkText & """}" + ' 2. 针对Mac终端的转义规则: + ' - 先替换JSON中的双引号为 \\"" (终端需要双层转义) + ' - 避免直接拼接空格,用单引号包裹整个-d参数 + escapedPostData = Replace(postData, """", "\""") + escapedApiUrl = apiUrl ' 地址无特殊字符可直接用,若有空格需额外转义 + + ' 3. 构造正确的curl命令(核心修正) + ' /bin/bash -c 后必须用单引号包裹整个命令,内部双引号双层转义 + curlCommand = "/bin/bash -c '" & _ + "curl --silent --show-error -X POST " & _ + "-H ""Content-Type: application/json;charset=utf-8"" " & _ + "-d """ & escapedPostData & """ " & _ + apiUrl & "'" + + ' 调试用:弹出构造后的curl命令,可复制到终端手动测试 + 'MsgBox "最终curl命令:" & vbCrLf & curlCommand, vbInformation, "调试信息" + + ' 执行curl命令(Mac下Shell返回值>0代表执行成功,不代表接口返回成功) + Dim shellResult As Double + On Error Resume Next ' 捕获Shell执行错误 + shellResult = Shell(curlCommand, vbHide) + On Error GoTo 0 + + ' 验证执行结果 + 'If shellResult > 0 Then + ' MsgBox "curl命令执行成功!请检查Python接口是否接收到数据", vbInformation, "执行成功" + 'Else + ' MsgBox "curl命令执行失败!请复制上方调试命令到终端手动测试", vbCritical, "执行失败" + ' End If +End Sub \ No newline at end of file diff --git a/glass-mouse/README.md b/glass-mouse/README.md new file mode 100644 index 0000000..bbafef6 --- /dev/null +++ b/glass-mouse/README.md @@ -0,0 +1,167 @@ +# GlassMouse —— 蓝牙遥控智能眼镜(iPhone BLE 遥控 + PPT 备注投屏) + +> 绕过 INMO 官方「仅 Android 手机伴侣可做鼠标/投屏」的限制:在**眼镜端**装一个原生接收 App, +> iPhone 用 **GlassRemote(BLE)** 直连遥控;PPT 演讲备注由 Mac(WPS/PowerPoint 宏)经 HTTP 推到眼镜投屏。 +> 无需 root、无需 Mac、无需网页。 + +## 一、项目简介 + +INMO Air3 官方手机伴侣的「模拟鼠标 / 文字投屏」只在 Android 版可用(私有 BLE / `MediaProjection`)。 +难点「把指令变成眼镜真实输入」发生在眼镜端(Android),所以**眼镜上有一个原生接收器即可**。 + +- **遥控 / 投屏长文本**:iPhone 原生 App **GlassRemote** 走 BLE 直连(绕开 INMO 绑定墙),低延迟。 +- **PPT 备注投屏**:Mac 上 WPS/PowerPoint 放映时,宏把当前页备注 `POST` 到眼镜 `:8000`,眼镜全屏自动滚动显示(提词)。 +- 本项目「文字投屏」是把**一段文本**从外部推到眼镜屏幕全屏显示,**不是**镜像 iPhone 整屏(受 iOS 限制做不到)。 + +## 二、架构 + +``` +iPhone(GlassRemote App) ──BLE GATT A9E0ABCD…──▶ ProxyBleServer ──▶ handleCommand + │ (控制指令 + 投屏长文本分片发送) + ▼ + 眼镜 App 前台服务(MouseService) + ├─ 悬浮光标(可隐藏,需悬浮窗权限) + ├─ 文字投屏(全屏 Activity:翻页大字 / 自动滚动三行跳) + ├─ 无障碍服务注入 点击/主页/返回(可选) + └─ Camera2 拍照存相册(可选) + +Mac(WPS/PowerPoint 宏) ──http POST :8000/ppt/remark──▶ HttpRemoteServer ──▶ 投屏(自动滚动提词) +``` + +- **主通道(遥控)**:BLE。iPhone GlassRemote 走自定义 GATT(UUID `A9E0ABCD-0000-1000-8000-00805F9B34FB`),命令 JSON 与旧 WS 协议完全一致,由 `ProxyBleServer` 重组分片后交 `handleCommand`。 +- **PPT 投屏通道**:HTTP `:8000` 仅接收 `POST /ppt/remark`(`{pageNum, remark}`),回调投屏层自动滚动显示;`GET /` 仅返回一个小状态页(含「眼镜控制器」标识,方便手动扫网段定位眼镜)。 +- **已移除(网络版遥控)**:Safari 网页遥控器(`remote.html`)、WebSocket `:8080` 命令服务、密码鉴权、mDNS 发现。统一由 BLE 替代。 +- 全程不经任何云端。 + +## 三、目录结构 + +``` +glass-mouse/ +├── android/ # 眼镜端 Kotlin App(Gradle,compileSdk 34 / minSdk 24 / Java 17) +│ └── app/src/main/ +│ ├── java/com/glass/mouse/ +│ │ ├── MainActivity.kt # 主界面 + 投屏显示(翻页大字/自动滚动) + 显示 BLE 状态与 WiFi IP +│ │ ├── MouseService.kt # 核心:前台服务 + 命令分发 + 光标/拍照/投屏调度 + IP 刷新 +│ │ ├── GlassMouseService.kt # 无障碍服务:点击/主页/返回注入(可选) +│ │ ├── GlassCamera.kt # Camera2 抓拍存相册 Pictures/GlassMouse/ +│ │ ├── HttpRemoteServer.kt # :8000 仅接收 POST /ppt/remark(PPT 投屏) +│ │ └── ProxyBleServer.kt # BLE GATT 桥接(主遥控通道) +│ └── res/layout/activity_main.xml # 启动页(BLE 状态 + 本机 IP)+ 投屏层 +├── docs/ +│ ├── ble_protocol.md # 早期 BLE 协议逆向研究 +│ └── ble_probe_guide.md # BLE 探测笔记 +├── macros/ +│ ├── ppt-remark-macro.bas # PowerPoint VBA 宏(备注→眼镜投屏) +│ └── ppt-remark-macro-wps.js # WPS JS 宏(备注→眼镜投屏) +├── scripts/ # Python 辅助脚本(参考 / 备用) +│ ├── ble_proxy.py # Mac 端通过 BLE 桥接发命令 +│ ├── ble_bind_pyobjc.py / ble_diag.py / ble_scan.py # BLE 探测/绑定/扫描 +│ ├── ws_probe.py / test_ws_server.py # ⚠️ 已弃用(网络版 WS 已移除) +│ ├── inmo_proto.py # INMO 协议解析 +│ └── _*.py # 早期逆向/破解/字符串提取脚本(归档参考) +├── download/app-debug.apk # 已编译 debug 包(侧载到眼镜),实际位于仓库根目录 download/ +└── README.md +``` + +## 四、功能清单 + +| 功能 | 说明 | +|------|------| +| 光标控制 | 触摸板相对移动;「滚动」开关下上下滑=滚动;「光标:开/关」可隐藏悬浮圈 | +| 点击 / 导航 | 触摸板轻点=点击;系统导航键(返回/主页/最近)经无障碍注入 | +| 拍照 | Camera2 抓拍(眼镜前向摄像头),存 `Pictures/GlassMouse/` | +| 文字投屏·翻页模式 | 整页**粗体**大字、四周 10% 黑边、屏幕常亮 | +| 文字投屏·自动滚动 | 全文**粗体**、光标自动移到最右侧;**间隔跳三行**(非平滑);10 档速度(最慢 20s/三行、最快 5s/三行、默认≈10s);到末尾循环 | +| PPT 备注投屏 | Mac WPS/PowerPoint 放映时,宏 POST 当前页备注 → 眼镜自动滚动提词 | +| 投屏常亮 | 投屏期间 `FLAG_KEEP_SCREEN_ON`,关闭后恢复自动熄屏 | + +## 五、通信协议(JSON over BLE,复用同一 `handleCommand`) + +iPhone → 眼镜(经 BLE 分片重组后): + +```json +{"type":"mouse","action":"move","dx":5,"dy":-3} // 相对移动光标 +{"type":"mouse","action":"click"} // 在光标处点按 +{"type":"mouse","action":"scroll","dy":-50} // 滚动(负=上) +{"type":"key","action":"home"|"back"|"recent"} // 系统导航键 +{"type":"cursor","visible":false} // 隐藏/显示悬浮光标 + +{"type":"text","content":"要投屏的文字…"} // 翻页模式:整页大字显示 +{"type":"text","action":"close"} // 关闭投屏 +{"type":"text","action":"auto_scroll","content":"…","speed":7} // 自动滚动(三行跳),speed=档位1..10 +{"type":"text","action":"stop_scroll"} // 暂停滚动(保留位置) +{"type":"text","action":"resume_scroll"} // 继续滚动 +{"type":"text","action":"set_speed","speed":7} // 运行中实时调速 + +{"type":"camera","action":"capture"} // 拍照 +{"type":"sys","action":"ip"} // 查询本机 WiFi IP(回传供 PPT 宏参考) +``` + +眼镜 → iPhone(经 BLE 通知 `bleAckSink`): + +```json +{"type":"ack","action":"click","result":"ok"|"no_instance","x":..,"y":..} +{"type":"ack","action":"cast","result":"ok"|"paused"|"resumed"|"app_not_foreground"} +{"type":"ack","action":"camera","result":"ok"|"no_camera_permission"|"open_error"} +{"type":"ack","action":"ip","result":"ok","ip":"192.168.1.x"} +``` + +> 点击结果:`ok`=已注入;`no_instance`=未开无障碍服务(重装后系统会重置,需去眼镜「设置→无障碍→GlassMouse」重新开启)。 + +## 六、PPT 备注投屏(Mac WPS / PowerPoint) + +- 宏文件:`macros/ppt-remark-macro-wps.js`(WPS JS 宏)或 `macros/ppt-remark-macro.bas`(PowerPoint VBA)。 +- 工作原理:放映翻页时,宏读取当前页「备注」,向 `http://<眼镜IP>:8000/ppt/remark` 发送 + `{"pageNum":N,"remark":"…"}`;眼镜收到后全屏自动滚动显示(演讲提词)。 +- 眼镜屏显本机 IP,手填进宏顶部 `GLASS_IP` 常量(IP 随路由器变,变了改一行重填)。 +- `pageNum==0` 视作演示结束,自动收起投屏。 + +## 七、构建与部署 + +前置:Android SDK(compileSdk 34)、JDK 17、Gradle 8.14+。 + +```bash +cd glass-mouse/android +gradle assembleDebug --no-daemon # 或 ./gradlew +# 产物:android/app/build/outputs/apk/debug/app-debug.apk +# 复制到仓库根 download/app-debug.apk 供侧载 +adb install -r app-debug.apk # 侧载到眼镜(需 USB 调试) +``` + +依赖:无第三方库(BLE 用 Android `BluetoothGatt`,HTTP 用 `ServerSocket`,均原生 API)。 + +## 八、使用指南 + +1. **安装**:把 `download/app-debug.apk`(或自编译包)侧载到眼镜,打开 GlassMouse App。 +2. **iPhone 遥控**:Mac 上用 Xcode(免费 Apple ID 签名)跑 `ios/GlassRemote`,数据线装到 iPhone; + 打开 GlassRemote,BLE 连眼镜(设备名 `INMO AIR3 …`)。 +3. **PPT 投屏**:Mac 打开 WPS/PowerPoint,把对应宏粘进 VBA/JS 宏编辑器,宏顶部 `GLASS_IP` 填眼镜屏显示的 IP; + 放映时每翻一页,备注自动滚到眼镜上。 +4. 眼镜主界面显示:BLE 连接状态 + 本机 WiFi IP(供 PPT 宏手填)。 + +> ⚠️ 每次重装 APK 后,系统会**重置无障碍服务**为关闭 → 点击/返回失效。去眼镜「设置 → 无障碍 → GlassMouse」重新开启,约 3 秒恢复。 + +## 九、安全 + +- 遥控走 BLE(近场、需物理邻近),不经网络,无云端。 +- PPT 投屏 HTTP `:8000` 仅接受同网段 `POST /ppt/remark`,无鉴权(同 WiFi 均为自己人);不暴露任何控制指令。 + +## 十、已知问题 / 排查 + +| 现象 | 原因 / 处理 | +|------|------| +| 能移动光标但不能点击 | 无障碍被重置 → 眼镜「设置→无障碍→GlassMouse」重开 | +| PPT 备注没上屏 | 宏 `GLASS_IP` 与眼镜屏 IP 不一致 / 眼镜 App 未在前台 / 同 WiFi | +| 拍照返回红字 | 多为未授权相机(装后首次允许)或相机被占用 | +| BLE 连不上 | 眼镜 App 在前台;iPhone 蓝牙开;设备名 `INMO AIR3 …` | + +## 十一、Python 辅助脚本(参考 / 备用,均在 `scripts/` 目录) + +- `scripts/ble_proxy.py`:Mac 端通过 BLE 桥接发命令(需 `pyobjc` + `bleak`)。 +- `scripts/ble_bind_pyobjc.py` / `scripts/ble_diag.py` / `scripts/ble_scan.py`:早期 BLE 探测、绑定、扫描。 +- `scripts/ws_probe.py` / `scripts/test_ws_server.py`:**已弃用**(网络版 WebSocket 已移除)。 +- `docs/ble_protocol.md` / `docs/ble_probe_guide.md`:INMO 官方 BLE GATT 逆向研究笔记。 + +--- + +项目记忆见 `.workbuddy/memory/`(按日期日志 + MEMORY.md 长期笔记)。 diff --git a/glass-mouse/android/app/build.gradle b/glass-mouse/android/app/build.gradle new file mode 100644 index 0000000..8ba3f10 --- /dev/null +++ b/glass-mouse/android/app/build.gradle @@ -0,0 +1,34 @@ +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' + +android { + compileSdk 34 + + defaultConfig { + namespace "com.glass.mouse" + applicationId "com.glass.mouse" + minSdk 24 + targetSdk 34 + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + } + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + + kotlinOptions { + jvmTarget = '17' + } +} + +dependencies { + implementation 'org.java-websocket:Java-WebSocket:1.5.7' +} diff --git a/glass-mouse/android/app/src/main/AndroidManifest.xml b/glass-mouse/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..0f2dcb8 --- /dev/null +++ b/glass-mouse/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/glass-mouse/android/app/src/main/java/com/glass/mouse/GlassCamera.kt b/glass-mouse/android/app/src/main/java/com/glass/mouse/GlassCamera.kt new file mode 100644 index 0000000..24cc981 --- /dev/null +++ b/glass-mouse/android/app/src/main/java/com/glass/mouse/GlassCamera.kt @@ -0,0 +1,179 @@ +package com.glass.mouse + +import android.Manifest +import android.content.ContentValues +import android.content.Context +import android.content.pm.PackageManager +import android.graphics.ImageFormat +import android.hardware.camera2.CameraCaptureSession +import android.hardware.camera2.CameraCharacteristics +import android.hardware.camera2.CameraDevice +import android.hardware.camera2.CameraManager +import android.hardware.camera2.CaptureRequest +import android.media.ImageReader +import android.os.Build +import android.os.Handler +import android.os.HandlerThread +import android.provider.MediaStore +import android.util.Log +import android.util.Size +import java.io.IOException +import java.util.concurrent.Semaphore + +/** + * 用 Camera2 直接抓拍一张 JPEG 存到相册(Pictures/GlassMouse)。 + * 设计为从后台服务触发(被遥控器网页的「拍照」指令调用), + * 因此自带独立 HandlerThread,不依赖 Activity / 预览 Surface。 + * 所有结果通过 onResult(ok, msg) 回传,msg 用于 ack 上报。 + */ +object GlassCamera { + + private const val TAG = "GlassCamera" + private val lock = Semaphore(1) // 同一时刻只允许一次拍摄 + + fun capture(context: Context, onResult: (ok: Boolean, msg: String) -> Unit) { + if (context.checkSelfPermission(Manifest.permission.CAMERA) + != PackageManager.PERMISSION_GRANTED + ) { + onResult(false, "no_camera_permission") + return + } + if (!lock.tryAcquire()) { + onResult(false, "busy") + return + } + + val thread = HandlerThread("GlassCameraThread").apply { start() } + val handler = Handler(thread.looper) + + val cleanup = { + try { lock.release() } catch (_: Exception) {} + thread.quitSafely() + } + + val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager + val cameraId = chooseCamera(manager) + if (cameraId == null) { + cleanup(); onResult(false, "no_camera"); return + } + + try { + manager.openCamera(cameraId, object : CameraDevice.StateCallback() { + override fun onOpened(camera: CameraDevice) { + val characteristics = manager.getCameraCharacteristics(cameraId) + val map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) + val jpegSizes = map?.getOutputSizes(ImageFormat.JPEG) + val size = jpegSizes?.maxByOrNull { it.width * it.height } ?: Size(1280, 720) + + val reader = ImageReader.newInstance(size.width, size.height, ImageFormat.JPEG, 1) + val readerSurface = reader.surface + reader.setOnImageAvailableListener({ readerRef -> + try { + val image = readerRef.acquireNextImage() + val buffer = image.planes[0].buffer + val bytes = ByteArray(buffer.remaining()) + buffer.get(bytes) + image.close() + val saved = saveToPictures(context, bytes) + onResult(true, if (saved) "ok" else "save_failed") + } catch (e: Exception) { + Log.e(TAG, "read image failed", e) + onResult(false, "capture_failed") + } finally { + try { camera.close() } catch (_: Exception) {} + try { reader.close() } catch (_: Exception) {} + cleanup() + } + }, handler) + + camera.createCaptureSession(listOf(readerSurface), + object : CameraCaptureSession.StateCallback() { + override fun onConfigured(session: CameraCaptureSession) { + try { + val req = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE) + req.addTarget(readerSurface) + session.capture(req.build(), + object : CameraCaptureSession.CaptureCallback() {}, handler) + } catch (e: Exception) { + Log.e(TAG, "capture failed", e) + try { camera.close() } catch (_: Exception) {} + try { reader.close() } catch (_: Exception) {} + cleanup(); onResult(false, "capture_failed") + } + } + + override fun onConfigureFailed(session: CameraCaptureSession) { + Log.e(TAG, "configure failed") + try { camera.close() } catch (_: Exception) {} + try { reader.close() } catch (_: Exception) {} + cleanup(); onResult(false, "config_failed") + } + }, handler) + } + + override fun onDisconnected(camera: CameraDevice) { + try { camera.close() } catch (_: Exception) {} + } + + override fun onError(camera: CameraDevice, error: Int) { + Log.e(TAG, "camera open error: $error") + try { camera.close() } catch (_: Exception) {} + cleanup(); onResult(false, "open_error") + } + }, handler) + } catch (e: SecurityException) { + Log.e(TAG, "security", e); cleanup(); onResult(false, "no_camera_permission") + } catch (e: Exception) { + Log.e(TAG, "openCamera", e); cleanup(); onResult(false, "open_exception") + } + } + + /** 优先取后置(眼镜前向)摄像头,其次前置,最后任意 */ + private fun chooseCamera(manager: CameraManager): String? { + return try { + val ids = manager.cameraIdList + var front: String? = null + var any: String? = null + for (id in ids) { + val facing = manager.getCameraCharacteristics(id) + .get(CameraCharacteristics.LENS_FACING) + if (facing == CameraCharacteristics.LENS_FACING_BACK) return id + if (facing == CameraCharacteristics.LENS_FACING_FRONT) front = id + any = id + } + front ?: any + } catch (e: Exception) { + Log.e(TAG, "chooseCamera", e); null + } + } + + private fun saveToPictures(context: Context, bytes: ByteArray): Boolean { + val name = "GlassPhoto_${System.currentTimeMillis()}.jpg" + return try { + val resolver = context.contentResolver + val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) + else MediaStore.Images.Media.EXTERNAL_CONTENT_URI + val values = ContentValues().apply { + put(MediaStore.Images.Media.DISPLAY_NAME, name) + put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/GlassMouse") + put(MediaStore.Images.Media.IS_PENDING, 1) + } + } + val uri = resolver.insert(collection, values) ?: return false + resolver.openOutputStream(uri)?.use { it.write(bytes) } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + values.clear() + values.put(MediaStore.Images.Media.IS_PENDING, 0) + resolver.update(uri, values, null, null) + } + true + } catch (e: IOException) { + Log.e(TAG, "save io", e); false + } catch (e: Exception) { + Log.e(TAG, "save", e); false + } + } +} diff --git a/glass-mouse/android/app/src/main/java/com/glass/mouse/GlassMouseService.kt b/glass-mouse/android/app/src/main/java/com/glass/mouse/GlassMouseService.kt new file mode 100644 index 0000000..129a71f --- /dev/null +++ b/glass-mouse/android/app/src/main/java/com/glass/mouse/GlassMouseService.kt @@ -0,0 +1,122 @@ +package com.glass.mouse + +import android.accessibilityservice.AccessibilityService +import android.accessibilityservice.AccessibilityService.GestureResultCallback +import android.accessibilityservice.GestureDescription +import android.graphics.Path +import android.os.Handler +import android.os.Looper +import android.view.accessibility.AccessibilityEvent + +/** + * 仅负责「系统级输入注入」(点击 / 滑动 / 导航键)。 + * 这是可选能力:用户开启无障碍后才生效;未开启时 MouseService 会安全跳过点击。 + * WebSocket 与光标已移到 MouseService,不再依赖本服务启动。 + * + * 关键:injectTap/injectScroll 通过 GestureResultCallback 拿到「手势是否真正被执行」 + * 的结果,便于诊断 IMOS 是否拦截了第三方无障碍手势。 + */ +class GlassMouseService : AccessibilityService() { + + override fun onServiceConnected() { + super.onServiceConnected() + INSTANCE = this + } + + /** 在 (x,y) 注入一次点按;onResult(true)=系统已执行, false=被拒绝/取消 */ + fun injectTap(x: Int, y: Int, onResult: (Boolean) -> Unit) { + val path = Path().apply { + moveTo(x.toFloat(), y.toFloat()) + lineTo(x.toFloat() + 1, y.toFloat() + 1) + } + val gesture = GestureDescription.Builder() + .addStroke(GestureDescription.StrokeDescription(path, 0, 80)) + .build() + val handler = Handler(Looper.getMainLooper()) + val accepted = dispatchGesture(gesture, object : GestureResultCallback() { + override fun onCompleted(gesture: GestureDescription?) = onResult(true) + override fun onCancelled(gesture: GestureDescription?) = onResult(false) + }, handler) + if (!accepted) onResult(false) + } + + /** 在 (x,y) 注入一次滑动(用于滚动) */ + fun injectScroll(x: Int, y: Int, dy: Int, onResult: (Boolean) -> Unit) { + val path = Path().apply { + moveTo(x.toFloat(), y.toFloat()) + lineTo(x.toFloat(), (y + dy).toFloat()) + } + val gesture = GestureDescription.Builder() + .addStroke(GestureDescription.StrokeDescription(path, 0, 160)) + .build() + val handler = Handler(Looper.getMainLooper()) + val accepted = dispatchGesture(gesture, object : GestureResultCallback() { + override fun onCompleted(gesture: GestureDescription?) = onResult(true) + override fun onCancelled(gesture: GestureDescription?) = onResult(false) + }, handler) + if (!accepted) onResult(false) + } + + /** + * 通用手势注入(iPhone/官方硬件 touch 协议入口): + * - 单击:x1==x2, y1==y2, duration 短 + * - 滑动:x1!=x2 或 y1!=y2, duration 与手势时长匹配 + * 走真实系统 GestureDescription 通道,效果等同 INMO 官方硬件 touch + */ + fun injectGesture(x1: Int, y1: Int, x2: Int, y2: Int, durationMs: Long, onResult: (Boolean) -> Unit) { + val path = Path().apply { + moveTo(x1.toFloat(), y1.toFloat()) + lineTo(x2.toFloat(), y2.toFloat()) + } + val gesture = GestureDescription.Builder() + .addStroke(GestureDescription.StrokeDescription(path, 0, durationMs)) + .build() + val handler = Handler(Looper.getMainLooper()) + val accepted = dispatchGesture(gesture, object : GestureResultCallback() { + override fun onCompleted(gesture: GestureDescription?) = onResult(true) + override fun onCancelled(gesture: GestureDescription?) = onResult(false) + }, handler) + if (!accepted) onResult(false) + } + + /** 长按手势(用于拖动滑块等) */ + fun injectLongPress(x: Int, y: Int, durationMs: Long, onResult: (Boolean) -> Unit) { + val path = Path().apply { + moveTo(x.toFloat(), y.toFloat()) + lineTo(x.toFloat() + 1, y.toFloat() + 1) + } + val gesture = GestureDescription.Builder() + .addStroke(GestureDescription.StrokeDescription(path, 0, durationMs)) + .build() + val handler = Handler(Looper.getMainLooper()) + val accepted = dispatchGesture(gesture, object : GestureResultCallback() { + override fun onCompleted(gesture: GestureDescription?) = onResult(true) + override fun onCancelled(gesture: GestureDescription?) = onResult(false) + }, handler) + if (!accepted) onResult(false) + } + + /** 全局导航键;返回是否执行成功 */ + fun injectKey(action: String): Boolean { + return when (action) { + "home" -> performGlobalAction(GLOBAL_ACTION_HOME) + "back" -> performGlobalAction(GLOBAL_ACTION_BACK) + "recent" -> performGlobalAction(GLOBAL_ACTION_RECENTS) + "screen_on" -> performGlobalAction(GLOBAL_ACTION_POWER_DIALOG) // 弹电源菜单(最稳的兼容做法) + "screen_off" -> performGlobalAction(GLOBAL_ACTION_POWER_DIALOG) + else -> false + } + } + + override fun onAccessibilityEvent(event: AccessibilityEvent?) {} + override fun onInterrupt() {} + + override fun onDestroy() { + super.onDestroy() + INSTANCE = null + } + + companion object { + var INSTANCE: GlassMouseService? = null + } +} diff --git a/glass-mouse/android/app/src/main/java/com/glass/mouse/HttpRemoteServer.kt b/glass-mouse/android/app/src/main/java/com/glass/mouse/HttpRemoteServer.kt new file mode 100644 index 0000000..e699898 --- /dev/null +++ b/glass-mouse/android/app/src/main/java/com/glass/mouse/HttpRemoteServer.kt @@ -0,0 +1,169 @@ +package com.glass.mouse + +import android.content.Context +import android.util.Log +import java.io.ByteArrayOutputStream +import java.net.ServerSocket +import java.net.Socket +import java.nio.charset.Charset +import java.util.concurrent.Executors +import org.json.JSONObject + +/** + * 极简 HTTP 服务(仅用于 PPT 备注投屏): + * 接收 POST /ppt/remark(PowerPoint/WPS 宏在翻页时下发 {pageNum, remark}), + * 回调给上层,由眼镜原生投屏层显示(自动滚动提词)。 + * GET / 仅返回一个小状态页(含「眼镜控制器」标识,方便手动扫网段定位眼镜), + * 不再托管遥控器网页——网络版遥控(Safari 网页 + WebSocket 命令)已移除, + * iPhone 遥控/投屏统一走 BLE(ProxyBleServer)。 + */ +class HttpRemoteServer( + @Suppress("UNUSED_PARAMETER") private val context: Context, + private val onRemark: (Int, String) -> Unit +) { + + private var serverSocket: ServerSocket? = null + private var thread: Thread? = null + private val executor = Executors.newCachedThreadPool() + private val statusPage: ByteArray = STATUS_HTML.toByteArray(Charset.forName("UTF-8")) + + fun start(port: Int = 8000) { + try { + serverSocket = ServerSocket(port) + } catch (e: Exception) { + Log.e("HttpRemote", "bind $port failed", e) + return + } + thread = Thread { + while (serverSocket?.isClosed == false) { + try { + val sock = serverSocket!!.accept() + executor.execute { handle(sock) } + } catch (e: Exception) { + break + } + } + } + thread?.start() + Log.i("HttpRemote", "HTTP PPT-remark server on :$port") + } + + private fun handle(sock: Socket) { + try { + val input = sock.getInputStream() + val req = readRequest(input) ?: return + if (req.method == "POST" && req.path == "/ppt/remark") { + handleRemark(req.body, sock) + return + } + // 默认:返回状态页(GET / 及任何其它路径) + sendHtml(sock) + } catch (e: Exception) { + // 忽略单连接异常 + } finally { + try { sock.close() } catch (_: Exception) {} + } + } + + /** + * 按字节解析 HTTP 请求:请求行 + 头部(到 \r\n\r\n 为止),再按 Content-Length 读 body。 + * 不用 BufferedReader 读头部是为了避免缓冲吞掉 body 字节,保证 body 读取位置准确。 + */ + private fun readRequest(input: java.io.InputStream): Request? { + val headerBuf = ByteArrayOutputStream() + val one = ByteArray(1) + var c1 = 0; var c2 = 0; var c3 = 0; var c4 = 0 + while (true) { + val n = input.read(one) + if (n <= 0) break + val b = one[0].toInt() and 0xFF + headerBuf.write(b) + c1 = c2; c2 = c3; c3 = c4; c4 = b + if (c1 == 0x0D && c2 == 0x0A && c3 == 0x0D && c4 == 0x0A) break + } + val headerStr = String(headerBuf.toByteArray(), Charsets.UTF_8) + val firstLine = headerStr.split("\r\n").firstOrNull() ?: return null + val parts = firstLine.split(" ") + val method = parts.getOrNull(0) ?: "GET" + val path = parts.getOrNull(1) ?: "/" + + var contentLength = 0 + for (h in headerStr.split("\r\n")) { + if (h.lowercase().startsWith("content-length:")) { + contentLength = h.substringAfter(":").trim().toIntOrNull() ?: 0 + } + } + + val body = if (contentLength > 0) { + val b = ByteArray(contentLength) + var off = 0 + while (off < contentLength) { + val n = input.read(b, off, contentLength - off) + if (n <= 0) break + off += n + } + String(b, 0, off, Charsets.UTF_8) + } else "" + return Request(method, path, body) + } + + private data class Request(val method: String, val path: String, val body: String) + + private fun handleRemark(body: String, sock: Socket) { + var pageNum = 0 + var remark = "" + try { + val json = JSONObject(body) + pageNum = json.optInt("pageNum", 0) + remark = json.optString("remark", "") + } catch (e: Exception) { + Log.w("HttpRemote", "ppt/remark json parse failed", e) + } + onRemark(pageNum, remark) + val resp = """{"result":"ok","pageNum":$pageNum}""" + val respBytes = resp.toByteArray(Charsets.UTF_8) + val out = sock.getOutputStream() + val header = buildString { + append("HTTP/1.1 200 OK\r\n") + append("Content-Type: application/json; charset=utf-8\r\n") + append("Content-Length: ${respBytes.size}\r\n") + append("Connection: close\r\n") + append("Access-Control-Allow-Origin: *\r\n") + append("\r\n") + } + out.write(header.toByteArray(Charsets.UTF_8)) + out.write(respBytes) + out.flush() + } + + private fun sendHtml(sock: Socket) { + val out = sock.getOutputStream() + val header = buildString { + append("HTTP/1.1 200 OK\r\n") + append("Content-Type: text/html; charset=utf-8\r\n") + append("Content-Length: ${statusPage.size}\r\n") + append("Connection: close\r\n") + append("Cache-Control: no-store\r\n") + append("\r\n") + } + out.write(header.toByteArray(Charset.forName("UTF-8"))) + out.write(statusPage) + out.flush() + } + + fun stop() { + try { serverSocket?.close() } catch (_: Exception) {} + serverSocket = null + thread = null + } + + companion object { + // 状态页:含「眼镜控制器」标识,方便手动扫网段定位眼镜(非必须,仅作指纹) + private const val STATUS_HTML = """眼镜控制器 + +

GlassMouse

+

PPT 备注投屏服务运行中。

+

iPhone 遥控 / 投屏请使用 GlassRemote(BLE 直连),无需本页面。

+""" + } +} diff --git a/glass-mouse/android/app/src/main/java/com/glass/mouse/MainActivity.kt b/glass-mouse/android/app/src/main/java/com/glass/mouse/MainActivity.kt new file mode 100644 index 0000000..8ec1995 --- /dev/null +++ b/glass-mouse/android/app/src/main/java/com/glass/mouse/MainActivity.kt @@ -0,0 +1,289 @@ +package com.glass.mouse + +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.provider.Settings +import android.view.View +import android.view.WindowManager +import android.util.TypedValue +import android.widget.Button +import android.widget.ScrollView +import android.widget.TextView +import android.widget.Toast +import android.app.Activity + +class MainActivity : Activity() { + + companion object { + // 供 MouseService 调用,把投屏文字显示到本 Activity(全屏、无需悬浮窗权限) + var instance: MainActivity? = null + } + + private var started = false + + // 自动滚动模式 + private val scrollHandler = Handler(Looper.getMainLooper()) + private var scrollRunnable: Runnable? = null + private var isScrollMode = false + private var scrollSpeed = 7 // 滚动速度档位 1..10(越大越快),由遥控器下发 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + instance = this + + // 权限申请:Android 13+ 通知权限(前台服务需要)+ Android 12+ 蓝牙连接权限(BLE 桥接需要) + val perms = mutableListOf() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && + checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) + != PackageManager.PERMISSION_GRANTED + ) { + perms.add(Manifest.permission.POST_NOTIFICATIONS) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && + checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) + != PackageManager.PERMISSION_GRANTED + ) { + perms.add(Manifest.permission.BLUETOOTH_CONNECT) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && + checkSelfPermission(Manifest.permission.BLUETOOTH_ADVERTISE) + != PackageManager.PERMISSION_GRANTED + ) { + perms.add(Manifest.permission.BLUETOOTH_ADVERTISE) + } + if (checkSelfPermission(Manifest.permission.CAMERA) + != PackageManager.PERMISSION_GRANTED + ) { + perms.add(Manifest.permission.CAMERA) + } + if (perms.isNotEmpty()) { + requestPermissions(perms.toTypedArray(), 100) + } + + // 悬浮窗权限(光标需要);未授权时服务仍会起、WS 仍可用,仅无光标 + findViewById