Skip to main content
Version: next

Get devices

Use the AdbServerClient#getDevices method to get all connected devices.

It returns an array of AdbServerClient.Device objects, which contains the device's serial number, product name, model name, and device name, as well as the transport ID used to create a transport later.

namespace AdbServerClient {
interface Device {
serial: string;
state: AdbServerClient.ConnectionState;
product?: string;
model?: string;
device?: string;
transportId: bigint;
}
}

declare class AdbServerClient {
getDevices(): Promise<AdbServerClient.Device[]>;
}
Equivalent ADB Command
adb devices -l

serial

The serial number of the device.

USB device

For USB devices, it's the serial number in its USB descriptor.

Most of the time, each USB device has a unique serial number, but some lazy manufacturers use the same serial number for all devices, so it's possible that multiple devices with the same serial number will be returned.

Some even lazier manufacturers don't even fill in the serial number field, so it's possible that the serial number is an empty string.

If multiple devices have the same (or empty) serial number, they can't be selected by serial number. So it's recommended to always use transportId to select devices.

TCP device

For devices connected using adb connect <ip>:<port>, the serial number is the IP address and port number of the device. For example:

192.168.0.123:5555

For devices discovered by mDNS, the serial number is the device's service name. For example:

adb-12345678-ABCDEF._adb-tls-connect._tcp

For local Android emulator, the serial number is emulator-<port>. For example:

emulator-5554

For all TCP devices, their serial number might change after disconnection and reconnection.

For more information about connecting wireless devices, see Connect wireless devices.

transportId

Transport ID is a number that uniquely identify a connection between the server and the device.

It's not tied to a specific device, and will change when the device is disconnected and reconnected.

However, transport ID is the only truly unique identifier, so it's recommended to use it to select devices when calling other methods.

state

The state property indicates the connection state of the device.

type ConnectionState =
| "offline"
| "bootloader"
| "device"
| "host"
| "recovery"
| "rescue"
| "sideload"
| "unauthorized"
| "authorizing"
| "detached";

Possible values

  • "offline": The device is connected but not responding. This typically happens when a wireless device loses connection or the ADB daemon on the device is not responding.

  • "bootloader": The device is in bootloader mode (also known as fastboot mode). In this state, you can flash images and perform low-level operations, but regular ADB commands are not available.

  • "device": The device is connected, authorized, and ready for ADB commands. This is the normal operating state.

  • "host": The connection is to the ADB server itself rather than a device. This is rarely seen in normal usage.

  • "recovery": The device is in recovery mode. Similar to bootloader mode but with a different recovery system running.

  • "rescue": The device is in rescue mode, a special state for system recovery operations. In this state, the device runs minadbd (a minimal ADB daemon) instead of the normal adbd. Only a limited set of commands are available, typically used for emergency system recovery and troubleshooting.

  • "sideload": The device is in sideload mode, typically used for OTA updates via ADB. Like rescue mode, it runs minadbd instead of the normal adbd. The available commands are restricted to sideload-related operations, primarily for installing OTA update packages. Most regular ADB commands (like shell, pull, push) are not available in this state.

  • "unauthorized": The device is connected but not yet authorized. A dialog should appear on the device asking the user to authorize the computer's RSA key.

  • "authorizing": The device is in the process of being authorized. This is a transitional state between "unauthorized" and "device".

  • "detached": The device handle is not opened by the ADB server, allowing other programs to take exclusive access to the device. This state is only supported when the ADB server is using libusb to connect to USB devices. In this state, the device is physically connected but the server has released its handle, so no ADB commands can be executed until the device is reattached. For more information about USB interface exclusivity, see Exclusivity.

Filter devices by state

getDevices method accepts an optional includeStates parameter to filter devices by connection state.

It can be an array of AdbServerClient.ConnectionState:

  • "unauthorized": The device is connected but not yet authorized (a popup should appear on the device to ask for authorization)
  • "offline": The device is connected previously, but now offline (typically for wireless devices)
  • "device": The device is connected and authorized

The default value is ["unauthorized", "device"] for backward compatibility. In a future major version, the default value will be changed to ["unauthorized", "offline", "device"].

import type { AdbServerClient } from "@yume-chan/adb";

declare const client: AdbServerClient;

const devices: AdbServerClient.Device[] = await client.getDevices([
"unauthorized",
"offline",
"device",
]);

Usage

When devices with multiple states are returned, the state property can be used to determine which state each device is in.

import type { AdbServerClient } from "@yume-chan/adb";

declare const device: AdbServerClient.Device;

console.log(device.state); // "unauthorized" | "offline" | "device" | ...

Example

import type { AdbServerClient } from "@yume-chan/adb";

const devices: AdbServerClient.Device[] = await client.getDevices();
if (devices.length === 0) {
alert("No device connected");
return;
}

const device = devices[0];