Skip to content

Commit 1a9a3cb

Browse files
authored
Added partial support for Web Serial API on Chromebooks (#544)
Added partial support for Web Serial API on Chromebooks.
1 parent 0cc287b commit 1a9a3cb

15 files changed

+3281
-8518
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
# arduino-create-agent-js-client
55
JS module providing discovery of the [Arduino Create Agent](https://github.com/arduino/arduino-create-agent) and communication with it
66

7+
8+
## Changelog
9+
[2.8.0] - 2022-03-21
10+
11+
### Added
12+
- Added support (still in Beta) for Chrome's Web Serial API on ChromeOS.
13+
Other operating systems should not be affected.
14+
715
## Installation
816

917
```bash

demo/app.jsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import V2 from './v2/v2.jsx';
2727

2828
const chromeExtensionID = 'hfejhkbipnickajaidoppbadcomekkde';
2929

30+
const isChromeOs = () => window.navigator.userAgent.indexOf(' CrOS ') !== -1;
31+
3032
const scrollToBottom = (target) => {
3133
if (target) {
3234
target.scrollTop = target.scrollHeight; // eslint-disable-line no-param-reassign
@@ -151,6 +153,17 @@ class App extends React.Component {
151153
}
152154
}
153155

156+
requestDevicePermission = () => {
157+
if ('serial' in navigator) {
158+
navigator.serial.requestPort([{ usbVendorId: 0x2341 }]).then((port) => {
159+
daemon.devicesList.next({
160+
serial: [port],
161+
network: []
162+
});
163+
});
164+
}
165+
};
166+
154167
showError(err) {
155168
this.setState({ error: err });
156169
scrollToBottom(document.body);
@@ -275,8 +288,10 @@ class App extends React.Component {
275288
</div>
276289

277290
<div className="section">
278-
<h2>Connected Devices</h2>
279-
291+
<div>
292+
<h2 style={{ display: 'inline-block', marginRight: 10 } }>Connected Devices </h2>
293+
{ isChromeOs() && <button type="button" onClick={() => this.requestDevicePermission()}>Request access to serial port</button> }
294+
</div>
280295
<strong>serial:</strong>
281296
<ul>
282297
{ listSerialDevices }

demo/v2/install_tool.jsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ export class V2InstallTool extends React.Component {
2121
componentDidMount() {
2222
this.daemon = this.props.daemon;
2323

24-
this.daemon.agentV2Found.subscribe(daemonV2 => {
25-
if (!daemonV2) {
26-
return;
27-
}
28-
this.daemonV2 = daemonV2;
29-
});
24+
if (this.daemon.agentV2Found) { // agentV2Found not available for instance on chromebooks
25+
this.daemon.agentV2Found.subscribe(daemonV2 => {
26+
if (!daemonV2) {
27+
return;
28+
}
29+
this.daemonV2 = daemonV2;
30+
});
31+
}
3032
}
3133

3234
handleChange(event) {

demo/v2/v2.jsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ class V2 extends React.Component {
1212
componentDidMount() {
1313
this.daemon = this.props.daemon;
1414

15-
this.daemon.agentV2Found.subscribe(daemonV2 => {
16-
if (!daemonV2) {
17-
return;
18-
}
19-
this.daemonV2 = daemonV2;
20-
this.daemonV2.installedTools().then(res => {
21-
this.setState({
22-
tools: res
15+
if (this.daemon.agentV2Found) { // agentV2Found not available for instance on chromebooks
16+
this.daemon.agentV2Found.subscribe(daemonV2 => {
17+
if (!daemonV2) {
18+
return;
19+
}
20+
this.daemonV2 = daemonV2;
21+
this.daemonV2.installedTools().then(res => {
22+
this.setState({
23+
tools: res
24+
});
2325
});
2426
});
25-
});
27+
}
2628
}
2729

2830
render() {

0 commit comments

Comments
 (0)