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). However, it might return non-ADB devices as well.
To only get ADB devices, AdbDaemonWebUsbDeviceManager#getDevices
method also accepts a filters
option, to only return devices that match the specified criteria.
declare class AdbDaemonWebUsbDeviceManager {
getDevices(filters?: AdbDeviceFilter[]): Promise<AdbDaemonWebUsbDevice[]>;
}
See Device filter for more information about how does the filter work and how to define a filter.
The filters
option defaults to [ADB_DEFAULT_DEVICE_FILTER]
, which matches the ADB interface. Unless your ADB devices uses a different identifier, or you want to select a specific device, you can just call AdbDaemonWebUsbDeviceManager#getDevices
without any arguments:
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.
- 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];
Examples
To select a specific manufacturer and model:
- JavaScript
- TypeScript
import { ADB_DEFAULT_DEVICE_FILTER } from "@yume-chan/adb-daemon-webusb";
const devices = await Manager.getDevices([
{
...ADB_DEFAULT_DEVICE_FILTER,
vendorId: 0x18d1,
productId: 0x4ee2,
},
]);
import { AdbDaemonWebUsbDevice, ADB_DEFAULT_DEVICE_FILTER } from "@yume-chan/adb-daemon-webusb";
const devices: AdbDaemonWebUsbDevice[] = await Manager.getDevices([
{
...ADB_DEFAULT_DEVICE_FILTER,
vendorId: 0x18d1,
productId: 0x4ee2,
},
]);
To allow multiple manufacturers/models:
- JavaScript
- TypeScript
import { ADB_DEFAULT_DEVICE_FILTER } from "@yume-chan/adb-daemon-webusb";
const device = await Manager.getDevices([
{
...ADB_DEFAULT_DEVICE_FILTER,
vendorId: 0x18d1,
productId: 0x4ee2,
},
{
...ADB_DEFAULT_DEVICE_FILTER,
vendorId: 0x04e8,
productId: 0x6860,
},
]);
import { AdbDaemonWebUsbDevice, ADB_DEFAULT_DEVICE_FILTER } from "@yume-chan/adb-daemon-webusb";
const device: AdbDaemonWebUsbDevice | undefined = await Manager.getDevices([
{
...ADB_DEFAULT_DEVICE_FILTER,
vendorId: 0x18d1,
productId: 0x4ee2,
},
{
...ADB_DEFAULT_DEVICE_FILTER,
vendorId: 0x04e8,
productId: 0x6860,
},
]);