From 192f8bde33f9430651b84a0ffc24f8474e9beade Mon Sep 17 00:00:00 2001 From: Miguel Angel Mulero Martinez Date: Mon, 13 May 2024 10:20:54 +0200 Subject: [PATCH] First attemp to try to make usb-serial working in Capacitor --- android/app/capacitor.build.gradle | 2 +- android/build.gradle | 6 ++++++ android/capacitor.settings.gradle | 3 +++ package.json | 1 + src/js/webSerial.js | 17 ++++++++++++++--- yarn.lock | 5 +++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/android/app/capacitor.build.gradle b/android/app/capacitor.build.gradle index fdb4970c41f..e055d9b38ff 100644 --- a/android/app/capacitor.build.gradle +++ b/android/app/capacitor.build.gradle @@ -9,7 +9,7 @@ android { apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" dependencies { - + implementation project(':usb-serial-plugin') } diff --git a/android/build.gradle b/android/build.gradle index b5eb8387da3..4363db6f389 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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' @@ -21,6 +24,9 @@ allprojects { repositories { google() mavenCentral() + maven { + url "https://maven.scijava.org/content/repositories/public/" + } } } diff --git a/android/capacitor.settings.gradle b/android/capacitor.settings.gradle index 9a5fa872e75..debb85bb3be 100644 --- a/android/capacitor.settings.gradle +++ b/android/capacitor.settings.gradle @@ -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') diff --git a/package.json b/package.json index 741a868b632..8dd9086c841 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/js/webSerial.js b/src/js/webSerial.js index 9d0317335e5..678c38780bb 100644 --- a/src/js/webSerial.js +++ b/src/js/webSerial.js @@ -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 { @@ -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(); diff --git a/yarn.lock b/yarn.lock index 92a8737fcfc..d51ce6865fb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"