Skip to main content
Version: 2.1.0

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.

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.

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];

Filter devices by state

getDevices method also accepts an optional includeStates parameter, to filter devices by 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)
  • "offline": the device is connected previously, but now offline (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",
]);

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"