Termux API Skill 用于通过 SSH 控制 Android 设备,调用 Termux API 命令访问设备硬件与系统能力。
用途:在远程或脚本化环境中操作 Android 手机,例如获取设备信息、拍照、录音、定位、发送通知、收发短信、读写剪贴板、语音播报等。
前置条件:Android 设备需安装 Termux;安装 Termux:API 应用并授予权限;配置 SSH 访问(默认端口 8022);安装 termux-api 包:pkg install termux-api。
连接方式:默认 SSH 连接命令为 ssh -p 8022 <device-ip> '<termux-api-command>'。
主要能力(按类别):
- 设备信息:termux-battery-status(电量、充电状态、温度)、termux-audio-info、termux-wifi-connectioninfo、termux-wifi-scaninfo、termux-telephony-deviceinfo、termux-telephony-cellinfo、termux-sensor -l(列出传感器)、termux-sensor -s <sensor> -n 1(读取一次)。
- 摄像头与媒体:termux-camera-info(列出摄像头,id 0=后置,1=前置)、termux-camera-photo -c <id> <file.jpg>(拍照,需前台)、termux-microphone-record -f <file>(录音)、termux-media-player play <file>(播放音频)、termux-tts-speak "text"(文字转语音)、termux-tts-engines(列出 TTS 引擎)。
- 通知与反馈:termux-notification -t "title" -c "content"(显示通知)、termux-notification-remove --id <id>(移除通知)、termux-toast "message"(弹出提示)、termux-vibrate -d <ms>(震动)、termux-torch on/off(手电筒开关)、termux-dialog(显示对话框)。
- 通信(需权限):termux-sms-list -l 10(最近短信)、termux-sms-send -n <number> "message"(发送短信)、termux-contact-list(联系人)、termux-call-log -l 10(通话记录)、termux-telephony-call <number>(拨打电话)。
- 剪贴板:termux-clipboard-get(获取)、termux-clipboard-set "text"(设置)。
- 定位:termux-location(需权限)、termux-location -p gps、termux-location -p network。
- 系统控制:termux-volume(音量)、termux-brightness <0-255>(亮度)、termux-wallpaper -f <file>(壁纸)、termux-wake-lock(防休眠)、termux-wake-unlock(允许休眠)。
- 存储与分享:termux-share -a send <file>(通过 Android intent 分享文件)、termux-open <file>(用默认应用打开文件)、termux-open-url <url>(浏览器打开 URL)、termux-download <url>(下载文件)、termux-storage-get <dest>(从存储选择文件)。
常见用法示例:
- 自拍并取回:先用 adb shell am start -n com.termux/.HomeActivity 确保 Termux 在前台;ssh -p 8022 <ip> 'termux-camera-photo -c 1 ~/selfie.jpg';再用 scp -P 8022 <ip>:~/selfie.jpg /local/path/ 取回。
- 发送带动作的通知:ssh -p 8022 <ip> 'termux-notification -t "Alert" -c "Task complete" --id myalert --vibrate 200,100,200'。
- 获取位置:ssh -p 8022 <ip> 'termux-location -p network'(返回包含 latitude、longitude、accuracy 的 JSON)。
- 监控电量:ssh -p 8022 <ip> 'termux-battery-status' | jq '.percentage, .status'。
限制与注意事项:
- 摄像头/麦克风命令要求 Termux 处于前台。
- 权限需在 Android 设置 → Termux:API → 权限中授予。
- 若命令超时,可执行 termux-api-start 启动 API 服务。
故障排查:
- 命令超时:执行 termux-api-start;检查是否安装 Termux:API 应用;摄像头/麦克风需 Termux 在前台。
- 权限被拒:打开 Android 设置 → 应用 → Termux:API → 权限,授予所需权限(摄像头、定位、短信等)。
- SSH 连接被拒:在 Termux 中执行 pkg install openssh && sshd;SSH 默认端口为 8022;使用 passwd 设置密码或将 SSH 密钥添加到 ~/.ssh/authorized_keys。