Skip to content

Add ADB track-devices support for efficient device monitoring #12073

Description

@rmarinho

Context

The VS Code MAUI extension currently uses a C# ServiceHub (Microsoft.VisualStudio.Maui.Devices.AndroidDeviceManager) for real-time device connect/disconnect detection. This works via the ADB host:track-devices-l TCP protocol, which provides push-based notifications over a persistent socket connection to the ADB server.

We are migrating Android operations from the ServiceHub to the MAUI DevTools CLI tool (maui command). As an interim solution, the extension now polls maui device list --json every 3 seconds and diffs the results to detect device changes. This works but is less efficient than a native event-driven approach.

Proposal

Add an ADB track-devices based device monitoring capability to the Xamarin.Android.Tools.AndroidSdk package, so the MAUI DevTools CLI can offer a streaming device watch command.

What exists today

  • AdbRunner.ListDevicesAsync() — one-shot device enumeration via adb devices -l
  • AdbDeviceInfo — device model with serial, status, type, model, etc.

What's needed

  1. AdbRunner.WatchDevicesAsync() (or similar) — a method that opens a persistent connection to the ADB server using the host:track-devices-l protocol and yields device change events as they occur
  2. The method should:
    • Connect to ADB server TCP socket (default localhost:5037)
    • Send host:track-devices-l command
    • Parse the continuous stream of device status lines
    • Emit structured events: device added, device removed, device state changed
    • Support CancellationToken for clean shutdown
    • Handle ADB server restarts gracefully (reconnect with backoff)

Reference implementation

The ServiceHub's AdbdClient.cs already implements this pattern:

csharp public async Task WatchDevicesAsync(CancellationToken ct, Func<DeviceInfo, Task> handle) { await SendCommandAsync("host:track-devices-l", ct); while (true) { var reply = await ReadNextReplyAsync(ct); // Parse: "SERIAL STATE [key:value ...]" var d = new DeviceInfo(serial, state, product, model, device, transportId); handle?.Invoke(d); } }

CLI integration

Once available, the MAUI DevTools CLI would expose:
�ash maui device watch --platform android --json
Emitting newline-delimited JSON (NDJSON) events:
json {"event":"initial","devices":[...]} {"event":"added","device":{...}} {"event":"removed","identifier":"emulator-5554"} {"event":"changed","device":{...},"previous_state":"offline","new_state":"device"}

Why not just poll?

ApproachLatencyCPU/IO costBattery impact
Polling 3s0–3s delaySpawns adb devices every 3sHigher
track-devicesNear-instantSingle persistent connectionMinimal

Polling works as a v1 but the track-devices protocol is the proper long-term solution used by Android Studio, VS, and all major IDEs.

Related

  • MAUI DevTools CLI: dotnet/maui branch maui-client-tool-android
  • VS Code extension: dotnet/vscode-maui
  • Interim polling implementation: MauiDevicePoller.ts in vscode-maui

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    android-toolsIssues migrated from dotnet/android-tools / relates to the xamarin-android-tools subtreeneeds-triageIssues that need to be assigned.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions