2026-10-22 17:21:00 +08:00

56 lines
2.7 KiB
Markdown
Raw 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.

# CP Bridge — Android 桥接 App
[atx-agent](https://github.com/openatx/atx-agent) 手机管理平台的设备端桥接应用(Kotlin + Jetpack Compose / Material 3)。
以前台服务常驻,主动连接管理平台服务器(WebSocket),把服务器指令转发到手机本地 atx-agent。
## 架构
```
管理平台服务器 (Python, :8000) ←── WebSocket ── CP Bridge (本 App) ── HTTP localhost:7912 ── atx-agent
```
## 功能(Phase 1)
- 设备注册(首条 register 消息,deviceId 取 ANDROID_ID)
- 心跳上报(10s 一次:电量 / CPU / 内存 / 存储)+ atx-agent 健康探活(30s,失联上报 `event.agent_error`)
- `shell.exec` → atx-agent `POST /shell/0`
- `device.info` → atx-agent `GET /info`(agent 离线时返回基础信息)
- 断线指数退避重连(1s → 2s → ... → 30s)
- 前台服务保活(START_STICKY + 常驻通知)+ 开机自启(可开关)+ 电池白名单引导
- 现代化 Material 3 界面:连接配置、状态仪表盘、服务控制
## 构建
用 Android Studio 打开 `android-bridge` 目录(或 `gradle assembleDebug`),minSdk 26 / targetSdk 34。
## 使用步骤
1. **手机端准备 atx-agent**(非 root,需一次性 adb):
```bash
adb push atx-agent /data/local/tmp/atx-agent
adb shell chmod 755 /data/local/tmp/atx-agent
adb shell /data/local/tmp/atx-agent server -d # 启动 atx-agent
```
2. 安装并打开 CP Bridge APK,填写服务器地址(如 `ws://192.168.1.100:8000/ws/device`)、token(服务器启用时才需要)、设备名称,保存
3. 点击「启动桥接服务」;状态卡显示「已连接服务器」即接入成功
4. 管理平台 Web 页面(http://服务器:8000)会出现该设备,可执行 Shell / 查看设备信息
> 重启手机后 atx-agent 进程会丢失,需重新 `adb shell /data/local/tmp/atx-agent server -d`(root 设备可自行加入启动脚本)。
## 模块说明
| 文件 | 职责 |
|---|---|
| `BridgeService.kt` | 前台服务:组装各模块、通知栏状态、全局运行状态 |
| `ws/BridgeWsClient.kt` | WS 客户端 + 指数退避重连 |
| `ws/Proto.kt` / `Envelope.kt` | 消息信封与协议常量(与服务器 `core/protocol.py` 对齐) |
| `handler/CommandRouter.kt` | 指令分发:shell.exec / device.info |
| `agent/AtxClient.kt` | atx-agent localhost HTTP 封装 |
| `monitor/HeartbeatMonitor.kt` | 心跳上报 + agent 探活 + CPU/内存/存储采集 |
| `MainActivity.kt` | Compose UI |
## 权限
INTERNET、FOREGROUND_SERVICE(connectedDevice)、RECEIVE_BOOT_COMPLETED、WAKE_LOCK、POST_NOTIFICATIONS、
ACCESS_NETWORK_STATE、REQUEST_IGNORE_BATTERY_OPTIMIZATIONS