Get all connected devices
- Web
- Node.js
After the user has granted permission to access the device, the next time the user visits the page, the Web app can access the device directly without asking for permission again.
Previously, we have disabled the permission system in usb
package's WebUSB implementation, so it will return all devices that are currently connected.
WebUSB USB#getDevices
method can be used to get all connected devices (that the user has granted permission to access).
USB#getDevices
method doesn't accept filters like USB#requestDevice
, so it might return non-ADB devices. Tango's AdbDaemonWebUsbDeviceManager#getDevices
accepts the same options as AdbDaemonWebUsbDeviceManager#requestDevice
and does the filtering on its own to better support usage with ADB devices.
declare class AdbDaemonWebUsbDeviceManager {
getDevices(
options?: AdbDaemonWebUsbDeviceManager.RequestDeviceOptions
): Promise<AdbDaemonWebUsbDevice[]>;
}
Basic usage:
- JavaScript
- TypeScript
const devices = await Manager.getDevices();
if (!devices.length) {
alert("No device connected");
return;
}
const device = devices[0];
import { AdbDaemonWebUsbDevice } from "@yume-chan/adb-daemon-webusb";
const devices: AdbDaemonWebUsbDevice[] = await Manager.getDevices();
if (!devices.length) {
alert("No device connected");
return;
}
const device: AdbDaemonWebUsbDevice = devices[0];
Options
The options
parameter works the same as in requestDevice
. By default, it only returns devices that match the default ADB interface.
Some lazy manufacturers use the same serialNumber
for all devices. So even if serialNumber
is specified, it's still possible that multiple devices will be returned.
To select a specific manufacturer and model:
- JavaScript
- TypeScript
const devices = await Manager.getDevices({
filters: [
{
vendorId: 0x18d1,
productId: 0x4ee2,
},
],
});
export {};
import { AdbDaemonWebUsbDevice } from "@yume-chan/adb-daemon-webusb";
const devices: AdbDaemonWebUsbDevice[] = await Manager.getDevices({
filters: [
{
vendorId: 0x18d1,
productId: 0x4ee2,
},
],
});
To allow multiple manufacturers/models:
- JavaScript
- TypeScript
const device = await Manager.getDevices({
filters: [
{
vendorId: 0x18d1,
productId: 0x4ee2,
},
{
vendorId: 0x04e8,
productId: 0x6860,
},
],
});
export {};
import { AdbDaemonWebUsbDevice } from "@yume-chan/adb-daemon-webusb";
const device: AdbDaemonWebUsbDevice | undefined = await Manager.getDevices({
filters: [
{
vendorId: 0x18d1,
productId: 0x4ee2,
},
{
vendorId: 0x04e8,
productId: 0x6860,
},
],
});