@senxor/capacitor-serial 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # @senxor/capacitor-serial
2
+
3
+ `@senxor/capacitor-serial` is the Capacitor transport for Senxor.js. It lets you discover and connect Senxor devices from a Capacitor application (for example, an Android app running inside a WebView) and gives you ready-to-use `Senxor` instances from `@senxor/core`.
4
+
5
+ Use this package together with:
6
+
7
+ - `@senxor/core` – provides the `Senxor` device controller and processing helpers.
8
+ - `capacitor-serial-plugin` – a native Capacitor plugin that exposes USB serial ports.
9
+
10
+ ## Installation
11
+
12
+ In your Capacitor app project, install the JavaScript dependencies:
13
+
14
+ ```bash
15
+ pnpm add @senxor/core @senxor/capacitor-serial
16
+ pnpm add capacitor-serial-plugin
17
+ ```
18
+
19
+ Then run Capacitor sync so the native project picks up the plugin:
20
+
21
+ ```bash
22
+ npx cap sync
23
+ ```
24
+
25
+ ## Android configuration
26
+
27
+ On Android you must ensure that:
28
+
29
+ - The JitPack Maven repository is available so the native plugin can be resolved.
30
+ - USB permissions and device filters are correctly declared in your manifest.
31
+
32
+ ### JitPack repository
33
+
34
+ In your Android project-level Gradle file (for example, `android/build.gradle` in a standard Capacitor project), add the JitPack repository to the list of repositories:
35
+
36
+ ```gradle
37
+ allprojects {
38
+ repositories {
39
+ google()
40
+ mavenCentral()
41
+ maven { url 'https://jitpack.io' }
42
+ }
43
+ }
44
+ ```
45
+
46
+ If your project uses `dependencyResolutionManagement` instead of `allprojects`, add the same `maven { url 'https://jitpack.io' }` entry to the appropriate repositories block.
47
+
48
+ ### USB permissions and filters
49
+
50
+ Your Android manifest must declare USB permissions and, typically, a device filter so that the system knows which USB devices should trigger your app. A minimal configuration looks similar to:
51
+
52
+ ```xml
53
+ <uses-permission android:name="android.permission.USB_PERMISSION" />
54
+ <uses-feature
55
+ android:name="android.hardware.usb.host"
56
+ android:required="true" />
57
+ ```
58
+
59
+ Inside your main activity, you can also declare an intent filter and metadata for USB device attachment, for example:
60
+
61
+ ```xml
62
+ <activity
63
+ android:name=".MainActivity"
64
+ android:exported="true">
65
+
66
+ <intent-filter>
67
+ <action android:name="android.intent.action.MAIN" />
68
+ <category android:name="android.intent.category.LAUNCHER" />
69
+ <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
70
+ </intent-filter>
71
+
72
+ <meta-data
73
+ android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
74
+ android:resource="@xml/device_filter" />
75
+ </activity>
76
+ ```
77
+
78
+ The exact requirements may vary depending on the version of `capacitor-serial-plugin`. Always refer to that plugin's documentation for the most up-to-date Android configuration details and sample `device_filter` definitions.
79
+
80
+ ## Quick start
81
+
82
+ Once your Capacitor project is configured and the native plugin is installed, you can use `@senxor/capacitor-serial` from your web code to discover Senxor devices and work with `Senxor` instances.
83
+
84
+ ```ts
85
+ import {
86
+ listCapacitorSerialSenxors,
87
+ onCapacitorSerialSenxorConnect,
88
+ } from "@senxor/capacitor-serial";
89
+ import {
90
+ nomalizeSenxorData,
91
+ applyColorMap,
92
+ type SenxorData,
93
+ } from "@senxor/core";
94
+
95
+ async function initDevices() {
96
+ const devices = await listCapacitorSerialSenxors();
97
+
98
+ for (const senxor of devices) {
99
+ await senxor.open();
100
+
101
+ senxor.onError((error) => {
102
+ console.error("Senxor error:", error);
103
+ });
104
+
105
+ senxor.onData((data: SenxorData) => {
106
+ const normalized = nomalizeSenxorData(data);
107
+ const image = applyColorMap(normalized, "rainbow2");
108
+ // Render `image` to a canvas or process it further
109
+ });
110
+
111
+ await senxor.startStreaming();
112
+ }
113
+
114
+ // Optionally, listen for new devices being connected while the app is running
115
+ onCapacitorSerialSenxorConnect(async (senxor) => {
116
+ await senxor.open();
117
+ await senxor.startStreaming();
118
+ });
119
+ }
120
+ ```
121
+
122
+ The `Senxor` instances you receive here behave the same way as in other transports. For more details about the `Senxor` API and available processors, see the `@senxor/core` README.
123
+
124
+ ## API
125
+
126
+ ### `listCapacitorSerialSenxors()`
127
+
128
+ ```ts
129
+ import { listCapacitorSerialSenxors } from "@senxor/capacitor-serial";
130
+
131
+ const devices = await listCapacitorSerialSenxors();
132
+ ```
133
+
134
+ Returns an array of `Senxor` instances for Senxor devices the native serial layer currently reports as connected. Use this at app startup or whenever you want to refresh the device list. Call `open()` on each instance when you are ready to talk to the device.
135
+
136
+ ### `onCapacitorSerialSenxorConnect(listener)`
137
+
138
+ ```ts
139
+ import { onCapacitorSerialSenxorConnect } from "@senxor/capacitor-serial";
140
+
141
+ const unsubscribe = onCapacitorSerialSenxorConnect((senxor) => {
142
+ // A new Senxor device has been connected
143
+ });
144
+
145
+ // Later, to stop listening:
146
+ unsubscribe();
147
+ ```
148
+
149
+ Registers a listener that runs when a new compatible Senxor device is attached while your app is running. The listener receives a `Senxor` instance you can `open()` and stream from. Returns an unsubscribe function that removes the listener.
150
+
151
+ ## Working with @senxor/core
152
+
153
+ `@senxor/capacitor-serial` focuses on integrating with the native Capacitor serial plugin and returning `Senxor` instances. After you obtain a `Senxor` from this package, you will typically:
154
+
155
+ - Use `open()`, `close()`, `startStreaming()`, and `stopStreaming()` to control the device session.
156
+ - Subscribe to `onData` and `onError` to receive frames and handle errors.
157
+ - Use processors from `@senxor/core` (such as `nomalizeSenxorData`, `createGrayScaleImageData`, and `applyColorMap`) to normalize and visualize frames.
158
+
159
+ For a full description of the `Senxor` class, error types, and processing helpers, see the `@senxor/core` README.
160
+
161
+ ## Example project
162
+
163
+ This repository includes a Capacitor example application that demonstrates how to:
164
+
165
+ - Configure the Android project with the required repositories and USB permissions.
166
+ - Install and sync the `capacitor-serial-plugin`.
167
+ - Discover and stream from Senxor devices inside a Capacitor WebView.
168
+
169
+ Look for the `examples/capacitor-serial` directory in the Senxor.js repository for a complete, runnable example built on top of `@senxor/capacitor-serial` and `@senxor/core`.
170
+
@@ -0,0 +1,9 @@
1
+ import { Senxor } from "@senxor/core";
2
+ import { SerialTransportBase } from "@senxor/serial-core";
3
+ import { CapacitorSerialPort } from "serial-adaptor-capacitor";
4
+
5
+ //#region src/api.d.ts
6
+ declare function listCapacitorSerialSenxors(): Promise<Senxor<SerialTransportBase<CapacitorSerialPort>>[]>;
7
+ declare function onCapacitorSerialSenxorConnect(listener: (device: Senxor<SerialTransportBase<CapacitorSerialPort>>) => void): () => void;
8
+ //#endregion
9
+ export { listCapacitorSerialSenxors, onCapacitorSerialSenxorConnect };
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ import { Senxor } from "@senxor/core";
2
+ import { SerialTransportBase, isSenxorDevice } from "@senxor/serial-core";
3
+ import * as capacitorSerial from "serial-adaptor-capacitor";
4
+ //#region src/api.ts
5
+ async function listCapacitorSerialSenxors() {
6
+ return (await capacitorSerial.listDevices()).filter((port) => isSenxorDevice(port.deviceInfo)).map((port) => new SerialTransportBase(port)).map((transport) => new Senxor(transport));
7
+ }
8
+ function onCapacitorSerialSenxorConnect(listener) {
9
+ return capacitorSerial.onDeviceConnect((port) => {
10
+ if (isSenxorDevice(port.deviceInfo)) listener(new Senxor(new SerialTransportBase(port)));
11
+ });
12
+ }
13
+ //#endregion
14
+ export { listCapacitorSerialSenxors, onCapacitorSerialSenxorConnect };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@senxor/capacitor-serial",
3
+ "version": "1.1.0",
4
+ "description": "A library for connecting to Senxor devices on Android via Capacitor plugin.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "homepage": "https://github.com/MeridianInnovation/senxor-js#readme",
8
+ "bugs": {
9
+ "url": "https://github.com/MeridianInnovation/senxor-js/issues"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/MeridianInnovation/senxor-js.git"
14
+ },
15
+ "author": "Shui <zelongshui@meridianinno.com>",
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": "./dist/index.js",
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "serial-adaptor-capacitor": "latest",
26
+ "@senxor/core": "1.1.0",
27
+ "@senxor/serial-core": "1.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "capacitor-serial-plugin": "^1.1.1"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown",
34
+ "dev": "tsdown --watch",
35
+ "test": "vitest",
36
+ "typecheck": "tsc --noEmit"
37
+ }
38
+ }