Serial: requestPort() 方法

可用性有限

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

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

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

Serial.requestPort() 方法是 Serial 接口的一部分,它会向用户展示一个对话框,请求用户选择要连接的串行设备。它返回一个 Promise,该 Promise 解析后会得到一个 SerialPort 实例,代表用户选择的设备。

描述

当用户首次访问一个网站时,该网站没有权限访问任何串行设备。网站必须先调用 requestPort() 来提示用户选择允许该网站控制哪个设备。

此方法必须通过 临时激活 调用。用户必须与页面或 UI 元素进行交互,此功能才能正常工作。

语法

js
requestPort()
requestPort(options)

参数

options 可选

具有以下属性的对象:

filters 可选

这是一个包含 vendor、product 或 Bluetooth service class ID 的对象的列表,用于过滤用户可以请求连接的特定设备类型。如果未指定 filters,则用户会看到所有可用设备的列表供选择。Filters 可以包含以下值:

bluetoothServiceClassId 可选

一个无符号长整型或字符串,表示 Bluetooth 服务类 ID。这可以是 16 位或 32 位 UUID 别名、任何有效的 UUID,或来自 GATT 已分配服务键 的有效名称。

usbVendorId 可选

一个无符号短整型,用于标识 USB 设备供应商。USB Implementers Forum 为特定供应商分配 ID。

usbProductId 可选

一个无符号短整型,用于标识 USB 设备。每个供应商为其产品分配 ID。

allowedBluetoothServiceClassIds 可选

一个无符号长整型和/或字符串的列表,表示 Bluetooth 服务类 ID。具有自定义服务类 ID 的蓝牙端口将不包含在呈现给用户的端口列表中,除非该服务类 ID 包含在此列表中。无论您是否过滤列表,都会是这样。

返回值

一个 Promise,它解析为一个 SerialPort 实例。

异常

SecurityError DOMException

在以下任一情况下,返回的 Promise 会被拒绝:

  • 一个 serial 权限策略 阻止了此功能的用法。
  • 用户权限提示被拒绝。
NotFoundError DOMException

如果用户在提示时未选择端口,返回的 Promise 会因此异常而被拒绝。

示例

允许用户选择任何设备

此示例在按下 <button> 时,通过 requestPort() 提示用户选择设备。它不包含 filters,这意味着选择列表将包含所有可用设备。

html
<button id="connect">Connect</button>
js
const connectBtn = document.getElementById("connect");
connectBtn.addEventListener("click", () => {
  try {
    const port = await navigator.serial.requestPort();
    // Connect to port or add it to the list of available ports
  } catch (e) {
    // The user didn't select a device
  }
});

允许用户选择特定供应商的设备

在这种情况下,一个 filter 会被传递给 requestPort(),其中包含一个 USB vendor ID,用于将显示给用户的设备列表限制为仅限于特定制造商生产的 USB 设备。

js
connectBtn.addEventListener("click", () => {
  const usbVendorId = 0xabcd;
  try {
    const port = await navigator.serial.requestPort({ filters: [{ usbVendorId }] });
    // Connect to port or add it to the list of available ports
  } catch (e) {
    // The user didn't select a device
  }
});

允许用户选择自定义的基于 RFCOMM 的服务

尽管大多数设备通过标准化的蓝牙经典串行端口配置文件 (Serial Port Profile) 提供 SPP 通信,但有些设备使用自定义的射频通信 (RFCOMM) 服务。这些设备的服务类 ID 不在标准的蓝牙 UUID 范围内。

您需要将 allowedBluetoothServiceClassIds 列表传递给 requestPort() 才能访问这些自定义的基于 RFCOMM 的服务。

js
const myBluetoothServiceUuid = "01234567-89ab-cdef-0123-456789abcdef";

// Prompt user to select any serial port
// Access to the custom Bluetooth RFCOMM service above will be allowed
const port = await navigator.serial.requestPort({
  allowedBluetoothServiceClassIds: [myBluetoothServiceUuid],
});

您也可以在调用 requestPort() 时使用 bluetoothServiceClassId filter 键,通过按服务类 ID 标识的过滤后的蓝牙串行端口列表来提示用户。

js
const myBluetoothServiceUuid = "01234567-89ab-cdef-0123-456789abcdef";

// Prompt the user to select Bluetooth serial ports with
// the custom Bluetooth RFCOMM service above.
const port = await navigator.serial.requestPort({
  allowedBluetoothServiceClassIds: [myBluetoothServiceUuid],
  filters: [{ bluetoothServiceClassId: myBluetoothServiceUuid }],
});

规范

规范
Web Serial API
# dom-serial-requestport

浏览器兼容性