adb命令发送程序[C#]

现在移动设备很多在测试的时候都是采用ADB的方式进行通信,所以非常有必要了解Adb指令怎么发送控制命令的。

A. 首先初始化Adb:

        public bool AdbConnectInit()
        {
            string strRet = AdbSendCmdEx("adb.exe", "devices", 10000, true, false, "");
            if (!strRet.Contains("List of devices attached"))
            {
                MessageBox.Show(String.Format("Send command \"adb devices\" and get result: {0}  fail.", strRet));
                return false;
            }
            return true;
        }

B. 发送和接收命令:
        public string AdbSendCmdEx(string exeName, string command, int timeoutMiliSecForEveryCommand,
                                   bool NeedReturn, bool SaveDebuglog, string DebugLogPath)
        {
            lock (FileConfig.locker)
            {
                string adbworkdir = System.Environment.CurrentDirectory + "\\adb\\";
                ProcessStartInfo info = new ProcessStartInfo("adb.exe");
                info.UseShellExecute = false;
                info.WorkingDirectory = adbworkdir;
                info.FileName = adbworkdir + exeName;
                info.Arguments = command;
                info.RedirectStandardInput = false;
                info.RedirectStandardOutput = NeedReturn;
                info.RedirectStandardError = NeedReturn;
                info.CreateNoWindow = true;
                string returnValue = "";

                using (Process process = Process.Start(info))
                {
                    int resttime = timeoutMiliSecForEveryCommand;
                    do
                    {
                        if (process.HasExited == true || !NeedReturn)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                        resttime = resttime - 100;

                    } while (resttime > 0);
                    if (NeedReturn)
                    {
                        returnValue = process.StandardOutput.ReadToEnd();
                        returnValue += process.StandardError.ReadToEnd();
                        returnValue = returnValue.Replace("\n", "");
                        returnValue = returnValue.Replace("\r", "");
                    }
                    if (process.HasExited == false && NeedReturn)
                    {
                        process.Kill();
                        return returnValue;
                    }
                    if (SaveDebuglog)
                    {
                        DateTime CurrTime = DateTime.Now;
                        string LogFile = String.Format("{0}\\ADB_{1}{2:D2}{3:D2}.log", DebugLogPath, CurrTime.Year, CurrTime.Month, CurrTime.Day);
                        StreamWriter sw = new StreamWriter(LogFile, true);
                        if (command == "wait-for-device")
                        {
                            sw.WriteLine("----------------ADB Start---------------");
                        }
                        sw.Close();
                        sw.Dispose();
                        sw = null;
                    }
                }
                return returnValue;
            }
        }
        

C. 调用Demo:


        public override bool adbPushFileTo8019(int DutIndex, string clientIP, int clientPort = 6776)
        {
            string command = string.Format("connect {0}:{1}", clientIP, clientPort);
            string passValue = string.Format("connected to {0}:{1}", clientIP, clientPort);
            ShowMessage("Adb Send:" + command);

            int nCmd = 0;
            string strOut = "";
            for (nCmd = 0; nCmd < 10; nCmd++)
            {
                strOut = AdbSendCmdEx("adb.exe", command, 1200, true, false, "");
                ShowMessage(String.Format("Adb Receive:[No.{0}] - {1}", nCmd, strOut));
                if (strOut.IndexOf(passValue) != -1)
                {
                    break;
                }
                strOut = "";
            }
            if (10 == nCmd)
            {
                ShowMessage("adb connect fail!");
                return false;
            }
            ShowMessage("adb connect pass");

            /// ---- Adb connect的时候已经连接设备了,所以不用重复连接
                         
            command = string.Format("-s {0}:{1} push ", clientIP, clientPort) + adbworkdir + string.Format("{0}\\wifi_socket_config.ini /sdcard/", DutIndex);
         
            ShowMessage("Adb Send:" + command);
            string strRet = AdbSendCmdEx("adb.exe", command, 3000, true, false, "");
            ShowMessage("Adb Receive:" + strRet);
            if (!strRet.ToUpper().Contains("BYTES"))
            {
                ShowMessage("push wifi_socket_config.ini fail!");
                return false;
            }
            command = string.Format("-s {0}:{1} ", clientIP, clientPort)+"shell dumpsys engineer --execute_engineer_order -e order *#8019#";
            ShowMessage("Adb Send:" + command);
            strRet = AdbSendCmdEx("adb.exe", command, 3000, true, false, "");
            ShowMessage("Adb Receive:" + strRet);
            if (!strRet.ToUpper().Contains("OK"))
            {
                ShowMessage("execute_engineer 8019 fail!");
                return false;
            }

            string strIpLine = ReadWifiSocketConfigFileAndReturnIpAddr(String.Format("{0}\\adb\\{1}\\wifi_socket_config.ini",
                                                                      System.Environment.CurrentDirectory, DutIndex));
            string[] strIpInfo = strIpLine.Split('/');  // strIpInfo包含Ip地址和端口号
            if (strIpInfo.Length <= 0)
            {
                return false;
            }


            int dwTime = 1000;
            string cmd8019 = string.Format("connect {0}:6776", strIpInfo[0]);
            do
            {
                string strOutput = AdbSendCmdEx("adb.exe", cmd8019, 1000, true, false, "");
                ShowMessage(String.Format("Adb Receive[For Running]:{0}", strOutput));
                if (strOutput.Contains("connected"))
                {
                    ShowMessage(String.Format("Adb Receive[Run Success]:{0}", strOutput));
                    break;
                }
                Thread.Sleep(100);
                dwTime -= 100;
            } while (dwTime > 0);
            if (dwTime <= 0)
            {
                ShowMessage(String.Format("Adb Send commadn:{0} timeout.", cmd8019));
                return false;
            }
            return true;
        }
       

关于C++ 相关的Adb操作,请参考我的另外一篇:

adb命令读/写操作:可以嵌入到代码中执行-C++文档类资源-CSDN下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值