using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WQ
{
public class adb
{
// https://www.jianshu.com/p/114fece11006
// https://www.lmlphp.com/user/58023/article/item/2449069/
// https://bbs.csdn.net/topics/390509349?list=1985063
// https://blog.csdn.net/qq_43547638/article/details/120645834
private string Example1()
{
Process p = new Process();
p.StartInfo.FileName = @"H:\学习\adb\adb.exe";
p.StartInfo.Arguments = "devices";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.Close();
return result;
}
private string Example2()
{
Process p = new Process();
p.StartInfo.FileName = "adb.exe";
p.StartInfo.Arguments = "devices";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.Close();
return result;
}
private string Example3()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c adb devices";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.Close();
return result;
}
/// <summary>
/// 日志打印,默认开启。true表示开启,false表示关闭。
/// </summary>
public bool Log = true;
/// <summary>
/// adb.exe的路径。默认使用Path环境变量下的adb.exe。示例:H:\adb\adb.exe
/// </summary>
public string AppPath = "adb.exe";
/// <summary>
/// 设备序列号。
/// </summary>
public string SerialNumber;
/// <summary>
/// 运行adb指令。
/// </summary>
/// <param name="cmd">adb指令。</param>
/// <returns></returns>
public string Run(string cmd)
{
cmd = cmd.Replace("adb", ""); // 将adb指令中的"adb"字符串去掉。
cmd = cmd.Trim();
if (SerialNumber != null) // 如果设置了序列号,则在指令前加上序列号。
{
cmd = cmd.Insert(0, $" ");
cmd = cmd.Insert(0, $"-s {SerialNumber}");
}
Process p = new Process();
p.StartInfo.FileName = AppPath;
p.StartInfo.Arguments = cmd;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string result = p.StandardOutput.ReadToEnd();
p.Close();
if (Log)
{
txtFile txt = new txtFile();
txt.Log(AppPath, cmd);
txt.Log(AppPath, result);
}
return result;
}
/// <summary>
/// 连接的设备列表。
/// </summary>
/// <returns></returns>
public string Devices()
{
return Run("adb devices");
}
/// <summary>
/// 连接的设备列表。
/// </summary>
/// <returns></returns>
public List<string> Devices_SN()
{
List<string> list = new List<string>();
string str = Devices();
str = str.Replace("List of devices attached", "");
str = str.Trim();
str = str.Replace("\r\n", "\t");
string[] array = str.Split('\t');
for (byte m = 0; m < array.Length; m += 2)
list.Add(array[m]);
return list;
}
/// <summary>
/// 设备状态。
/// </summary>
/// <returns></returns>
public string Device_State()
{
return Run("adb get-state");
}
/// <summary>
/// 重启。
/// </summary>
public string Reboot()
{
return Run("adb reboot");
}
/// <summary>
/// 打开飞行模式。
/// </summary>
/// <returns></returns>
public string Airplane_Open()
{
Run("adb shell settings put global airplane_mode_on 1");
return Run("adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true");
}
/// <summary>
/// 关闭飞行模式。
/// </summary>
/// <returns></returns>
public string Airplane_Off()
{
Run("adb shell settings put global airplane_mode_on 0");
return Run("adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false");
}
/// <summary>
/// 打开WiFi。
/// </summary>
public string WiFi_Open()
{
return Run("adb shell svc wifi enable");
}
/// <summary>
/// 关闭WiFi。
/// </summary>
public string WiFi_Off()
{
return Run("adb shell svc wifi disable");
}
/// <summary>
/// 打开NFC。
/// </summary>
public string NFC_Open()
{
return Run("adb shell svc nfc enable");
}
/// <summary>
/// 关闭NFC。
/// </summary>
public string NFC_Off()
{
return Run("adb shell svc nfc disable");
}
/// <summary>
/// 打开蓝牙。
/// </summary>
/// <returns></returns>
public string BT_Open()
{
return Run("adb shell svc bluetooth enable");
}
/// <summary>
/// 关闭蓝牙。
/// </summary>
/// <returns></returns>
public string BT_Off()
{
return Run("adb shell svc bluetooth disable");
}
/// <summary>
/// 打开GPS。
/// </summary>
/// <returns></returns>
public string GPS_Open()
{
return Run("adb shell settings put secure location_providers_allowed +gps,-network");
}
/// <summary>
/// 关闭GPS。
/// </summary>
/// <returns></returns>
public string GPS_Off()
{
return Run("adb shell settings put secure location_providers_allowed -gps,-network");
}
/// <summary>
/// 打开定位。
/// </summary>
/// <returns></returns>
public string Location_Open()
{
return Run("adb shell settings put secure location_mode 1");
}
/// <summary>
/// 关闭定位。
/// </summary>
/// <returns></returns>
public string Location_Off()
{
return Run("adb shell settings put secure location_mode 0");
}
/// <summary>
/// 设置亮度。
/// </summary>
/// <param name="value">亮度值。</param>
/// <returns></returns>
public string Brightness_Set(byte value)
{
return Run($"adb shell settings put system screen_brightness {value}");
}
/// <summary>
/// 获取亮度值。
/// </summary>
/// <returns></returns>
public string Brightness_Get()
{
return Run("adb shell settings get system screen_brightness");
}
/// <summary>
/// 打开亮度自动调节。
/// </summary>
/// <returns></returns>
public string Brightness_Mode_Auto()
{
return Run("adb shell settings put system screen_brightness_mode 1");
}
/// <summary>
/// 关闭亮度自动调节。
/// </summary>
/// <returns></returns>
public string Brightness_Mode_Manual()
{
return Run("adb shell settings put system screen_brightness_mode 0");
}
/// <summary>
/// 获取亮度调节模式。
/// </summary>
/// <returns></returns>
public string Brightness_Mode_Get()
{
return Run("adb shell settings get system screen_brightness_mode");
}
/// <summary>
/// 按键事件。
/// </summary>
/// <param name="keycode">键码。</param>
/// <returns></returns>
public string Keyevent(string keycode)
{
return Run($"adb shell input keyevent {keycode}");
}
/// <summary>
/// 菜单键。没有密码的情况下,可解锁屏幕。
/// </summary>
/// <returns></returns>
public string Keyevent_MENU()
{
// KEYCODE_MENU = 82
return Keyevent("KEYCODE_MENU");
}
/// <summary>
/// HOME键。
/// </summary>
/// <returns></returns>
public string Keyevent_HOME()
{
// KEYCODE_HOME = 3
return Keyevent("KEYCODE_HOME");
}
/// <summary>
/// 拨号键。来电时,可接通电话。
/// </summary>
/// <returns></returns>
public string Keyevent_CALL()
{
// KEYCODE_CALL = 5
return Keyevent("KEYCODE_CALL");
}
/// <summary>
/// 电源键。
/// </summary>
/// <returns></returns>
public string Keyevent_POWER()
{
// KEYCODE_POWER = 26
return Keyevent("KEYCODE_POWER");
}
/// <summary>
/// 音量加。
/// </summary>
/// <returns></returns>
public string Keyevent_VOLUME_UP()
{
// KEYCODE_VOLUME_UP = 24
return Keyevent("KEYCODE_VOLUME_UP");
}
/// <summary>
/// 音量减。
/// </summary>
/// <returns></returns>
public string Keyevent_VOLUME_DOWN()
{
// KEYCODE_VOLUME_DOWN = 25
return Keyevent("KEYCODE_VOLUME_DOWN");
}
/// <summary>
/// 睡眠。
/// </summary>
/// <returns></returns>
public string Keyevent_SLEEP()
{
// KEYCODE_SLEEP = 223
return Keyevent("223");
}
/// <summary>
/// 唤醒。
/// </summary>
/// <returns></returns>
public string Keyevent_WAKEUP()
{
// KEYCODE_WAKEUP = 224
return Keyevent("224");
}
/// <summary>
/// 回车键。
/// </summary>
/// <returns></returns>
public string Keyevent_ENTER()
{
// KEYCODE_ENTER = 66
return Keyevent("KEYCODE_ENTER");
}
/// <summary>
/// 屏幕状态。
/// </summary>
/// <returns></returns>
public string ScreenState()
{
return Run("adb shell dumpsys display | grep mScreenState");
}
/// <summary>
/// 点击。
/// </summary>
/// <param name="x">x坐标。</param>
/// <param name="y">y坐标。</param>
/// <returns></returns>
public string Tap(uint x, uint y)
{
return Run($"adb shell input tap {x} {y}");
}
/// <summary>
/// 滑动,默认滑动时长。
/// </summary>
/// <param name="x_start">x起点坐标。</param>
/// <param name="y_start">y起点坐标</param>
/// <param name="x_end">x终点坐标。</param>
/// <param name="y_end">y终点坐标。</param>
/// <returns></returns>
public string Swipe(uint x_start, uint y_start, uint x_end, uint y_end)
{
return Run($"adb shell input swipe {x_start} {y_start} {x_end} {y_end}");
}
/// <summary>
/// 滑动,自定义滑动时长。当xy的起点坐标和终点坐标相同时,则是长按。
/// </summary>
/// <param name="x_start">x起点坐标。</param>
/// <param name="y_start">y起点坐标</param>
/// <param name="x_end">x终点坐标。</param>
/// <param name="y_end">y终点坐标。</param>
/// <param name="time_ms">滑动时长,单位毫秒。</param>
/// <returns></returns>
public string Swipe(uint x_start, uint y_start, uint x_end, uint y_end, uint time_ms)
{
return Run($"adb shell input swipe {x_start} {y_start} {x_end} {y_end} {time_ms}");
}
/// <summary>
/// 文本。
/// </summary>
/// <param name="str">输入内容。仅支持ASCII码。</param>
/// <returns></returns>
public string Text(string str)
{
return Run($"adb shell input text {str}");
}
/// <summary>
/// 屏幕分辨率。
/// </summary>
/// <returns></returns>
public string WindowManager_size()
{
return Run("adb shell wm size");
}
}
}
C#操作adb指令
于 2023-02-24 23:10:04 首次发布