diff --git a/glass-mouse/android/app/src/main/AndroidManifest.xml b/glass-mouse/android/app/src/main/AndroidManifest.xml
index 0f2dcb8..b2a5169 100644
--- a/glass-mouse/android/app/src/main/AndroidManifest.xml
+++ b/glass-mouse/android/app/src/main/AndroidManifest.xml
@@ -9,6 +9,8 @@
+
+
diff --git a/glass-mouse/android/app/src/main/java/com/glass/mouse/MouseService.kt b/glass-mouse/android/app/src/main/java/com/glass/mouse/MouseService.kt
index 06299b3..c9000ee 100644
--- a/glass-mouse/android/app/src/main/java/com/glass/mouse/MouseService.kt
+++ b/glass-mouse/android/app/src/main/java/com/glass/mouse/MouseService.kt
@@ -11,11 +11,13 @@ import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.PixelFormat
import android.graphics.drawable.GradientDrawable
+import android.media.AudioManager
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.PowerManager
+import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
@@ -220,6 +222,42 @@ class MouseService : Service() {
}
sendAckRaw(obj)
}
+ // 音量增减:delta=+1/-1,走媒体流并显示系统音量条
+ "volume" -> {
+ val ok = adjustVolume(json.optInt("delta", 1))
+ sendAck("volume", if (ok) "ok" else "failed", 0, 0)
+ }
+ // 亮度增减:delta=+1/-1,步进 32(系统亮度 0..255)
+ "brightness" -> {
+ val ok = adjustBrightness(json.optInt("delta", 1))
+ sendAck("brightness", if (ok) "ok" else "no_permission", 0, 0)
+ }
+ // 跳转「修改系统设置」授权页,供首次开启亮度调节
+ "grant_write_settings" -> {
+ var ok = false
+ try {
+ startActivity(Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS).apply {
+ data = android.net.Uri.parse("package:$packageName")
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ })
+ ok = true
+ } catch (_: Exception) {}
+ sendAck("grant_write_settings", if (ok) "ok" else "failed", 0, 0)
+ }
+ // 状态回读:iPhone 端显示当前音量/亮度与常亮状态
+ "status" -> {
+ val obj = JSONObject().apply {
+ put("type", "ack")
+ put("action", "status")
+ put("result", "ok")
+ put("volume", currentVolume())
+ put("volumeMax", maxVolume())
+ put("brightness", currentBrightness())
+ put("screenKeepOn", screenKeepOn)
+ put("writeSettings", canWriteSettings())
+ }
+ sendAckRaw(obj)
+ }
}
}
"cursor" -> {
@@ -345,15 +383,39 @@ class MouseService : Service() {
}
/**
- * 屏幕开关:通过 PowerManager.WakeLock + WindowManager.FLAG_KEEP_SCREEN_ON 控制
- * - on(true):点亮 + 保持常亮(用 FULL_WAKE_LOCK 旧 API + 新 API 的 FLAG_KEEP_SCREEN_ON)
- * - off(false):释放 lock + 移除 FLAG_KEEP_SCREEN_ON,让系统自然熄屏
- * 不需要任何权限,跨 OEM 100% 可靠
+ * 屏幕常亮:主用 FLAG_KEEP_SCREEN_ON(Android 13+ 唯一可靠的方式)。
+ *
+ * 旧实现只用 SCREEN_BRIGHT_WAKE_LOCK —— 该 flag 自 Android 13 起被系统忽略,
+ * 普通应用即使拿到锁也不会点亮/保持屏幕,这正是「常亮按钮按了没反应」的根因。
+ * 现改为双保险:
+ * ① 常驻光标悬浮窗挂 FLAG_KEEP_SCREEN_ON(切到其他 App 也保持常亮)
+ * ② MainActivity 窗口同样挂上(App 在前台时更稳)
+ * 同时保留 WakeLock 作为 Android 12 及以下的兜底(老系统该 flag 仍有效)。
*/
fun setScreenKeepOn(on: Boolean): Boolean {
return try {
+ screenKeepOn = on
+ mainHandler.post {
+ // ① 常驻悬浮窗(overlay):改 layoutParams.flags 后必须 updateViewLayout 才生效
+ layoutParams?.let { lp ->
+ lp.flags = if (on) {
+ lp.flags or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ } else {
+ lp.flags and WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON.inv()
+ }
+ try { windowManager.updateViewLayout(cursorView, lp) } catch (_: Exception) {}
+ }
+ // ② MainActivity 窗口
+ MainActivity.instance?.let { act ->
+ try {
+ if (on) act.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
+ else act.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
+ act.window.decorView.keepScreenOn = on
+ } catch (_: Exception) {}
+ }
+ }
+ // ③ 兜底:Android 12 及以下,deprecated wake lock 仍然有效
if (on) {
- // 拿 wake lock 唤醒屏幕 + 保持 CPU 唤醒(FULL_WAKE_LOCK 在 API 17+ 改 SCREEN_BRIGHT)
if (wakeLock == null) {
val pm = getSystemService(POWER_SERVICE) as PowerManager
wakeLock = pm.newWakeLock(
@@ -362,12 +424,9 @@ class MouseService : Service() {
)
}
if (wakeLock?.isHeld != true) wakeLock?.acquire(/* 24h 安全上限 */ 24L * 60 * 60 * 1000)
- screenKeepOn = true
} else {
- // 释放 wake lock → 系统按超时空闲自然熄屏
wakeLock?.let { if (it.isHeld) it.release() }
wakeLock = null
- screenKeepOn = false
}
true
} catch (_: Exception) {
@@ -375,6 +434,52 @@ class MouseService : Service() {
}
}
+ // ---- 音量 ----
+ private fun audioManager(): AudioManager? = getSystemService(AUDIO_SERVICE) as? AudioManager
+
+ private fun maxVolume(): Int =
+ audioManager()?.getStreamMaxVolume(AudioManager.STREAM_MUSIC) ?: 0
+
+ private fun currentVolume(): Int =
+ audioManager()?.getStreamVolume(AudioManager.STREAM_MUSIC) ?: -1
+
+ /** 增减媒体音量:delta=+1 加一档,-1 减一档。无需任何权限。 */
+ private fun adjustVolume(delta: Int): Boolean {
+ val am = audioManager() ?: return false
+ val dir = if (delta >= 0) AudioManager.ADJUST_RAISE else AudioManager.ADJUST_LOWER
+ return try {
+ am.adjustStreamVolume(AudioManager.STREAM_MUSIC, dir, AudioManager.FLAG_SHOW_UI)
+ true
+ } catch (_: Exception) {
+ false
+ }
+ }
+
+ // ---- 亮度 ----
+ /** 是否已获得「修改系统设置」特殊权限(API 23 以下默认有) */
+ private fun canWriteSettings(): Boolean =
+ Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.System.canWrite(this)
+
+ private fun currentBrightness(): Int =
+ try {
+ Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS)
+ } catch (_: Exception) {
+ -1
+ }
+
+ /** 增减系统亮度:步进 32,钳制在 1..255;未授权时返回 false(调用方回 no_permission) */
+ private fun adjustBrightness(delta: Int): Boolean {
+ if (!canWriteSettings()) return false
+ return try {
+ val cur = currentBrightness().let { if (it < 0) 128 else it }
+ val next = (cur + delta * 32).coerceIn(1, 255)
+ Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, next)
+ true
+ } catch (_: Exception) {
+ false
+ }
+ }
+
// ---- 光标显隐 ----
private fun showCursor() {
mainHandler.post { cursorView?.visibility = View.VISIBLE }
diff --git a/ios/GlassRemote/Sources/GlassBLEManager.swift b/ios/GlassRemote/Sources/GlassBLEManager.swift
index 7ab122c..50f01e1 100644
--- a/ios/GlassRemote/Sources/GlassBLEManager.swift
+++ b/ios/GlassRemote/Sources/GlassBLEManager.swift
@@ -31,6 +31,12 @@ final class GlassBLEManager: NSObject, ObservableObject {
@Published var clickReady: Bool = false
@Published var hasReceivedClickReadyStatus: Bool = false
@Published var lastAck: String = ""
+ // 眼镜端回读的系统状态(音量/亮度/常亮/授权),供 ControlView 展示与状态同步
+ @Published var glassVolume: Int = -1
+ @Published var glassVolumeMax: Int = 0
+ @Published var glassBrightness: Int = -1
+ @Published var glassScreenKeepOn: Bool? = nil
+ @Published var glassWriteSettings: Bool? = nil
private var central: CBCentralManager?
private var peripheral: CBPeripheral?
@@ -42,6 +48,7 @@ final class GlassBLEManager: NSObject, ObservableObject {
private let maxRetries = 3
private let cachedIdKey = "glassPeripheralId"
private var triedGapConnect = false // 本次连接周期内,已连接直连只试一次
+ private var isRecovering = false // 句柄失效自愈中,防止重复触发重连
override init() {
super.init()
@@ -160,12 +167,17 @@ final class GlassBLEManager: NSObject, ObservableObject {
// MARK: - 发送命令(JSON,与 WebSocket 完全一致)
func send(_ dict: [String: Any], withResponse: Bool = false) {
- guard let p = peripheral, let ch = writeChar else {
+ // 除了句柄存在与状态位,还要校验底层链路的真实连接态:
+ // app 在后台时链路可能已断而回调未及更新,仅凭 connectionState 会写出 invalidHandle。
+ guard let p = peripheral, let ch = writeChar, p.state == .connected else {
appendLog("未连接,丢弃命令:\(dict["type"] ?? "")")
+ // 曾就绪过却校验失败 = 链路被后台静默断开,主动重建而不是干等回调
+ if connectionState != .idle { recoverInvalidHandle() }
return
}
guard connectionState == .ready else {
appendLog("链路未就绪,丢弃命令:\(dict["type"] ?? "")")
+ if connectionState != .idle { recoverInvalidHandle() }
return
}
guard let data = try? JSONSerialization.data(withJSONObject: dict) else { return }
@@ -176,8 +188,9 @@ final class GlassBLEManager: NSObject, ObservableObject {
/// 投屏长文本分片发送:把整条 JSON 按字节切片,眼镜端 ProxyBleServer 重组后交 handleCommand。
/// 普通短命令直接走 send() 即可;本方法用于 content 可能超 BLE MTU 的场景。
func sendFragmented(_ dict: [String: Any]) {
- guard let p = peripheral, let ch = writeChar else {
+ guard let p = peripheral, let ch = writeChar, p.state == .connected else {
appendLog("未连接,丢弃分片命令")
+ if connectionState != .idle { recoverInvalidHandle() }
return
}
guard connectionState == .ready else {
@@ -227,6 +240,16 @@ final class GlassBLEManager: NSObject, ObservableObject {
func screenOn() { key("screen_on") }
func screenOff() { key("screen_off") }
+ // 音量 / 亮度:走眼镜端 sys 命令(由眼镜系统侧执行)
+ func volumeUp() { send(["type": "sys", "action": "volume", "delta": 1], withResponse: true) }
+ func volumeDown() { send(["type": "sys", "action": "volume", "delta": -1], withResponse: true) }
+ func brightnessUp() { send(["type": "sys", "action": "brightness", "delta": 1], withResponse: true) }
+ func brightnessDown() { send(["type": "sys", "action": "brightness", "delta": -1], withResponse: true) }
+ /// 打开眼镜「修改系统设置」授权页(首次调亮度需要,授予后永久有效)
+ func grantWriteSettings() {
+ send(["type": "sys", "action": "grant_write_settings"], withResponse: true)
+ }
+
/**
* 真实硬件 touch 手势:通过 BLE 发到眼镜端走 GestureDescription 通道
* - swipe(x1,y1,x2,y2,duration): 滑动(单击 x1==x2,y1==y2)
@@ -282,6 +305,20 @@ final class GlassBLEManager: NSObject, ObservableObject {
appendLog("收到眼镜端无障碍状态: \(clickReady ? "已开启" : "未开启")")
}
}
+ // 眼镜端系统状态回读(sys status 的返回)
+ if (obj["action"] as? String) == "status" {
+ if let v = obj["volume"] as? Int { glassVolume = v }
+ if let m = obj["volumeMax"] as? Int { glassVolumeMax = m }
+ if let b = obj["brightness"] as? Int { glassBrightness = b }
+ if let k = obj["screenKeepOn"] as? Bool { glassScreenKeepOn = k }
+ if let w = obj["writeSettings"] as? Bool { glassWriteSettings = w }
+ appendLog("眼镜状态:音量 \(glassVolume)/\(glassVolumeMax) · 亮度 \(glassBrightness) · 常亮 \(glassScreenKeepOn == true ? "开" : "关") · 亮度授权 \(glassWriteSettings == true ? "有" : "无")")
+ }
+ // 亮度被拒:眼镜端未授予「修改系统设置」权限
+ if (obj["action"] as? String) == "brightness", (obj["result"] as? String) == "no_permission" {
+ glassWriteSettings = false
+ appendLog("⚠️ 亮度调节被拒:眼镜端未授予「修改系统设置」权限")
+ }
}
}
@@ -318,10 +355,14 @@ extension GlassBLEManager: CBCentralManagerDelegate {
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
appendLog("✅ 已连接 ACL,准备发现服务…")
- // 只有未 ready 才显示"连接中",已 ready(重连场景)保持 ready
- if connectionState != .ready {
- connectionState = .connecting
- }
+ // 关键:无条件作废旧特征句柄并回到 .connecting。
+ // BLE 链路在后台断开重连后,旧的 CBCharacteristic 在 iOS 侧已失效,沿用旧引用
+ // 写入会返回 CBError.invalidHandle(1);而旧逻辑在重连时「保持 .ready」,会让下方
+ // 6s 超时保护(判断 connectionState != .ready)永不触发 → UI 卡在假「已就绪」,
+ // 表现为「显示已连接、但所有按键/投屏都写不进去」。
+ writeChar = nil
+ notifyChar = nil
+ connectionState = .connecting
peripheral.delegate = self
// 服务/特征发现超时保护:6s 内未就绪则断开重试(缩短避免长时间卡"连接中")
discoverTimer?.cancel()
@@ -445,10 +486,49 @@ extension GlassBLEManager: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral,
didWriteValueFor characteristic: CBCharacteristic,
error: Error?) {
- if let e = error {
- appendLog("❌ 写入失败:\(e.localizedDescription) (code:\((e as NSError).code))")
- } else {
+ guard let e = error else {
appendLog("✓ 写入 ACK 收到")
+ return
+ }
+ let ns = e as NSError
+ appendLog("❌ 写入失败:\(e.localizedDescription) (code:\(ns.code))")
+ // CBError.invalidHandle(1):句柄在链路重连后已失效,而 UI 可能仍停在「已就绪」。
+ // 主动重建链路自愈,避免「显示已连接却怎么按都没反应」。
+ if ns.domain == "CBErrorDomain", ns.code == CBError.invalidHandle.rawValue {
+ recoverInvalidHandle()
+ }
+ }
+
+ /// 前后台体检:app 从后台回到前台时调用。
+ /// BLE 链路可能在后台被系统静默断开,而 connectionState 仍停在 .ready,
+ /// 表现为界面「已就绪」但所有按键被丢弃。此处按真实链路态纠正并自动重建。
+ func healthCheck() {
+ guard connectionState == .ready else { return }
+ if peripheral == nil || peripheral?.state != .connected || writeChar == nil {
+ appendLog("🩺 前后台体检:链路已失效(UI 仍显示已就绪),自动重建…")
+ recoverInvalidHandle()
+ } else {
+ appendLog("🩺 前后台体检:链路正常")
+ }
+ }
+
+ /// 句柄失效自愈:作废旧句柄 → 断开 → 重新连接并重新发现服务
+ private func recoverInvalidHandle() {
+ guard !isRecovering else { return }
+ isRecovering = true
+ appendLog("⚠️ 检测到 BLE 句柄失效,自动重建链路…")
+ discoverTimer?.cancel()
+ writeChar = nil
+ notifyChar = nil
+ connectionState = .connecting
+ connectRetries = 0
+ if let p = peripheral, let c = central {
+ c.cancelPeripheralConnection(p)
+ }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
+ guard let self = self else { return }
+ self.isRecovering = false
+ self.beginConnect()
}
}
}
diff --git a/ios/GlassRemote/Sources/Views/ContentView.swift b/ios/GlassRemote/Sources/Views/ContentView.swift
index 3223e5b..67466f2 100644
--- a/ios/GlassRemote/Sources/Views/ContentView.swift
+++ b/ios/GlassRemote/Sources/Views/ContentView.swift
@@ -3,6 +3,7 @@ import SwiftUI
struct ContentView: View {
@EnvironmentObject var ble: GlassBLEManager
@State private var selected = 0
+ @Environment(\.scenePhase) private var scenePhase
var body: some View {
VStack(spacing: 0) {
@@ -28,6 +29,11 @@ struct ContentView: View {
}
.background(Color(.systemGroupedBackground))
.onAppear { ble.connect() }
+ // 回到前台做链路体检:后台断链后 connectionState 可能仍停在 .ready,
+ // 表现为「显示已就绪但按键全部无效」。体检发现失效会自动重建链路。
+ .onChange(of: scenePhase) { _, phase in
+ if phase == .active { ble.healthCheck() }
+ }
}
// 浅色顶栏
diff --git a/ios/GlassRemote/Sources/Views/ControlView.swift b/ios/GlassRemote/Sources/Views/ControlView.swift
index dc9c886..99e73ba 100644
--- a/ios/GlassRemote/Sources/Views/ControlView.swift
+++ b/ios/GlassRemote/Sources/Views/ControlView.swift
@@ -10,7 +10,6 @@ struct ControlView: View {
@EnvironmentObject var ble: GlassBLEManager
@AppStorage("sensitivity") private var sensitivity = 1.6
@AppStorage("cursorOn") private var cursorOn = true
- @AppStorage("screenOn") private var screenOnPref = true
@AppStorage("centerMode") private var centerMode: String = "cursor" // 默认 = 光标移动;"swipe" = 真实 touch 短滑
// 启动时强制检查:如果默认值是 swipe(v15 留下的),改回 cursor
private let didInitMode: Bool = {
@@ -30,8 +29,14 @@ struct ControlView: View {
funcIcons
gesturePadCard
bottomBar
+ avBar
+ if ble.glassWriteSettings == false { brightnessHint }
}
.padding(.horizontal, 16).padding(.top, 8).padding(.bottom, 4)
+ // 链路就绪后回读眼镜状态(音量/亮度/授权),纠正「UI 与眼镜实际状态不一致」
+ .onChange(of: ble.connectionState) { _, state in
+ if state == .ready { ble.queryStatus() }
+ }
}
// MARK: 顶部 3 个功能图标
@@ -47,11 +52,6 @@ struct ControlView: View {
cursorOn.toggle()
ble.setCursor(cursorOn)
}
- iconBtn(system: screenOnPref ? "sun.max.fill" : "moon.fill",
- label: screenOnPref ? "常亮" : "节能") {
- screenOnPref.toggle()
- ble.screenOn()
- }
iconBtn(system: centerMode == "cursor" ? "scope" : "hand.draw.fill",
label: centerMode == "cursor" ? "光标模式" : "短滑模式") {
centerMode = centerMode == "cursor" ? "swipe" : "cursor"
@@ -222,25 +222,12 @@ struct ControlView: View {
}
}
- // MARK: 底部三按钮(屏幕开/关 + 返回 + 主页 + 拍照)
+ // MARK: 底部三按钮(返回 + 主页 + 拍照)
private var bottomBar: some View {
- HStack {
- screenBtn(system: "power", label: "屏幕", isOn: screenOnPref) {
- if screenOnPref {
- ble.screenOff()
- screenOnPref = false
- } else {
- ble.screenOn()
- screenOnPref = true
- }
- }
- Spacer()
+ HStack(spacing: 0) {
circleBtn(system: "arrow.uturn.backward", label: "返回") { ble.key("back") }
- Spacer()
circleBtn(system: "house.fill", label: "主页") { ble.key("home") }
- Spacer()
circleBtn(system: "camera.fill", label: "拍照") { ble.camera() }
- Spacer()
}
.padding(.horizontal, 8).padding(.vertical, 10)
.background(Color(.secondarySystemGroupedBackground))
@@ -248,20 +235,6 @@ struct ControlView: View {
.shadow(color: .black.opacity(0.08), radius: 8, y: 2)
}
- private func screenBtn(system: String, label: String, isOn: Bool, action: @escaping () -> Void) -> some View {
- Button(action: action) {
- VStack(spacing: 4) {
- Image(systemName: system)
- .font(.title3)
- .frame(width: 48, height: 48)
- .background(isOn ? Color.yellow.opacity(0.18) : Color(.systemGroupedBackground))
- .clipShape(Circle())
- .foregroundColor(isOn ? .yellow : .primary)
- Text(label).font(.caption2).foregroundColor(.secondary)
- }
- }
- }
-
private func circleBtn(system: String, label: String, action: @escaping () -> Void) -> some View {
Button(action: action) {
VStack(spacing: 4) {
@@ -274,5 +247,53 @@ struct ControlView: View {
Text(label).font(.caption2).foregroundColor(.secondary)
}
}
+ .frame(maxWidth: .infinity)
+ }
+
+ // MARK: 音量 / 亮度 调节条
+ private var avBar: some View {
+ HStack(spacing: 0) {
+ avBtn(system: "speaker.slash.fill", label: "音量−") { ble.volumeDown() }
+ avBtn(system: "speaker.wave.3.fill", label: "音量+") { ble.volumeUp() }
+ Rectangle()
+ .fill(Color.secondary.opacity(0.22))
+ .frame(width: 1, height: 36)
+ .padding(.horizontal, 4)
+ avBtn(system: "sun.min.fill", label: "亮度−") { ble.brightnessDown() }
+ avBtn(system: "sun.max.fill", label: "亮度+") { ble.brightnessUp() }
+ }
+ .padding(.horizontal, 10).padding(.vertical, 8)
+ .background(Color(.secondarySystemGroupedBackground))
+ .clipShape(RoundedRectangle(cornerRadius: 18))
+ .shadow(color: .black.opacity(0.08), radius: 8, y: 2)
+ }
+
+ private func avBtn(system: String, label: String, action: @escaping () -> Void) -> some View {
+ Button(action: action) {
+ VStack(spacing: 3) {
+ Image(systemName: system)
+ .font(.title3)
+ .frame(width: 42, height: 42)
+ .background(Color(.systemGroupedBackground))
+ .clipShape(Circle())
+ .foregroundColor(.primary)
+ Text(label).font(.caption2).foregroundColor(.secondary)
+ }
+ }
+ .frame(maxWidth: .infinity)
+ }
+
+ /// 亮度未授权时的引导条:点一下跳到眼镜的「修改系统设置」页
+ private var brightnessHint: some View {
+ Button(action: { ble.grantWriteSettings() }) {
+ HStack(spacing: 5) {
+ Image(systemName: "exclamationmark.triangle.fill").font(.caption2)
+ Text("亮度需先授予「修改系统设置」,点此前往").font(.caption2)
+ }
+ .foregroundColor(.orange)
+ .padding(.vertical, 7).padding(.horizontal, 12)
+ .background(Color.orange.opacity(0.12))
+ .clipShape(Capsule())
+ }
}
}
\ No newline at end of file