USBEndpoint

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

USBEndpoint 接口(WebUSB API 的一部分)提供有关 USB 设备提供的端点的信息。端点表示进入或离开设备的单向数据流。

构造函数

USBEndpoint() 实验性

创建一个新的 USBEndpoint 对象,该对象将使用提供的 USBAlternateInterface 对象、给定的端点编号和传输方向的信息进行填充。

实例属性

USBEndpoint.endpointNumber 实验性

返回此端点的“端点编号”,该编号是从定义此端点的端点描述符的 bEndpointAddress 字段中提取的值(1 到 15)。调用 USBDevice 上的方法时使用此值来标识端点。

USBEndpoint.direction 实验性

返回此端点传输数据的方向,以下之一:

  • "in" - 数据从设备传输到主机。
  • "out" - 数据从主机传输到设备。
USBEndpoint.type 实验性

返回此端点的类型,以下之一:

  • "bulk" - 提供大数据块的可靠数据传输。通过批量端点发送的数据保证会被传递或生成错误,但可能会被其他数据流量抢占。
  • "interrupt" - 提供小数据块的可靠数据传输。通过中断端点发送的数据保证会被传递或生成错误,并且还享有专用的总线传输时间。
  • "isochronous" - 提供必须定期传输的数据块的不可靠数据传输。它们享有专用的总线时间,但如果错过截止日期,数据将被丢弃。
USBEndpoint.packetSize 实验性

返回通过此端点发送的数据将被分割成的包的大小。

示例

虽然开发人员有时会提前知道设备端点的确切布局,但在某些情况下必须在运行时发现。例如,USB 串行设备必须提供批量输入和输出端点,但它们的端点编号将取决于设备提供的其他接口。

此代码通过查找实现 USB CDC 接口类的接口,然后根据它们的类型和方向识别候选端点来识别正确的端点。

js
let inEndpoint = undefined;
let outEndpoint = undefined;

for (const { alternates } of device.configuration.interfaces) {
  // Only support devices with out multiple alternate interfaces.
  const alternate = alternates[0];

  // Identify the interface implementing the USB CDC class.
  const USB_CDC_CLASS = 10;
  if (alternate.interfaceClass !== USB_CDC_CLASS) {
    continue;
  }

  for (const endpoint of alternate.endpoints) {
    // Identify the bulk transfer endpoints.
    if (endpoint.type !== "bulk") {
      continue;
    }

    if (endpoint.direction === "in") {
      inEndpoint = endpoint.endpointNumber;
    } else if (endpoint.direction === "out") {
      outEndpoint = endpoint.endpointNumber;
    }
  }
}

规范

规范
WebUSB API
# usbendpoint-interface

浏览器兼容性