#!/bin/bash
# ADB 命令大全 - 包含常用 Android Debug Bridge 命令及说明
# 更新时间: 2024

# ==============================================
# 服务管理命令
# ==============================================
adb start-server      # 启动 ADB 服务
adb kill-server       # 停止 ADB 服务
adb version           # 查看 ADB 版本

# ==============================================
# 设备管理命令
# ==============================================
adb devices           # 列出所有连接的设备和模拟器
adb devices -l        # 列出连接设备的详细信息，包括型号
adb connect <ip>      # 通过 IP 地址连接设备（需设备开启调试）
adb connect 193.33.31.203  # 示例：连接特定IP的设备
adb disconnect <ip>   # 断开指定IP的设备连接
adb disconnect        # 断开所有设备连接
adb -s <device_id> <command>  # 指定设备执行命令
adb -s [IP&主机名] shell       # 通过序列号明确指定目标设备

# ==============================================
# 设备状态与控制
# ==============================================
adb reboot            # 重启设备
adb reboot recovery   # 重启设备进入 recovery 模式
adb reboot bootloader # 重启设备进入 bootloader 模式
adb poweroff          # 关闭设备（部分设备可能不支持）

# ==============================================
# Shell 相关命令
# ==============================================
adb shell             # 进入设备的 shell 环境
adb shell exit        # 退出 shell 环境
adb shell su          # 获取 root 权限（需设备已 root）
adb shell ls          # 列出目录内容
adb shell cd <dir>    # 切换目录
adb shell pwd         # 显示当前目录
adb shell cat <file>  # 查看文件内容
adb shell mkdir <dir> # 创建目录
adb shell rm <file>   # 删除文件

# ==============================================
# 应用管理命令
# ==============================================
adb install <apk_path>  # 安装 APK 文件
adb install -r <apk_path>  # 覆盖安装 APK
adb install -s <apk_path>  # 安装 APK 到 SD 卡
adb uninstall <package_name>  # 卸载应用
adb uninstall -k <package_name>  # 卸载应用但保留数据和缓存
adb shell pm list packages  # 列出所有已安装应用的包名
adb shell pm list packages -3  # 列出所有第三方应用的包名
adb shell pm clear <package_name>  # 清除应用数据和缓存

# 启动应用
adb shell am start -n <package_name>/<activity_name>
adb shell am start -n com.mininglamp.mq.dmb/.ui.setting.EthSettingsActivity  # 示例：查看网络信息
adb -s 193.33.33.33 shell am start -n com.mininglamp.mq.dmb/.ui.setting.EthSettingsActivity  # 远程指定查看网络信息

# 停止应用
adb shell am force-stop <package_name>

# ==============================================
# 文件传输命令
# ==============================================
adb push <local_path> <remote_path>  # 将本地文件推送到设备
adb pull <remote_path> <local_path>  # 从设备拉取文件到本地

# ==============================================
# 屏幕操作命令
# ==============================================
# 屏幕截图
adb shell screencap -p /sdcard/screenshot.png
export CLASSPATH=/sdcard/screenshot.png
exit
exit
adb pull /sdcard/screenshot.png ./

# 录制屏幕
adb shell screenrecord /sdcard/record.mp4  # 按 Ctrl+C 停止录制
adb pull /sdcard/record.mp4 ./

# 模拟输入
adb shell input text 123456           # 安卓键入命令
adb shell input text 1111111          # 进入文件管理器可以召唤出工程模式
adb shell input keyevent KEYCODE_HOME  # 调用键盘函数，返回桌面
adb shell input keyevent KEYCODE_BACK  # 返回键
adb shell input keyevent KEYCODE_POWER  # 电源键
adb shell input tap <x> <y>            # 点击屏幕坐标(x,y)
adb shell input swipe <x1> <y1> <x2> <y2> [duration]  # 滑动屏幕

# ==============================================
# 系统信息查询
# ==============================================
adb shell getprop ro.build.version.release  # 查看 Android 版本
adb shell getprop ro.build.version.sdk      # 查看 SDK 版本
adb shell getprop ro.product.model          # 查看设备型号
adb shell wm size                          # 查看屏幕分辨率
adb shell dumpsys window displays          # 查看更详细的显示信息

# ==============================================
# 网络相关命令
# ==============================================
adb shell netstat         # 查看网络连接状态
adb shell ifconfig        # 查看网络接口信息（需设备支持）
adb shell ip addr         # 查看 IP 地址（需设备支持）
adb shell ping <ip>       # 测试网络连接

# ==============================================
# 时间同步相关
# ==============================================
adb shell settings get global ntp_server            # 验证当前NTP服务器
adb shell settings put global ntp_server huorong.bkchina.cn  # 修改NTP服务器地址
adb shell setprop persist.sys.timezone Asia/Shanghai  # 设置时区

# ==============================================
# 端口和进程管理
# ==============================================
# Windows 下检查端口占用
netstat -ano | findstr :5555  # 查找占用5555端口的进程
taskkill /F /PID <PID>         # 结束指定PID的进程，解决端口冲突

# 模拟器相关
adb emu kill                # 关闭多余的模拟器

# ==============================================
# 第三方工具集成
# ==============================================
scrcpy.exe -s 193.33.31.303  # 远程指向命令（使用scrcpy投屏工具）

# ==============================================
# 常见问题排查
# ==============================================
# 1. 设备连接后显示 offline
#    解决方法: 重启 ADB 服务
#    adb kill-server
#    adb start-server

# 2. 设备连接后无法识别
#    解决方法: 确认USB调试已开启，尝试更换USB线或端口

# 3. 端口冲突问题
#    解决方法: 关闭占用5555端口的进程或修改ADB端口
#    set ADB_SERVER_PORT=5038  # Windows下修改ADB端口
#    export ADB_SERVER_PORT=5038  # Linux/Mac下修改ADB端口