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 會被拒絕:

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

瀏覽器相容性