现在移动设备很多在测试的时候都是采用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操作,请参考我的另外一篇: