Skip to content

Commit

Permalink
First attemp to try to make usb-serial working in Capacitor
Browse files Browse the repository at this point in the history
  • Loading branch information
McGiverGim committed May 16, 2024
1 parent 10c08c9 commit 192f8bd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {

implementation project(':usb-serial-plugin')

}

Expand Down
6 changes: 6 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ buildscript {
repositories {
google()
mavenCentral()
maven {
url "https://maven.scijava.org/content/repositories/public/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:8.4.0'
Expand All @@ -21,6 +24,9 @@ allprojects {
repositories {
google()
mavenCentral()
maven {
url "https://maven.scijava.org/content/repositories/public/"
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')

include ':usb-serial-plugin'
project(':usb-serial-plugin').projectDir = new File('../node_modules/usb-serial-plugin/android')
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"short-unique-id": "^4.4.4",
"switchery-latest": "^0.8.2",
"three": "~0.97.0",
"usb-serial-plugin": "^0.0.9",
"vite-plugin-pwa": "^0.17.4",
"vue": "^2.7.10"
},
Expand Down
17 changes: 14 additions & 3 deletions src/js/webSerial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { webSerialDevices } from "./serial_devices";
import { UsbSerial } from "usb-serial-plugin";
import { Capacitor } from "@capacitor/core";

async function* streamAsyncIterable(reader, keepReadingFlag) {
try {
Expand Down Expand Up @@ -49,9 +51,18 @@ class WebSerial extends EventTarget {

async connect(options) {
this.openRequested = true;
this.port = await navigator.serial.requestPort({
filters: webSerialDevices,
});
// TODO this is only a fast way to try to see if it works, there are better ways to do it
if (Capacitor.isNativePlatform()) {
const usbDevices = await UsbSerial.connectedDevices();
if (usbDevices.length > 0) {
this.port = usbDevices[0];
UsbSerial.open( { device: usbDevices[0].device, baudRate: options.baudRate } );
}
} else {
this.port = await navigator.serial.requestPort({
filters: webSerialDevices,
});
}

await this.port.open(options);
const connectionInfo = this.port.getInfo();
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11962,6 +11962,11 @@ url@^0.11.0:
punycode "^1.4.1"
qs "^6.11.2"

usb-serial-plugin@^0.0.9:
version "0.0.9"
resolved "https://registry.yarnpkg.com/usb-serial-plugin/-/usb-serial-plugin-0.0.9.tgz#7441423072b9f08894c289bded9ff884b682b120"
integrity sha512-pmjhRU+1Mf8zvsI1ThoL4RVIDH11+gFWQACunSwN1txFbnIj4ZzTtIRlaQKAoMyzII1wLKsXbNIcAFDrcPwOuQ==

use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
Expand Down

0 comments on commit 192f8bd

Please sign in to comment.