栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

adb命令发送程序[C#]

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985731.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号