Skip to main content

AdbWire

Struct AdbWire 

Source
pub struct AdbWire { /* private fields */ }
Expand description

Async ADB wire protocol client bound to a specific device.

Each method opens a fresh TCP connection to the ADB server, so this type holds no open connections. All fields are plain data, making AdbWire Send, Sync, and Clone.

Implementations§

Source§

impl AdbWire

Source

pub fn new(serial: &str) -> Self

Create a client for the given device serial (e.g. "emulator-5554", "127.0.0.1:5555", "R5CR1234567").

Source

pub fn server(self, addr: SocketAddr) -> Self

Use a custom ADB server address (default: 127.0.0.1:5037).

Source

pub fn connect_timeout(self, dur: Duration) -> Self

Set the TCP connect timeout (default: 2s).

Source

pub fn read_timeout(self, dur: Duration) -> Self

Set the read timeout for buffered methods like shell (default: 30s). For streaming methods, manage timeouts on the returned stream directly.

Source

pub fn serial(&self) -> &str

Get the device serial.

Source

pub async fn shell(&self, cmd: &str) -> Result<ShellOutput>

Run a shell command, returning separated stdout, stderr, and exit code.

let adb = AdbWire::new("emulator-5554");
let result = adb.shell("ls /sdcard").await.unwrap();
if result.success() {
    println!("{}", result.stdout_str());
} else {
    eprintln!("exit {}: {}", result.exit_code, result.stderr_str());
}
Source

pub async fn shell_stream(&self, cmd: &str) -> Result<ShellStream>

Run a shell command and return a streaming reader.

The returned ShellStream implements AsyncRead yielding stdout bytes. Use collect_output to buffer everything, or read incrementally for long-running commands.

use tokio::io::{AsyncBufReadExt, BufReader};

let adb = AdbWire::new("emulator-5554");
let stream = adb.shell_stream("logcat").await.unwrap();
let mut lines = BufReader::new(stream).lines();
while let Some(line) = lines.next_line().await.unwrap() {
    println!("{line}");
}
Source

pub async fn shell_detach(&self, cmd: &str) -> Result<()>

Send a shell command without waiting for output.

Opens a shell connection and immediately drops it. The ADB server will still deliver the command to the device, but the command may not have started on the device by the time this method returns.

Warning: Because the connection is dropped immediately, there is no guarantee the command will execute. If the device has not received the command before the TCP connection closes, it will be lost. Use shell if you need confirmation that the command ran.

Best suited for input commands (tap, swipe, keyevent) where occasional drops are acceptable.

Source

pub async fn stat(&self, remote_path: &str) -> Result<RemoteStat>

Query file metadata on the device.

Returns a RemoteStat with mode, size, and modification time. If the path does not exist, all fields will be zero — check with RemoteStat::exists().

let adb = AdbWire::new("emulator-5554");
let st = adb.stat("/sdcard/Download/test.txt").await.unwrap();
if st.exists() {
    println!("size: {} bytes, mode: {:o}", st.size, st.mode);
}
Source

pub async fn list_dir(&self, remote_path: &str) -> Result<Vec<DirEntry>>

List files in a remote directory.

Returns entries excluding . and ... Uses the sync LIST protocol.

let adb = AdbWire::new("emulator-5554");
for entry in adb.list_dir("/sdcard").await.unwrap() {
    println!("{}\t{} bytes", entry.name, entry.size);
}
Source

pub async fn pull( &self, remote_path: &str, writer: &mut (impl AsyncWrite + Unpin), ) -> Result<u64>

Pull a file from the device, writing its contents to writer.

Returns the number of bytes written.

Source

pub async fn pull_file( &self, remote_path: &str, local_path: impl AsRef<Path>, ) -> Result<u64>

Pull a file from the device to a local path.

Source

pub async fn push( &self, remote_path: &str, mode: u32, mtime: u32, reader: &mut (impl AsyncRead + Unpin), ) -> Result<()>

Push data from reader to a file on the device.

mode is the Unix file permission as an octal value (e.g. 0o644, 0o755). mtime is seconds since the Unix epoch.

Source

pub async fn push_file( &self, local_path: impl AsRef<Path>, remote_path: &str, ) -> Result<()>

Push a local file to the device with mode 0o644.

Source

pub async fn push_file_with_mode( &self, local_path: impl AsRef<Path>, remote_path: &str, mode: u32, ) -> Result<()>

Push a local file to the device with a custom Unix file mode.

let adb = AdbWire::new("emulator-5554");
adb.push_file_with_mode("run.sh", "/data/local/tmp/run.sh", 0o755).await.unwrap();
Source

pub async fn install(&self, local_apk: impl AsRef<Path>) -> Result<()>

Install an APK on the device. Equivalent to adb install <path>.

let adb = AdbWire::new("emulator-5554");
adb.install("app-debug.apk").await.unwrap();
Source

pub async fn install_with_args( &self, local_apk: impl AsRef<Path>, args: &[&str], ) -> Result<()>

Install an APK with additional pm install flags.

let adb = AdbWire::new("emulator-5554");
adb.install_with_args("app.apk", &["-r", "-d"]).await.unwrap();
Source

pub async fn forward(&self, local: &str, remote: &str) -> Result<()>

Set up port forwarding from a local socket to the device.

local and remote are ADB socket specs, e.g. "tcp:8080", "localabstract:chrome_devtools_remote".

Source

pub async fn reverse(&self, remote: &str, local: &str) -> Result<()>

Set up reverse port forwarding from the device to a local socket.

Source

pub async fn wait_for_device(&self) -> Result<()>

Wait until the device is online.

Blocks until the ADB server reports the device as available. Equivalent to adb -s <serial> wait-for-device.

let adb = AdbWire::new("192.168.1.100:5555");
adb.wait_for_device().await.unwrap();
println!("device online");
Source

pub async fn wait_for_boot(&self) -> Result<()>

Wait until the device has finished booting.

Waits for the device to come online, then polls sys.boot_completed until it reports 1.

let adb = AdbWire::new("emulator-5554");
adb.wait_for_boot().await.unwrap();
println!("device booted");

Trait Implementations§

Source§

impl Clone for AdbWire

Source§

fn clone(&self) -> AdbWire

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AdbWire

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.