Skip to main content

Create device manager

WebUSB provides APIs for discovering and communicating with USB devices. The AdbDaemonWebUsbDeviceManager class wraps the WebUSB API to only work with ADB devices.

Usage

The usage of AdbDaemonWebUsbDeviceManager is different in Node.js and Web browsers:

Chromium-based browsers support WebUSB natively. Chrome for Android is supported, but Chrome for iOS is based on Safari and not supported.

Electron Support
Secure Context Required

WebUSB API requires a Secure Context. Basically it means the page must be served over HTTPS or localhost.

  • If you have a domain name, you can easily get a free SSL certificate from Let's Encrypt.
  • If you only have an IP address, you can create a self-signed certificate and trust it in every device that will access your server (or ignore the warning every time).

Create a device manager for browsers:

import { AdbDaemonWebUsbDeviceManager } from "@yume-chan/adb-daemon-webusb";

const Manager: AdbDaemonWebUsbDeviceManager | undefined = AdbDaemonWebUsbDeviceManager.BROWSER;
info

AdbDaemonWebUsbDeviceManager.BROWSER is a shorthand of:

navigator.usb ? new AdbDaemonWebUsbDeviceManager(navigator.usb) : undefined

In two cases, Manager may be undefined:

  1. Current page isn't in a Secure Context
  2. Current browser does not support WebUSB API

It's your responsibility to make sure the first case doesn't happen. So there is only one reason left:

if (!Manager) {
alert("WebUSB is not supported in this browser");
return;
}