Skip to content

Added support for Web Serial API on Chromebooks #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
# arduino-create-agent-js-client
JS module providing discovery of the [Arduino Create Agent](https://github.com/arduino/arduino-create-agent) and communication with it


## Changelog
[2.8.0] - 2022-03-21

### Added
- Added support (still in Beta) for Chrome's Web Serial API on ChromeOS.
Other operating systems should not be affected.

## Installation

```bash
Expand Down
19 changes: 17 additions & 2 deletions demo/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import V2 from './v2/v2.jsx';

const chromeExtensionID = 'hfejhkbipnickajaidoppbadcomekkde';

const isChromeOs = () => window.navigator.userAgent.indexOf(' CrOS ') !== -1;

const scrollToBottom = (target) => {
if (target) {
target.scrollTop = target.scrollHeight; // eslint-disable-line no-param-reassign
Expand Down Expand Up @@ -151,6 +153,17 @@ class App extends React.Component {
}
}

requestDevicePermission = () => {
if ('serial' in navigator) {
navigator.serial.requestPort([{ usbVendorId: 0x2341 }]).then((port) => {
daemon.devicesList.next({
serial: [port],
network: []
});
});
}
};

showError(err) {
this.setState({ error: err });
scrollToBottom(document.body);
Expand Down Expand Up @@ -275,8 +288,10 @@ class App extends React.Component {
</div>

<div className="section">
<h2>Connected Devices</h2>

<div>
<h2 style={{ display: 'inline-block', marginRight: 10 } }>Connected Devices </h2>
{ isChromeOs() && <button type="button" onClick={() => this.requestDevicePermission()}>Request access to serial port</button> }
</div>
<strong>serial:</strong>
<ul>
{ listSerialDevices }
Expand Down
14 changes: 8 additions & 6 deletions demo/v2/install_tool.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export class V2InstallTool extends React.Component {
componentDidMount() {
this.daemon = this.props.daemon;

this.daemon.agentV2Found.subscribe(daemonV2 => {
if (!daemonV2) {
return;
}
this.daemonV2 = daemonV2;
});
if (this.daemon.agentV2Found) { // agentV2Found not available for instance on chromebooks
this.daemon.agentV2Found.subscribe(daemonV2 => {
if (!daemonV2) {
return;
}
this.daemonV2 = daemonV2;
});
}
}

handleChange(event) {
Expand Down
20 changes: 11 additions & 9 deletions demo/v2/v2.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ class V2 extends React.Component {
componentDidMount() {
this.daemon = this.props.daemon;

this.daemon.agentV2Found.subscribe(daemonV2 => {
if (!daemonV2) {
return;
}
this.daemonV2 = daemonV2;
this.daemonV2.installedTools().then(res => {
this.setState({
tools: res
if (this.daemon.agentV2Found) { // agentV2Found not available for instance on chromebooks
this.daemon.agentV2Found.subscribe(daemonV2 => {
if (!daemonV2) {
return;
}
this.daemonV2 = daemonV2;
this.daemonV2.installedTools().then(res => {
this.setState({
tools: res
});
});
});
});
}
}

render() {
Expand Down
Loading