Serial: requestPort() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
TheSerial.requestPort() method of theSerial interface presents the user with a dialog asking them to select a serial device to connect to. It returns aPromise that resolves with an instance ofSerialPort representing the device chosen by the user.
In this article
Description
When the user first visits a site it will not have permission to access any serial devices. A site must first callrequestPort() to prompt the user to select which device the site should be allowed to control.
This method must be called viatransient activation. The user has to interact with the page or a UI element in order for this feature to work.
Syntax
requestPort()requestPort(options)Parameters
optionsOptionalAn object with the following properties:
filtersOptionalA list of objects containing vendor, product, or Bluetooth service class IDs used to filter the specific device types made available for the user to request a connection to. If no filters are specified, the user is presented with a list of every available device to choose from. Filters can contain the following values:
bluetoothServiceClassIdOptionalAn unsigned long integer or string representing a Bluetooth service class ID. This can be a 16- or 32-bit UUID alias, any valid UUID, or a valid name from aGATT assigned services key.
usbVendorIdOptionalAn unsigned short integer that identifies a USB device vendor. TheUSB Implementors Forum assigns IDs to specific vendors.
usbProductIdOptionalAn unsigned short integer that identifies a USB device. Each vendor assigns IDs to its products.
allowedBluetoothServiceClassIdsOptionalA list of unsigned long integers and/or strings representing Bluetooth service class IDs. Bluetooth ports with custom service class IDs are excluded from the list of ports presented to the user unless the service class ID is included in this list. This is true whether you filter the list or not.
Return value
APromise that resolves with an instance ofSerialPort.
Exceptions
SecurityErrorDOMExceptionThe returned
Promiserejects with this error in either of the following situations:- A
serialPermissions Policy blocks the use of this feature. - A user permission prompt was denied.
- A
NotFoundErrorDOMExceptionThe returned
Promiserejects with this exception if the user does not select a port when prompted.
Examples
>Allow the user to select any device
This example prompts the user to select a device viarequestPort() when a<button> is pressed. It does not include a filter, which means that the selection list will include all available devices:
<button>Connect</button>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 }});Allow the user to select a specific vendor's device
In this case, a filter is passed torequestPort() with a USB vendor ID to limit the set of devices shown to the user to only USB devices built by a particular manufacturer.
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 }});Allow the user to select custom RFCOMM-based services
Although most devices expose SPP-based communication through the standardized Bluetooth Classic Serial Port Profile, some use custom radio frequency communication (RFCOMM) based services. These devices have a Service Class ID that is not in the standard Bluetooth UUID range.
You need to pass theallowedBluetoothServiceClassIds list torequestPort() to access these custom RFCOMM-based services:
const myBluetoothServiceUuid = "01234567-89ab-cdef-0123-456789abcdef";// Prompt user to select any serial port// Access to the custom Bluetooth RFCOMM service above will be allowedconst port = await navigator.serial.requestPort({ allowedBluetoothServiceClassIds: [myBluetoothServiceUuid],});You can also use thebluetoothServiceClassId filter key when callingrequestPort() to prompt the user with a list of filtered Bluetooth serial ports identified by Service Class IDs:
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 }],});Specifications
| Specification |
|---|
| Web Serial API> # dom-serial-requestport> |