From 6a0dad9dad05bbdaacaebfe3480019e54f7300f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Sat, 23 Nov 2024 15:01:14 +0100 Subject: [PATCH] feat: android support --- package.json | 1 + pnpm-lock.yaml | 8 ++++++++ src/lib/serial/device.ts | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/package.json b/package.json index 892a0adb..b05a893a 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "vite-plugin-mkcert": "^1.17.6", "vite-plugin-pwa": "^0.20.5", "vitest": "^2.1.4", + "web-serial-polyfill": "^1.0.15", "workbox-window": "^7.3.0" }, "type": "module" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a5e61fb..dd52f44f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -173,6 +173,9 @@ importers: vitest: specifier: ^2.1.4 version: 2.1.4(@types/node@20.14.10)(jsdom@25.0.1)(sass@1.80.6)(terser@5.31.1) + web-serial-polyfill: + specifier: ^1.0.15 + version: 1.0.15 workbox-window: specifier: ^7.3.0 version: 7.3.0 @@ -4060,6 +4063,9 @@ packages: resolution: {integrity: sha512-o8qghlI8NZHU1lLPrpi2+Uq7abh4GGPpYANlalzWxyWteJOCsr/P+oPBA49TOLu5FTZO4d3F9MnWJfiMo4BkmA==} engines: {node: '>=18'} + web-serial-polyfill@1.0.15: + resolution: {integrity: sha512-usZN7kGRkEWr8DzRWxW+og55L1fHo4hNIwxCSCfWKpM+i0L+2AwzupMvkDFxnJNqUFOhLaD3PlgAOJxUOUrAoA==} + webidl-conversions@4.0.2: resolution: {integrity: sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg==} @@ -8336,6 +8342,8 @@ snapshots: dependencies: xml-name-validator: 5.0.0 + web-serial-polyfill@1.0.15: {} + webidl-conversions@4.0.2: {} webidl-conversions@7.0.0: {} diff --git a/src/lib/serial/device.ts b/src/lib/serial/device.ts index 0c5cd50a..e2278359 100644 --- a/src/lib/serial/device.ts +++ b/src/lib/serial/device.ts @@ -37,6 +37,13 @@ if ( await import("./tauri-serial"); } +if (browser && navigator.serial === undefined && navigator.usb !== undefined) { + // @ts-expect-error polyfill + navigator.serial = await import("web-serial-polyfill").then( + ({ serial }) => serial, + ); +} + export async function getViablePorts(): Promise { return navigator.serial.getPorts().then((ports) => ports.filter((it) => {