windowsC++操作ADB


在自动化生产过程中,可能会遇到各种各样的场景,比如需要通过软件取读写连在电脑上的安卓设备,那此时采用adb指令将是一种极为方便的方法,adb\adb指令、C++代码详细记录如下:

ADB(Android Debug Bridge)是一种功能强大的命令行工具,允许开发者与Android设备进行通信和交互。它是Android SDK的一部分,常用于调试应用程序、安装APK、传输文件以及执行设备命令。下面从多个方面详细介绍ADB及其指令:

一、ADB基础

1. 工作原理
  • ADB通过USB或Wi-Fi在计算机(客户端)和Android设备(守护进程adbd)之间建立通信桥梁。
  • 通信流程:客户端(如命令行)→ ADB服务器(本地进程)→ 设备上的ADB守护进程。
2. 安装与配置
  • 安装:通过Android SDK Platform-Tools安装,或直接下载独立的Platform-Tools包。
  • 配置环境变量:将platform-tools目录添加到系统PATH中,以便全局使用adb命令。
  • 安装教程网上大把的,这里就不赘述了,参考地址:https://blog.csdn.net/qq_44807940/article/details/140588328

二、常用ADB指令分类

1. 设备连接与管理
adb devices          # 列出所有连接的设备(包括模拟器)
adb kill-server      # 终止ADB服务器
adb start-server     # 启动ADB服务器
adb reconnect        # 重新连接设备
2. 文件传输
adb push <本地路径> <设备路径>  # 从电脑复制文件到设备
adb pull <设备路径> <本地路径>  # 从设备复制文件到电脑
adb shell ls <设备路径>        # 查看设备文件列表
3. 应用管理
adb install <APK路径>          # 安装APK
adb uninstall <包名>           # 卸载应用
adb shell pm list packages     # 列出所有安装的应用包名
adb shell pm clear <包名>      # 清除应用数据
4. 设备交互
adb shell input text "hello"   # 在设备上输入文本
adb shell input keyevent 26    # 按下电源键(26为电源键码)
adb shell input tap x y        # 点击屏幕坐标(x,y)
adb shell screencap <路径>     # 截图并保存到设备
adb shell screenrecord <路径>  # 录制屏幕(最长180秒)
5. 系统信息
adb shell getprop ro.product.model  # 获取设备型号
adb shell getprop ro.build.version.release  # 获取Android版本
adb shell dumpsys battery           # 获取电池信息
adb shell dumpsys wifi              # 获取Wi-Fi信息
adb reboot                          # 重启设备
6. 日志与调试
adb logcat                          # 查看系统日志
adb logcat -s <标签名>               # 过滤特定标签的日志
adb shell ps                        # 查看进程列表
adb shell top                       # 查看资源占用情况
7. 网络与端口转发
adb forward tcp:8080 tcp:8080       # 将本地端口8080转发到设备端口8080
adb reverse tcp:8080 tcp:8080       # 将设备端口8080转发到本地端口8080(Android 5.0+)
adb shell ifconfig                  # 查看网络配置

三、高级用法

1. 多设备管理

当连接多个设备时,使用-s <设备ID>指定目标设备:

adb -s emulator-5554 shell          # 对模拟器执行命令
adb -s 0123456789ABCDEF install app.apk  # 对特定设备安装APK
2. 无线ADB连接

需先通过USB连接设备,然后执行:

adb tcpip 5555                     # 开启设备的TCP/IP模式,监听5555端口
adb connect <设备IP>:5555          # 通过IP连接设备(断开USB后仍可通信)
3. 批量执行命令

使用adb shell结合循环或脚本批量操作:

adb shell "for i in $(seq 1 10); do input tap 500 500; done"  # 连续点击10次
4. ADB脚本示例
# 自动化安装并启动应用
adb install myapp.apk
adb shell am start -n com.example.app/.MainActivity

四、常见问题与解决方案

  1. 设备未被识别

    • 检查USB连接和驱动
    • 重启ADB服务器:adb kill-server && adb start-server
    • 确保设备开启USB调试模式
  2. 权限问题

    • 在设备上确认USB调试授权弹窗
    • 使用root权限:adb root(需设备已root)
  3. 无线连接失败

    • 确保设备和电脑在同一Wi-Fi网络
    • 检查防火墙设置
    • 尝试重新执行adb tcpip 5555adb connect

五、注意事项

  1. 数据安全:ADB可访问设备敏感数据,建议仅在受信任环境使用。
  2. 性能影响:长时间使用screenrecord可能导致设备发热。
  3. 兼容性:部分指令可能因设备型号或Android版本不同而有差异。

六、进一步学习资源

  • 官方文档Android Developers - ADB
  • ADB命令速查表:可在线搜索或使用第三方工具(如ADBKeyBoard)提高效率。
  • ADB下载安装
    Windows版本: https://dl.google.com/android/repository/platform-tools-latest-windows.zip
    Mac版本:https://dl.google.com/android/repository/platform-tools-latest-windows.zip
    Linux版本:https://dl.google.com/android/repository/platform-tools-latest-linux.zip

通过掌握ADB指令,开发者可以大幅提升Android应用的调试、测试和部署效率。

七、C++代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/**
 * 执行ADB命令并返回结果
 * @param command 要执行的ADB命令(不含"adb "前缀)
 * @param outputBuffer 用于存储命令输出的缓冲区
 * @param bufferSize 缓冲区大小
 * @return 成功返回0,失败返回-1
 */
int executeAdbCommand(const char *command, char *outputBuffer, size_t bufferSize) {
    if (command == NULL || outputBuffer == NULL || bufferSize < 1) {
        errno = EINVAL;
        return -1;
    }

    // 构建完整的ADB命令
    char fullCommand[1024];
   _snprintf(fullCommand, sizeof(fullCommand), "adb %s", command);
    fullCommand[sizeof(fullCommand) - 1] = '\0'; // 确保字符串终止

    FILE *pPipe = _popen(fullCommand, "rt");
    if (pPipe == NULL) {
        fprintf(stderr, "Error: Failed to execute command (errno=%d)\n", errno);
        return -1;
    }

    // 初始化输出缓冲区
    outputBuffer[0] = '\0';
    
    // 读取命令输出
    char tempBuffer[256];
    while (fgets(tempBuffer, sizeof(tempBuffer), pPipe) != NULL) {
        size_t currentLen = strlen(outputBuffer);
        size_t lineLen = strlen(tempBuffer);
        
        // 检查是否有足够空间添加新行
        if (currentLen + lineLen + 1 >= bufferSize) {
            fprintf(stderr, "Warning: Output buffer is full, truncating output\n");
            break;
        }
        
        // 追加到输出缓冲区
        strncat(outputBuffer, tempBuffer, bufferSize - currentLen - 1);
    }

    // 关闭管道并获取退出码
    int exitCode = _pclose(pPipe);
    if (exitCode == -1) {
        fprintf(stderr, "Error: Failed to close pipe (errno=%d)\n", errno);
        return -1;
    }

    // 检查ADB命令是否成功执行(通常返回0表示成功)
    return (exitCode == 0) ? 0 : -1;
}

int main(void)
{
    char output[2048];
    const char *adbCommands[] = {
        "devices",
        "shell getprop ro.product.model",
        "version",
        "invalid-command"  // 测试失败情况
    };

    for (int i = 0; i < sizeof(adbCommands) / sizeof(adbCommands[0]); i++) 
	{
        printf("Executing: adb %s\n", adbCommands[i]);
        printf("----------------------start------------------\n");
        
        if (executeAdbCommand(adbCommands[i], output, sizeof(output)) == 0)
		{
            printf("Output:\n%s\n", output);
			  printf("==0\n");
        } 
		else
		{
            printf("Failed to execute command\n");
			  printf("==-1\n");
        }
        
        printf("==================end======================\n\n");
    }

    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值