USB: phương thức getDevices()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

getDevices là phương thức của giao diện USB trả về một Promise phân giải thành một mảng các đối tượng USBDevice đại diện cho những thiết bị đã ghép đôi và đang được kết nối. Để biết thông tin về việc ghép đôi thiết bị, xem USB.requestDevice().

Cú pháp

js
getDevices()

Tham số

Không có.

Giá trị trả về

Một Promise phân giải thành một mảng các đối tượng USBDevice.

Ví dụ

Ví dụ sau ghi nhật ký tên sản phẩm và số sê-ri của các thiết bị đã ghép đôi vào console. Để biết thông tin về việc ghép đôi thiết bị, xem USB.requestDevice().

js
navigator.usb.getDevices().then((devices) => {
  console.log(`Tổng số thiết bị: ${devices.length}`);
  devices.forEach((device) => {
    console.log(
      `Tên sản phẩm: ${device.productName}, số sê-ri ${device.serialNumber}`,
    );
  });
});

Thông số kỹ thuật

Specification
WebUSB API
# dom-usb-getdevices

Tương thích trình duyệt