files/en-us/web/api/serial/requestport/index.md
{{APIRef("Web Serial API")}}{{SecureContext_Header}}{{SeeCompatTable}}
The Serial.requestPort() method of the {{domxref("Serial")}} interface presents the user with a dialog asking them to select a serial device to connect to. It returns a {{jsxref("Promise")}} that resolves with an instance of {{domxref("SerialPort")}} representing the device chosen by the user.
When the user first visits a site it will not have permission to access any serial devices. A site must first call requestPort() to prompt the user to select which device the site should be allowed to control.
This method must be called via transient activation. The user has to interact with the page or a UI element in order for this feature to work.
requestPort()
requestPort(options)
options {{optional_inline}}
filters {{optional_inline}}
bluetoothServiceClassId {{optional_inline}}
usbVendorId {{optional_inline}}
usbProductId {{optional_inline}}
allowedBluetoothServiceClassIds {{optional_inline}}
A {{jsxref("Promise")}} that resolves with an instance of {{domxref("SerialPort")}}.
SecurityError {{domxref("DOMException")}}
Promise rejects with this error in either of the following situations:
NotFoundError {{domxref("DOMException")}}
Promise rejects with this exception if the user does not select a port when prompted.This example prompts the user to select a device via requestPort() when a <button> is pressed. It does not include a filter, which means that the selection list will include all available devices:
<button id="connect">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
}
});
In this case, a filter is passed to requestPort() 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
}
});
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 the allowedBluetoothServiceClassIds list to requestPort() 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 allowed
const port = await navigator.serial.requestPort({
allowedBluetoothServiceClassIds: [myBluetoothServiceUuid],
});
You can also use the bluetoothServiceClassId filter key when calling requestPort() 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}}
{{Compat}}