<button id="connect">Connect</button>Serial: requestPort() method / notebook
Serial: requestPort() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const connectBtn = document.getElementById("connect");
connectBtn.addEventListener("click", async () => {
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
}
});connectBtn.addEventListener("click", async () => {
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
}
});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],
});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 }],
});