-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs on SerialPort.connected and Bluetooth port event support
- Loading branch information
1 parent
c1fd7dc
commit e8fd193
Showing
4 changed files
with
88 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: "SerialPort: connected property" | ||
short-title: connected | ||
slug: Web/API/SerialPort/connected | ||
page-type: web-api-instance-property | ||
status: | ||
- experimental | ||
browser-compat: api.SerialPort.connected | ||
--- | ||
|
||
{{SecureContext_Header}}{{APIRef("Web Serial API")}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_dedicated")}} | ||
|
||
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. | ||
|
||
## Value | ||
|
||
A boolean — `true` if the port is logically connected, and `false` if not. | ||
|
||
## Examples | ||
|
||
### Logging when a port is connected | ||
|
||
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: | ||
|
||
```js | ||
requestPortButton.onclick = async () => { | ||
const port = await navigator.serial.requestPort(); | ||
log(`Requested serial port. Connected: ${port.connected}`); | ||
}; | ||
``` | ||
|
||
### Determining whether to automatically reconnect to a Bluetooth device | ||
|
||
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()")}}. | ||
|
||
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. | ||
|
||
The following snippet shows how the `connected` property can be used to distinguish between these two cases: | ||
|
||
```js | ||
const ports = await navigator.serial.getPorts(); | ||
for (const port of ports) { | ||
if (port.connected) { | ||
// The port is logically connected | ||
// automatically try to connect to the Bluetooth device | ||
await port.open({ baudRate: 9600 }); | ||
} else { | ||
// The port is not logically connected; at this point you could | ||
// prompt the user to make sure the Bluetooth device is available, and | ||
// Show a "connect" button to allow them to try opening the port if desired | ||
} | ||
} | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters