public void test(bool rootShell, string command, params string[] args) { if (string.IsNullOrWhiteSpace(command)) { throw new ArgumentException("command is null or empty.", "command"); } if (args.Length > 0) { command = string.Format("{0} {1}", command, string.Join(" ", args)); } if (rootShell) { if (!this.HasRoot) { throw new Exception("Device does not have root permissions."); } command = string.Format("su -c \"{0}\"", command); } var x = new byte[1]; using (var socket = AdbSocket.ConnectWithService(this, true, string.Format("shell:{0}", command))) { socket.ReadStatus().ThrowOnError(); socket.ReceiveBytes(ref x, x.Length); } }
/// <summary> /// Queries the ADB server version. Only the minor version is returned. /// </summary> /// <returns></returns> public int QueryADBVersion() { using (var s = new AdbSocket(true, TransportType.any, null)) { s.SendService("host:version"); return(s.ReceiveData(true, true).FromHexToInt32()); } }
public bool Disconnect() { using (var s = AdbSocket.ConnectWithService(null, false, "host:disconnect:")) s.ReceiveData(true, true).ToString(_encoding); Thread.Sleep(500); this.UpdateDevices(); return(true); }
public void WaitForDevice(TransportType ttype, string serialNumber) { using (var s = new AdbSocket(true, ttype, serialNumber)) { s.SendService(string.Format(":wait-for-{0}", ttype)); s.ReadStatus().ThrowOnError(); // First OKAY for command received s.ReadStatus().ThrowOnError(); // Second OKAY for device connected this.UpdateDevices(); } }
public bool Connect(IPAddress address, int port) { using (var s = AdbSocket.ConnectWithService(null, true, string.Format("host:connect:{0}:{1}", address.ToString(), port))) if (s.ReceiveData(true, true).ToString(_encoding).Contains("unable to connect")) { return(false); } Thread.Sleep(500); this.UpdateDevices(); return(true); }
public void Reboot(string rebootOption) { rebootOption = rebootOption.ToLower(); using (var s = new AdbSocket(true, TransportType.any, SerialNumber)) { if (rebootOption == "normal") { rebootOption = ""; } s.SendService(string.Format("reboot:{0}", rebootOption)); s.ReceiveData(true, false); } }
public byte[] ExecuteShellCommand(bool rootShell, string command, params string[] args) { if (string.IsNullOrWhiteSpace(command)) { throw new ArgumentException("command is null or empty.", "command"); } if (args.Length > 0) { command = string.Format("{0} {1}", command, string.Join(" ", args)); } if (rootShell) { if (!this.HasRoot) { throw new Exception("Device does not have root permissions."); } command = string.Format("su -c \"{0}\"", command); } using (var socket = AdbSocket.ConnectWithService(this, true, string.Format("shell:{0}", command))) { socket.ReadStatus().ThrowOnError(); int readBytes = 0; List <byte> bytes = new List <byte>(16384); byte[] buffer = new byte[16384]; do { readBytes = socket.ReceiveBytes(ref buffer, buffer.Length); bytes.AddRange(buffer.Take(readBytes)); } while (readBytes != 0); int newLength = 0; for (int i = 0; i < bytes.Count; ++i) { bytes[newLength++] = ((i + 1) < bytes.Count && bytes[i] == 0x0D && bytes[i + 1] == 0x0A) ? bytes[++i] : bytes[i]; } return(bytes.Take(newLength).ToArray()); } }
public static AdbSocket ConnectWithService(Device device, bool startServer, string service) { var s = new AdbSocket(startServer, TransportType.any, device == null ? null : device.SerialNumber); if (!s.Connect()) { if (startServer) { throw new Exception("Could not connect to the ADB server."); } else { return(null); } } s.SendService(service); return(s); }
/* * public byte[] ExecuteShellCommand(bool rootShell, string command, params string[] args) * { * if (string.IsNullOrWhiteSpace(command)) * throw new ArgumentException("command is null or empty.", "command"); * * if (args.Length > 0) * command = string.Format("{0} {1}", command, string.Join(" ", args)); * * if (rootShell) * { * if (!this.HasRoot) * throw new Exception("Device does not have root permissions."); * * command = string.Format("su -c \"{0}\"", command); * } * * using (var socket = AdbSocket.ConnectWithService(this, true, string.Format("shell:{0}", command))) * { * socket.ReadStatus().ThrowOnError(); * * using (MemoryStream stream = new MemoryStream()) * { * int readBytes = 0; * byte[] buffer = new byte[16384]; * * do * { * readBytes = socket.ReceiveBytes(ref buffer, buffer.Length); * stream.Write(buffer, 0, readBytes); * } while (readBytes != 0); * * var newArray = stream.ToArray(); * if (newArray.Length == 0) * return newArray; * * int newLength = 0; * for (int i = 0; i < newArray.Length; ++i) * newArray[newLength++] = ((i + 1) < newArray.Length && newArray[i] == 0x0D && newArray[i + 1] == 0x0A) ? newArray[++i] : newArray[i]; * return (newArray.Length == newLength) ? newArray : newArray.Take(newLength).ToArray(); * } * } * }*/ /// <summary> /// Disconnects the device from ADB if the device is connected on the LAN. /// </summary> /// <returns>True on success; otherwise, false.</returns> public bool Disconnect() { if (ConnectionType == Android.ConnectionType.LAN) { using (var s = AdbSocket.ConnectWithService(this, false, string.Format("host:disconnect:{0}", this.SerialNumber))) { if (s != null) { string str = s.ReceiveData(true, true) .ToString(AdbSocket._encoding); Adb.Instance.UpdateDevices(); return(!str.Contains("unable")); } } } return(false); }
/// <summary> /// Updates all connected devices. /// </summary> /// <returns>ADBManager.Devices</returns> public IList <Device> UpdateDevices() { using (var s = AdbSocket.ConnectWithService(null, true, "host:devices-l")) return(UpdateDevicesInternal(s.ReceiveData(true, true).ToString(_encoding))); }
/// <summary> /// Kills the ADB server /// </summary> public void KillServer() { AdbSocket.ConnectWithService(null, false, "host:kill").Dispose(); }