Skip to content

Commit 861d367

Browse files
Add docs on SerialPort.connected and Bluetooth port event support (#37002)
* Add docs on SerialPort.connected and Bluetooth port event support * Fixes for beaufortfrancois review comments * Fixes for beaufortfrancois and wbamberg review comments
1 parent 0e2c698 commit 861d367

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

files/en-us/web/api/serialport/connect_event/index.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ browser-compat: api.SerialPort.connect_event
1010

1111
{{APIRef("Web Serial API")}}{{SecureContext_Header}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_dedicated")}}
1212

13-
The **`connect`** event of the {{domxref("SerialPort")}} interface is fired when a port has connected to the device. This event is only fired for ports associated with removable devices such as those connected via USB.
13+
The **`connect`** event of the {{domxref("SerialPort")}} interface is fired when the port connects to the device.
1414

15-
This event bubbles to the instance of {{domxref("Serial")}} that returned this interface.
15+
## Description
16+
17+
More specifically, the `connect` event fires when the port becomes **logically connected** to the device after a user grants permission for a site to access the port following a {{domxref("Serial.requestPort()")}} call:
18+
19+
- In the case of a wired serial port, this occurs when the port is physically connected to the device, for example via USB.
20+
- In the case of a wireless serial port (for example, Bluetooth RFCOMM), this occurs when the port makes one or more active connections to the device (for example via Bluetooth L2CAP channels).
21+
22+
### Bubbling
23+
24+
This event bubbles to the instance of {{domxref("Serial")}} that returned this interface. The `event.target` property refers to the {{domxref('SerialPort')}} object that bubbles up.
25+
26+
For more information, see [Event bubbling](/en-US/docs/Learn/JavaScript/Building_blocks/Event_bubbling).
1627

1728
## Syntax
1829

@@ -28,12 +39,6 @@ onconnect = (event) => {};
2839

2940
A generic {{domxref("Event")}}.
3041

31-
## Bubbling
32-
33-
This event bubbles to {{domxref("Serial")}}. The `event.target` property refers to the {{domxref('SerialPort')}} object that bubbles up.
34-
35-
For more information, see [Event bubbling](/en-US/docs/Learn/JavaScript/Building_blocks/Event_bubbling).
36-
3742
## Examples
3843

3944
### Notify when a specific port connects
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: "SerialPort: connected property"
3+
short-title: connected
4+
slug: Web/API/SerialPort/connected
5+
page-type: web-api-instance-property
6+
status:
7+
- experimental
8+
browser-compat: api.SerialPort.connected
9+
---
10+
11+
{{SecureContext_Header}}{{APIRef("Web Serial API")}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_dedicated")}}
12+
13+
The **`connected`** read-only property of the {{domxref("SerialPort")}} interface returns a boolean value that indicates whether the port is [logically connected](/en-US/docs/Web/API/SerialPort/connect_event#description) to the device.
14+
15+
## Description
16+
17+
When a wireless device goes out of range of the host, any wireless serial port opened by a web app automatically closes, even though it stays logically connected. In such cases, the web app could attempt to reopen the port using {{domxref("SerialPort.open()")}}.
18+
19+
However, if the wireless device was intentionally disconnected (for example, if the user chose to disconnect it using the operating system control panel), the web app should refrain from reopening the port to prevent reconnecting to the wireless device.
20+
21+
The following snippet shows how the `connected` property can be used to distinguish between these two cases:
22+
23+
```js
24+
const ports = await navigator.serial.getPorts();
25+
for (const port of ports) {
26+
if (port.connected) {
27+
// The port is logically connected
28+
// automatically try to reopen the port
29+
await port.open({ baudRate: 9600 });
30+
} else {
31+
// The port is not logically connected; at this point you could
32+
// prompt the user to make sure the Bluetooth device is available, and
33+
// Show a "connect" button to allow them to try opening the port if desired
34+
}
35+
}
36+
```
37+
38+
## Value
39+
40+
A boolean — `true` if the port is logically connected, and `false` if not.
41+
42+
## Examples
43+
44+
### Logging when a port is connected
45+
46+
The following snippet invokes {{domxref("Serial.requestPort()")}} when the user presses a {{htmlelement("button")}}, prompting them to choose a serial port to connect to, then logs a message to the console reporting the connection status:
47+
48+
```js
49+
requestPortButton.addEventListener("click", async () => {
50+
const port = await navigator.serial.requestPort();
51+
console.log(`Requested serial port. Connected: ${port.connected}`);
52+
});
53+
```
54+
55+
### Logging connection status on connect and disconnect
56+
57+
You can use the following snippet to log the connection status when the {{domxref("SerialPort.connect_event", "connect")}} and {{domxref("SerialPort.disconnect_event", "disconnect")}} events fire:
58+
59+
```js
60+
navigator.serial.addEventListener("connect", ({ target: port }) => {
61+
console.log(`Connect event fired. Connected: ${port.connected}`);
62+
});
63+
64+
navigator.serial.addEventListener("disconnect", ({ target: port }) => {
65+
console.log(`Disconnect event fired. Connected: ${port.connected}`);
66+
});
67+
```
68+
69+
## Specifications
70+
71+
{{Specifications}}
72+
73+
## Browser compatibility
74+
75+
{{Compat}}

files/en-us/web/api/serialport/disconnect_event/index.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ browser-compat: api.SerialPort.disconnect_event
1010

1111
{{SecureContext_Header}}{{APIRef("Web Serial API")}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_dedicated")}}
1212

13-
The **`disconnect`** event of the {{domxref("SerialPort")}} interface is fired when the port has disconnected from the device. This event is only fired for ports associated with removable devices such as those connected via USB.
13+
The **`disconnect`** event of the {{domxref("SerialPort")}} interface is fired when the port disconnects from the device.
1414

15-
This event bubbles to the instance of {{domxref("Serial")}} that returned this interface.
15+
## Description
16+
17+
More specifically, the `disconnect` event fires when a port that was previously [logically connected](/en-US/docs/Web/API/SerialPort/connect_event#description) after a user granted permission for a site to access it (following a {{domxref("Serial.requestPort()")}} call) is no longer connected.
18+
19+
### Bubbling
20+
21+
This event bubbles to the instance of {{domxref("Serial")}} that returned this interface. The `event.target` property refers to the {{domxref('SerialPort')}} object that bubbles up.
22+
23+
For more information, see [Event bubbling](/en-US/docs/Learn/JavaScript/Building_blocks/Event_bubbling).
1624

1725
## Syntax
1826

@@ -28,12 +36,6 @@ ondisconnect = (event) => {};
2836

2937
A generic {{domxref("Event")}}.
3038

31-
## Bubbling
32-
33-
This event bubbles to {{domxref("Serial")}}. The `event.target` property refers to the {{domxref('SerialPort')}} object that bubbles up.
34-
35-
For more information, see [Event bubbling](/en-US/docs/Learn/JavaScript/Building_blocks/Event_bubbling).
36-
3739
## Examples
3840

3941
### Notify when a specific port disconnects

files/en-us/web/api/serialport/index.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Instances of this interface may be obtained by calling methods of the {{domxref(
1919

2020
## Instance properties
2121

22+
- {{domxref("SerialPort.connected")}} {{ReadOnlyInline}} {{Experimental_Inline}}
23+
- : Returns a boolean value that indicates whether the port is logically connected to the device.
2224
- {{domxref("SerialPort.readable")}} {{ReadOnlyInline}} {{Experimental_Inline}}
2325
- : Returns a {{domxref("ReadableStream")}} for receiving data from the device connected to the port.
2426
- {{domxref("SerialPort.writable")}} {{ReadOnlyInline}} {{Experimental_Inline}}
@@ -42,9 +44,9 @@ Instances of this interface may be obtained by calling methods of the {{domxref(
4244
## Events
4345

4446
- {{domxref("SerialPort.connect_event", "connect")}} {{Experimental_Inline}}
45-
- : An event fired when the port has connected to the device.
47+
- : Fired when the port connects to the device.
4648
- {{domxref("SerialPort.disconnect_event", "disconnect")}} {{Experimental_Inline}}
47-
- : An event fired when the port has disconnected from the device.
49+
- : Fired when the port disconnects from the device.
4850

4951
## Examples
5052

0 commit comments

Comments
 (0)