@posx/capacitor-usb-printer 0.0.1

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,126 @@
1
+ # @posx/capacitor-usb-printer
2
+
3
+ Capacitor plugin for
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @posx/capacitor-usb-printer
9
+ npx cap sync
10
+ ```
11
+
12
+ ## API
13
+
14
+ <docgen-index>
15
+
16
+ * [`listDevices()`](#listdevices)
17
+ * [`requestPermission(...)`](#requestpermission)
18
+ * [`connect(...)`](#connect)
19
+ * [`disconnect(...)`](#disconnect)
20
+ * [`print(...)`](#print)
21
+ * [`getStatus(...)`](#getstatus)
22
+ * [Interfaces](#interfaces)
23
+
24
+ </docgen-index>
25
+
26
+ <docgen-api>
27
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
28
+
29
+ ### listDevices()
30
+
31
+ ```typescript
32
+ listDevices() => Promise<{ devices: UsbDevice[]; }>
33
+ ```
34
+
35
+ **Returns:** <code>Promise&lt;{ devices: UsbDevice[]; }&gt;</code>
36
+
37
+ --------------------
38
+
39
+
40
+ ### requestPermission(...)
41
+
42
+ ```typescript
43
+ requestPermission(options: { deviceId: number; }) => Promise<{ granted: boolean; }>
44
+ ```
45
+
46
+ | Param | Type |
47
+ | ------------- | ---------------------------------- |
48
+ | **`options`** | <code>{ deviceId: number; }</code> |
49
+
50
+ **Returns:** <code>Promise&lt;{ granted: boolean; }&gt;</code>
51
+
52
+ --------------------
53
+
54
+
55
+ ### connect(...)
56
+
57
+ ```typescript
58
+ connect(options: { deviceId: number; }) => Promise<{ connected: boolean; }>
59
+ ```
60
+
61
+ | Param | Type |
62
+ | ------------- | ---------------------------------- |
63
+ | **`options`** | <code>{ deviceId: number; }</code> |
64
+
65
+ **Returns:** <code>Promise&lt;{ connected: boolean; }&gt;</code>
66
+
67
+ --------------------
68
+
69
+
70
+ ### disconnect(...)
71
+
72
+ ```typescript
73
+ disconnect(options: { deviceId: number; }) => Promise<void>
74
+ ```
75
+
76
+ | Param | Type |
77
+ | ------------- | ---------------------------------- |
78
+ | **`options`** | <code>{ deviceId: number; }</code> |
79
+
80
+ --------------------
81
+
82
+
83
+ ### print(...)
84
+
85
+ ```typescript
86
+ print(options: { deviceId: number; data: string; }) => Promise<void>
87
+ ```
88
+
89
+ | Param | Type |
90
+ | ------------- | ------------------------------------------------ |
91
+ | **`options`** | <code>{ deviceId: number; data: string; }</code> |
92
+
93
+ --------------------
94
+
95
+
96
+ ### getStatus(...)
97
+
98
+ ```typescript
99
+ getStatus(options: { deviceId: number; }) => Promise<{ connected: boolean; deviceName: string; serialNumber: string; }>
100
+ ```
101
+
102
+ | Param | Type |
103
+ | ------------- | ---------------------------------- |
104
+ | **`options`** | <code>{ deviceId: number; }</code> |
105
+
106
+ **Returns:** <code>Promise&lt;{ connected: boolean; deviceName: string; serialNumber: string; }&gt;</code>
107
+
108
+ --------------------
109
+
110
+
111
+ ### Interfaces
112
+
113
+
114
+ #### UsbDevice
115
+
116
+ | Prop | Type |
117
+ | ---------------------- | ------------------- |
118
+ | **`deviceId`** | <code>number</code> |
119
+ | **`vendorId`** | <code>number</code> |
120
+ | **`productId`** | <code>number</code> |
121
+ | **`deviceName`** | <code>string</code> |
122
+ | **`manufacturerName`** | <code>string</code> |
123
+ | **`productName`** | <code>string</code> |
124
+ | **`serialNumber`** | <code>string</code> |
125
+
126
+ </docgen-api>
@@ -0,0 +1,58 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
6
+ }
7
+
8
+ buildscript {
9
+ repositories {
10
+ google()
11
+ mavenCentral()
12
+ }
13
+ dependencies {
14
+ classpath 'com.android.tools.build:gradle:8.13.0'
15
+ }
16
+ }
17
+
18
+ apply plugin: 'com.android.library'
19
+
20
+ android {
21
+ namespace = "ai.posx.capacitor.usbprinter"
22
+ compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
23
+ defaultConfig {
24
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
25
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
26
+ versionCode 1
27
+ versionName "1.0"
28
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
29
+ }
30
+ buildTypes {
31
+ release {
32
+ minifyEnabled false
33
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34
+ }
35
+ }
36
+ lintOptions {
37
+ abortOnError = false
38
+ }
39
+ compileOptions {
40
+ sourceCompatibility JavaVersion.VERSION_21
41
+ targetCompatibility JavaVersion.VERSION_21
42
+ }
43
+ }
44
+
45
+ repositories {
46
+ google()
47
+ mavenCentral()
48
+ }
49
+
50
+
51
+ dependencies {
52
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
53
+ implementation project(':capacitor-android')
54
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
55
+ testImplementation "junit:junit:$junitVersion"
56
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
57
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
58
+ }
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+ <uses-feature android:name="android.hardware.usb.host" android:required="false" />
4
+ </manifest>
@@ -0,0 +1,179 @@
1
+ package ai.posx.capacitor.usbprinter;
2
+
3
+ import android.app.PendingIntent;
4
+ import android.content.BroadcastReceiver;
5
+ import android.content.Context;
6
+ import android.content.Intent;
7
+ import android.content.IntentFilter;
8
+ import android.hardware.usb.*;
9
+ import android.os.Build;
10
+ import android.util.Log;
11
+
12
+ import java.util.ArrayList;
13
+ import java.util.HashMap;
14
+ import java.util.List;
15
+ import java.util.Map;
16
+ import java.util.concurrent.ConcurrentHashMap;
17
+
18
+ public class UsbPrinter {
19
+ private static final String TAG = "UsbPrinter";
20
+ private static final String ACTION_USB_PERMISSION = "ai.posx.capacitor.usbprinter.USB_PERMISSION";
21
+
22
+ private final UsbManager usbManager;
23
+ private final Map<Integer, DeviceConnection> connections = new ConcurrentHashMap<>();
24
+
25
+ public UsbPrinter(Context context) {
26
+ usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
27
+ }
28
+
29
+ public List<UsbDevice> listDevices() {
30
+ return new ArrayList<>(usbManager.getDeviceList().values());
31
+ }
32
+
33
+ public String getSerialNumber(UsbDevice device) {
34
+ try {
35
+ return device.getSerialNumber() != null ? device.getSerialNumber() : "";
36
+ } catch (SecurityException e) {
37
+ return "";
38
+ }
39
+ }
40
+
41
+ public void requestPermission(Context context, UsbDevice device, PermissionCallback callback) {
42
+ if (usbManager.hasPermission(device)) {
43
+ callback.onResult(true);
44
+ return;
45
+ }
46
+
47
+ BroadcastReceiver receiver = new BroadcastReceiver() {
48
+ @Override
49
+ public void onReceive(Context ctx, Intent intent) {
50
+ context.unregisterReceiver(this);
51
+ boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
52
+ callback.onResult(granted);
53
+ }
54
+ };
55
+
56
+ IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
57
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
58
+ context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED);
59
+ } else {
60
+ context.registerReceiver(receiver, filter);
61
+ }
62
+
63
+ int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0;
64
+ PendingIntent pi = PendingIntent.getBroadcast(context, device.getDeviceId(), new Intent(ACTION_USB_PERMISSION), flags);
65
+ usbManager.requestPermission(device, pi);
66
+ }
67
+
68
+ public boolean connect(UsbDevice device) {
69
+ int id = device.getDeviceId();
70
+ if (connections.containsKey(id)) return true;
71
+
72
+ UsbEndpoint bulkOut = null;
73
+ UsbInterface usbInterface = null;
74
+
75
+ for (int i = 0; i < device.getInterfaceCount(); i++) {
76
+ UsbInterface iface = device.getInterface(i);
77
+ for (int j = 0; j < iface.getEndpointCount(); j++) {
78
+ UsbEndpoint ep = iface.getEndpoint(j);
79
+ if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && ep.getDirection() == UsbConstants.USB_DIR_OUT) {
80
+ usbInterface = iface;
81
+ bulkOut = ep;
82
+ break;
83
+ }
84
+ }
85
+ if (bulkOut != null) break;
86
+ }
87
+
88
+ if (bulkOut == null) {
89
+ Log.e(TAG, "No bulk OUT endpoint found for device " + id);
90
+ return false;
91
+ }
92
+
93
+ UsbDeviceConnection conn = usbManager.openDevice(device);
94
+ if (conn == null) {
95
+ Log.e(TAG, "Failed to open device " + id);
96
+ return false;
97
+ }
98
+
99
+ try {
100
+ conn.claimInterface(usbInterface, true);
101
+ String name = device.getProductName() != null ? device.getProductName() : device.getDeviceName();
102
+ String serial = getSerialNumber(device);
103
+ connections.put(id, new DeviceConnection(conn, bulkOut, usbInterface, name, serial));
104
+ return true;
105
+ } catch (Exception e) {
106
+ conn.close();
107
+ Log.e(TAG, "Failed to claim interface for device " + id, e);
108
+ return false;
109
+ }
110
+ }
111
+
112
+ public void disconnect(int deviceId) {
113
+ DeviceConnection dc = connections.remove(deviceId);
114
+ if (dc != null) dc.close();
115
+ }
116
+
117
+ public void disconnectAll() {
118
+ for (DeviceConnection dc : connections.values()) {
119
+ try { dc.close(); } catch (Exception e) { Log.e(TAG, "Failed to close connection", e); }
120
+ }
121
+ connections.clear();
122
+ }
123
+
124
+ public boolean print(int deviceId, byte[] data) {
125
+ DeviceConnection dc = connections.get(deviceId);
126
+ if (dc == null) return false;
127
+ int result = dc.connection.bulkTransfer(dc.bulkOut, data, data.length, 10000);
128
+ if (result < 0) Log.e(TAG, "Transfer failed for device " + deviceId + ": " + result);
129
+ return result >= 0;
130
+ }
131
+
132
+ public boolean isConnected(int deviceId) {
133
+ return connections.containsKey(deviceId);
134
+ }
135
+
136
+ public DeviceConnection getConnection(int deviceId) {
137
+ return connections.get(deviceId);
138
+ }
139
+
140
+ public UsbDevice findDevice(int deviceId) {
141
+ for (UsbDevice device : usbManager.getDeviceList().values()) {
142
+ if (device.getDeviceId() == deviceId) return device;
143
+ }
144
+ return null;
145
+ }
146
+
147
+ public static byte[] hexToBytes(String hex) {
148
+ int len = hex.length();
149
+ byte[] data = new byte[len / 2];
150
+ for (int i = 0; i < len; i += 2) {
151
+ data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
152
+ }
153
+ return data;
154
+ }
155
+
156
+ public interface PermissionCallback {
157
+ void onResult(boolean granted);
158
+ }
159
+
160
+ public static class DeviceConnection {
161
+ public final UsbDeviceConnection connection;
162
+ public final UsbEndpoint bulkOut;
163
+ public final UsbInterface usbInterface;
164
+ public final String deviceName;
165
+ public final String serialNumber;
166
+
167
+ DeviceConnection(UsbDeviceConnection connection, UsbEndpoint bulkOut, UsbInterface usbInterface, String deviceName, String serialNumber) {
168
+ this.connection = connection;
169
+ this.bulkOut = bulkOut;
170
+ this.usbInterface = usbInterface;
171
+ this.deviceName = deviceName;
172
+ this.serialNumber = serialNumber;
173
+ }
174
+
175
+ void close() {
176
+ try { connection.releaseInterface(usbInterface); } finally { connection.close(); }
177
+ }
178
+ }
179
+ }
@@ -0,0 +1,138 @@
1
+ package ai.posx.capacitor.usbprinter;
2
+
3
+ import android.hardware.usb.UsbDevice;
4
+
5
+ import com.getcapacitor.JSArray;
6
+ import com.getcapacitor.JSObject;
7
+ import com.getcapacitor.Plugin;
8
+ import com.getcapacitor.PluginCall;
9
+ import com.getcapacitor.PluginMethod;
10
+ import com.getcapacitor.annotation.CapacitorPlugin;
11
+
12
+ import java.util.List;
13
+
14
+ @CapacitorPlugin(name = "UsbPrinter")
15
+ public class UsbPrinterPlugin extends Plugin {
16
+
17
+ private UsbPrinter implementation;
18
+
19
+ @Override
20
+ public void load() {
21
+ implementation = new UsbPrinter(getContext());
22
+ }
23
+
24
+ @Override
25
+ protected void handleOnDestroy() {
26
+ implementation.disconnectAll();
27
+ super.handleOnDestroy();
28
+ }
29
+
30
+ @PluginMethod
31
+ public void listDevices(PluginCall call) {
32
+ try {
33
+ List<UsbDevice> devices = implementation.listDevices();
34
+ JSArray arr = new JSArray();
35
+ for (UsbDevice device : devices) {
36
+ JSObject d = new JSObject();
37
+ d.put("deviceId", device.getDeviceId());
38
+ d.put("vendorId", device.getVendorId());
39
+ d.put("productId", device.getProductId());
40
+ d.put("deviceName", device.getDeviceName());
41
+ d.put("manufacturerName", device.getManufacturerName() != null ? device.getManufacturerName() : "");
42
+ d.put("productName", device.getProductName() != null ? device.getProductName() : "");
43
+ d.put("serialNumber", implementation.getSerialNumber(device));
44
+ arr.put(d);
45
+ }
46
+ JSObject result = new JSObject();
47
+ result.put("devices", arr);
48
+ call.resolve(result);
49
+ } catch (Exception e) {
50
+ call.reject("Failed to list devices", e);
51
+ }
52
+ }
53
+
54
+ @PluginMethod
55
+ public void requestPermission(PluginCall call) {
56
+ try {
57
+ int deviceId = call.getInt("deviceId", -1);
58
+ UsbDevice device = implementation.findDevice(deviceId);
59
+ if (device == null) {
60
+ call.reject("Device not found");
61
+ return;
62
+ }
63
+ implementation.requestPermission(getContext(), device, granted -> {
64
+ JSObject result = new JSObject();
65
+ result.put("granted", granted);
66
+ call.resolve(result);
67
+ });
68
+ } catch (Exception e) {
69
+ call.reject("Failed to request permission", e);
70
+ }
71
+ }
72
+
73
+ @PluginMethod
74
+ public void connect(PluginCall call) {
75
+ try {
76
+ int deviceId = call.getInt("deviceId", -1);
77
+ UsbDevice device = implementation.findDevice(deviceId);
78
+ if (device == null) {
79
+ call.reject("Device not found");
80
+ return;
81
+ }
82
+ boolean connected = implementation.connect(device);
83
+ if (!connected) {
84
+ call.reject("Failed to connect");
85
+ return;
86
+ }
87
+ JSObject result = new JSObject();
88
+ result.put("connected", true);
89
+ call.resolve(result);
90
+ } catch (Exception e) {
91
+ call.reject("Failed to connect", e);
92
+ }
93
+ }
94
+
95
+ @PluginMethod
96
+ public void disconnect(PluginCall call) {
97
+ try {
98
+ int deviceId = call.getInt("deviceId", -1);
99
+ implementation.disconnect(deviceId);
100
+ call.resolve();
101
+ } catch (Exception e) {
102
+ call.reject("Failed to disconnect", e);
103
+ }
104
+ }
105
+
106
+ @PluginMethod
107
+ public void print(PluginCall call) {
108
+ try {
109
+ int deviceId = call.getInt("deviceId", -1);
110
+ String hex = call.getString("data", "");
111
+ if (hex.isEmpty()) {
112
+ call.reject("No data");
113
+ return;
114
+ }
115
+ byte[] bytes = UsbPrinter.hexToBytes(hex);
116
+ boolean success = implementation.print(deviceId, bytes);
117
+ if (success) call.resolve();
118
+ else call.reject("Not connected or transfer failed");
119
+ } catch (Exception e) {
120
+ call.reject("Failed to print", e);
121
+ }
122
+ }
123
+
124
+ @PluginMethod
125
+ public void getStatus(PluginCall call) {
126
+ try {
127
+ int deviceId = call.getInt("deviceId", -1);
128
+ UsbPrinter.DeviceConnection dc = implementation.getConnection(deviceId);
129
+ JSObject result = new JSObject();
130
+ result.put("connected", dc != null);
131
+ result.put("deviceName", dc != null ? dc.deviceName : "");
132
+ result.put("serialNumber", dc != null ? dc.serialNumber : "");
133
+ call.resolve(result);
134
+ } catch (Exception e) {
135
+ call.reject("Failed to get status", e);
136
+ }
137
+ }
138
+ }
File without changes
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <resources>
3
+ <usb-device />
4
+ </resources>
package/dist/docs.json ADDED
@@ -0,0 +1,166 @@
1
+ {
2
+ "api": {
3
+ "name": "UsbPrinterPlugin",
4
+ "slug": "usbprinterplugin",
5
+ "docs": "",
6
+ "tags": [],
7
+ "methods": [
8
+ {
9
+ "name": "listDevices",
10
+ "signature": "() => Promise<{ devices: UsbDevice[]; }>",
11
+ "parameters": [],
12
+ "returns": "Promise<{ devices: UsbDevice[]; }>",
13
+ "tags": [],
14
+ "docs": "",
15
+ "complexTypes": [
16
+ "UsbDevice"
17
+ ],
18
+ "slug": "listdevices"
19
+ },
20
+ {
21
+ "name": "requestPermission",
22
+ "signature": "(options: { deviceId: number; }) => Promise<{ granted: boolean; }>",
23
+ "parameters": [
24
+ {
25
+ "name": "options",
26
+ "docs": "",
27
+ "type": "{ deviceId: number; }"
28
+ }
29
+ ],
30
+ "returns": "Promise<{ granted: boolean; }>",
31
+ "tags": [],
32
+ "docs": "",
33
+ "complexTypes": [],
34
+ "slug": "requestpermission"
35
+ },
36
+ {
37
+ "name": "connect",
38
+ "signature": "(options: { deviceId: number; }) => Promise<{ connected: boolean; }>",
39
+ "parameters": [
40
+ {
41
+ "name": "options",
42
+ "docs": "",
43
+ "type": "{ deviceId: number; }"
44
+ }
45
+ ],
46
+ "returns": "Promise<{ connected: boolean; }>",
47
+ "tags": [],
48
+ "docs": "",
49
+ "complexTypes": [],
50
+ "slug": "connect"
51
+ },
52
+ {
53
+ "name": "disconnect",
54
+ "signature": "(options: { deviceId: number; }) => Promise<void>",
55
+ "parameters": [
56
+ {
57
+ "name": "options",
58
+ "docs": "",
59
+ "type": "{ deviceId: number; }"
60
+ }
61
+ ],
62
+ "returns": "Promise<void>",
63
+ "tags": [],
64
+ "docs": "",
65
+ "complexTypes": [],
66
+ "slug": "disconnect"
67
+ },
68
+ {
69
+ "name": "print",
70
+ "signature": "(options: { deviceId: number; data: string; }) => Promise<void>",
71
+ "parameters": [
72
+ {
73
+ "name": "options",
74
+ "docs": "",
75
+ "type": "{ deviceId: number; data: string; }"
76
+ }
77
+ ],
78
+ "returns": "Promise<void>",
79
+ "tags": [],
80
+ "docs": "",
81
+ "complexTypes": [],
82
+ "slug": "print"
83
+ },
84
+ {
85
+ "name": "getStatus",
86
+ "signature": "(options: { deviceId: number; }) => Promise<{ connected: boolean; deviceName: string; serialNumber: string; }>",
87
+ "parameters": [
88
+ {
89
+ "name": "options",
90
+ "docs": "",
91
+ "type": "{ deviceId: number; }"
92
+ }
93
+ ],
94
+ "returns": "Promise<{ connected: boolean; deviceName: string; serialNumber: string; }>",
95
+ "tags": [],
96
+ "docs": "",
97
+ "complexTypes": [],
98
+ "slug": "getstatus"
99
+ }
100
+ ],
101
+ "properties": []
102
+ },
103
+ "interfaces": [
104
+ {
105
+ "name": "UsbDevice",
106
+ "slug": "usbdevice",
107
+ "docs": "",
108
+ "tags": [],
109
+ "methods": [],
110
+ "properties": [
111
+ {
112
+ "name": "deviceId",
113
+ "tags": [],
114
+ "docs": "",
115
+ "complexTypes": [],
116
+ "type": "number"
117
+ },
118
+ {
119
+ "name": "vendorId",
120
+ "tags": [],
121
+ "docs": "",
122
+ "complexTypes": [],
123
+ "type": "number"
124
+ },
125
+ {
126
+ "name": "productId",
127
+ "tags": [],
128
+ "docs": "",
129
+ "complexTypes": [],
130
+ "type": "number"
131
+ },
132
+ {
133
+ "name": "deviceName",
134
+ "tags": [],
135
+ "docs": "",
136
+ "complexTypes": [],
137
+ "type": "string"
138
+ },
139
+ {
140
+ "name": "manufacturerName",
141
+ "tags": [],
142
+ "docs": "",
143
+ "complexTypes": [],
144
+ "type": "string"
145
+ },
146
+ {
147
+ "name": "productName",
148
+ "tags": [],
149
+ "docs": "",
150
+ "complexTypes": [],
151
+ "type": "string"
152
+ },
153
+ {
154
+ "name": "serialNumber",
155
+ "tags": [],
156
+ "docs": "",
157
+ "complexTypes": [],
158
+ "type": "string"
159
+ }
160
+ ]
161
+ }
162
+ ],
163
+ "enums": [],
164
+ "typeAliases": [],
165
+ "pluginConfigs": []
166
+ }
@@ -0,0 +1,38 @@
1
+ export interface UsbDevice {
2
+ deviceId: number;
3
+ vendorId: number;
4
+ productId: number;
5
+ deviceName: string;
6
+ manufacturerName: string;
7
+ productName: string;
8
+ serialNumber: string;
9
+ }
10
+ export interface UsbPrinterPlugin {
11
+ listDevices(): Promise<{
12
+ devices: UsbDevice[];
13
+ }>;
14
+ requestPermission(options: {
15
+ deviceId: number;
16
+ }): Promise<{
17
+ granted: boolean;
18
+ }>;
19
+ connect(options: {
20
+ deviceId: number;
21
+ }): Promise<{
22
+ connected: boolean;
23
+ }>;
24
+ disconnect(options: {
25
+ deviceId: number;
26
+ }): Promise<void>;
27
+ print(options: {
28
+ deviceId: number;
29
+ data: string;
30
+ }): Promise<void>;
31
+ getStatus(options: {
32
+ deviceId: number;
33
+ }): Promise<{
34
+ connected: boolean;
35
+ deviceName: string;
36
+ serialNumber: string;
37
+ }>;
38
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["export interface UsbDevice {\n deviceId: number;\n vendorId: number;\n productId: number;\n deviceName: string;\n manufacturerName: string;\n productName: string;\n serialNumber: string;\n}\n\nexport interface UsbPrinterPlugin {\n listDevices(): Promise<{ devices: UsbDevice[] }>;\n requestPermission(options: { deviceId: number }): Promise<{ granted: boolean }>;\n connect(options: { deviceId: number }): Promise<{ connected: boolean }>;\n disconnect(options: { deviceId: number }): Promise<void>;\n print(options: { deviceId: number; data: string }): Promise<void>;\n getStatus(options: { deviceId: number }): Promise<{ connected: boolean; deviceName: string; serialNumber: string }>;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { UsbPrinterPlugin } from './definitions';
2
+ declare const UsbPrinter: UsbPrinterPlugin;
3
+ export * from './definitions';
4
+ export { UsbPrinter };
@@ -0,0 +1,7 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const UsbPrinter = registerPlugin('UsbPrinter', {
3
+ web: () => import('./web').then((m) => new m.UsbPrinterWeb()),
4
+ });
5
+ export * from './definitions';
6
+ export { UsbPrinter };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,UAAU,GAAG,cAAc,CAAmB,YAAY,EAAE;IAChE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;CAC9D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { UsbPrinterPlugin } from './definitions';\n\nconst UsbPrinter = registerPlugin<UsbPrinterPlugin>('UsbPrinter', {\n web: () => import('./web').then((m) => new m.UsbPrinterWeb()),\n});\n\nexport * from './definitions';\nexport { UsbPrinter };\n"]}
@@ -0,0 +1,20 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { UsbPrinterPlugin } from './definitions';
3
+ export declare class UsbPrinterWeb extends WebPlugin implements UsbPrinterPlugin {
4
+ listDevices(): Promise<{
5
+ devices: never[];
6
+ }>;
7
+ requestPermission(): Promise<{
8
+ granted: boolean;
9
+ }>;
10
+ connect(): Promise<{
11
+ connected: boolean;
12
+ }>;
13
+ disconnect(): Promise<void>;
14
+ print(): Promise<void>;
15
+ getStatus(): Promise<{
16
+ connected: boolean;
17
+ deviceName: string;
18
+ serialNumber: string;
19
+ }>;
20
+ }
@@ -0,0 +1,26 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ export class UsbPrinterWeb extends WebPlugin {
3
+ async listDevices() {
4
+ console.warn('USB Printer is only available on Android devices');
5
+ return { devices: [] };
6
+ }
7
+ async requestPermission() {
8
+ console.warn('USB Printer is only available on Android devices');
9
+ return { granted: false };
10
+ }
11
+ async connect() {
12
+ console.warn('USB Printer is only available on Android devices');
13
+ return { connected: false };
14
+ }
15
+ async disconnect() {
16
+ console.warn('USB Printer is only available on Android devices');
17
+ }
18
+ async print() {
19
+ console.warn('USB Printer is only available on Android devices');
20
+ }
21
+ async getStatus() {
22
+ console.warn('USB Printer is only available on Android devices');
23
+ return { connected: false, deviceName: '', serialNumber: '' };
24
+ }
25
+ }
26
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC1C,KAAK,CAAC,WAAW;QACf,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;QACjE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,iBAAiB;QACrB,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;QACjE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,OAAO;QACX,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;QACjE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,UAAU;QACd,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;IACnE,CAAC;IAED,KAAK,CAAC,KAAK;QACT,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;IACnE,CAAC;IAED,KAAK,CAAC,SAAS;QACb,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;QACjE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC;IAChE,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type { UsbPrinterPlugin } from './definitions';\n\nexport class UsbPrinterWeb extends WebPlugin implements UsbPrinterPlugin {\n async listDevices(): Promise<{ devices: never[] }> {\n console.warn('USB Printer is only available on Android devices');\n return { devices: [] };\n }\n\n async requestPermission(): Promise<{ granted: boolean }> {\n console.warn('USB Printer is only available on Android devices');\n return { granted: false };\n }\n\n async connect(): Promise<{ connected: boolean }> {\n console.warn('USB Printer is only available on Android devices');\n return { connected: false };\n }\n\n async disconnect(): Promise<void> {\n console.warn('USB Printer is only available on Android devices');\n }\n\n async print(): Promise<void> {\n console.warn('USB Printer is only available on Android devices');\n }\n\n async getStatus(): Promise<{ connected: boolean; deviceName: string; serialNumber: string }> {\n console.warn('USB Printer is only available on Android devices');\n return { connected: false, deviceName: '', serialNumber: '' };\n }\n}\n"]}
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var core = require('@capacitor/core');
4
+
5
+ const UsbPrinter = core.registerPlugin('UsbPrinter', {
6
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.UsbPrinterWeb()),
7
+ });
8
+
9
+ class UsbPrinterWeb extends core.WebPlugin {
10
+ async listDevices() {
11
+ console.warn('USB Printer is only available on Android devices');
12
+ return { devices: [] };
13
+ }
14
+ async requestPermission() {
15
+ console.warn('USB Printer is only available on Android devices');
16
+ return { granted: false };
17
+ }
18
+ async connect() {
19
+ console.warn('USB Printer is only available on Android devices');
20
+ return { connected: false };
21
+ }
22
+ async disconnect() {
23
+ console.warn('USB Printer is only available on Android devices');
24
+ }
25
+ async print() {
26
+ console.warn('USB Printer is only available on Android devices');
27
+ }
28
+ async getStatus() {
29
+ console.warn('USB Printer is only available on Android devices');
30
+ return { connected: false, deviceName: '', serialNumber: '' };
31
+ }
32
+ }
33
+
34
+ var web = /*#__PURE__*/Object.freeze({
35
+ __proto__: null,
36
+ UsbPrinterWeb: UsbPrinterWeb
37
+ });
38
+
39
+ exports.UsbPrinter = UsbPrinter;
40
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst UsbPrinter = registerPlugin('UsbPrinter', {\n web: () => import('./web').then((m) => new m.UsbPrinterWeb()),\n});\nexport * from './definitions';\nexport { UsbPrinter };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class UsbPrinterWeb extends WebPlugin {\n async listDevices() {\n console.warn('USB Printer is only available on Android devices');\n return { devices: [] };\n }\n async requestPermission() {\n console.warn('USB Printer is only available on Android devices');\n return { granted: false };\n }\n async connect() {\n console.warn('USB Printer is only available on Android devices');\n return { connected: false };\n }\n async disconnect() {\n console.warn('USB Printer is only available on Android devices');\n }\n async print() {\n console.warn('USB Printer is only available on Android devices');\n }\n async getStatus() {\n console.warn('USB Printer is only available on Android devices');\n return { connected: false, deviceName: '', serialNumber: '' };\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,UAAU,GAAGA,mBAAc,CAAC,YAAY,EAAE;AAChD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;AACjE,CAAC;;ACFM,MAAM,aAAa,SAASC,cAAS,CAAC;AAC7C,IAAI,MAAM,WAAW,GAAG;AACxB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;AACxE,QAAQ,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE;AAC9B,IAAI;AACJ,IAAI,MAAM,iBAAiB,GAAG;AAC9B,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;AACxE,QAAQ,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE;AACjC,IAAI;AACJ,IAAI,MAAM,OAAO,GAAG;AACpB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;AACxE,QAAQ,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE;AACnC,IAAI;AACJ,IAAI,MAAM,UAAU,GAAG;AACvB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;AACxE,IAAI;AACJ,IAAI,MAAM,KAAK,GAAG;AAClB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;AACxE,IAAI;AACJ,IAAI,MAAM,SAAS,GAAG;AACtB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;AACxE,QAAQ,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,YAAY,EAAE,EAAE,EAAE;AACrE,IAAI;AACJ;;;;;;;;;"}
package/dist/plugin.js ADDED
@@ -0,0 +1,43 @@
1
+ var capacitorUsbPrinter = (function (exports, core) {
2
+ 'use strict';
3
+
4
+ const UsbPrinter = core.registerPlugin('UsbPrinter', {
5
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.UsbPrinterWeb()),
6
+ });
7
+
8
+ class UsbPrinterWeb extends core.WebPlugin {
9
+ async listDevices() {
10
+ console.warn('USB Printer is only available on Android devices');
11
+ return { devices: [] };
12
+ }
13
+ async requestPermission() {
14
+ console.warn('USB Printer is only available on Android devices');
15
+ return { granted: false };
16
+ }
17
+ async connect() {
18
+ console.warn('USB Printer is only available on Android devices');
19
+ return { connected: false };
20
+ }
21
+ async disconnect() {
22
+ console.warn('USB Printer is only available on Android devices');
23
+ }
24
+ async print() {
25
+ console.warn('USB Printer is only available on Android devices');
26
+ }
27
+ async getStatus() {
28
+ console.warn('USB Printer is only available on Android devices');
29
+ return { connected: false, deviceName: '', serialNumber: '' };
30
+ }
31
+ }
32
+
33
+ var web = /*#__PURE__*/Object.freeze({
34
+ __proto__: null,
35
+ UsbPrinterWeb: UsbPrinterWeb
36
+ });
37
+
38
+ exports.UsbPrinter = UsbPrinter;
39
+
40
+ return exports;
41
+
42
+ })({}, capacitorExports);
43
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst UsbPrinter = registerPlugin('UsbPrinter', {\n web: () => import('./web').then((m) => new m.UsbPrinterWeb()),\n});\nexport * from './definitions';\nexport { UsbPrinter };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class UsbPrinterWeb extends WebPlugin {\n async listDevices() {\n console.warn('USB Printer is only available on Android devices');\n return { devices: [] };\n }\n async requestPermission() {\n console.warn('USB Printer is only available on Android devices');\n return { granted: false };\n }\n async connect() {\n console.warn('USB Printer is only available on Android devices');\n return { connected: false };\n }\n async disconnect() {\n console.warn('USB Printer is only available on Android devices');\n }\n async print() {\n console.warn('USB Printer is only available on Android devices');\n }\n async getStatus() {\n console.warn('USB Printer is only available on Android devices');\n return { connected: false, deviceName: '', serialNumber: '' };\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,UAAU,GAAGA,mBAAc,CAAC,YAAY,EAAE;IAChD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;IACjE,CAAC;;ICFM,MAAM,aAAa,SAASC,cAAS,CAAC;IAC7C,IAAI,MAAM,WAAW,GAAG;IACxB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;IACxE,QAAQ,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE;IAC9B,IAAI;IACJ,IAAI,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;IACxE,QAAQ,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE;IACjC,IAAI;IACJ,IAAI,MAAM,OAAO,GAAG;IACpB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;IACxE,QAAQ,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE;IACnC,IAAI;IACJ,IAAI,MAAM,UAAU,GAAG;IACvB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;IACxE,IAAI;IACJ,IAAI,MAAM,KAAK,GAAG;IAClB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;IACxE,IAAI;IACJ,IAAI,MAAM,SAAS,GAAG;IACtB,QAAQ,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC;IACxE,QAAQ,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,YAAY,EAAE,EAAE,EAAE;IACrE,IAAI;IACJ;;;;;;;;;;;;;;;"}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@posx/capacitor-usb-printer",
3
+ "version": "0.0.1",
4
+ "description": "Capacitor plugin for USB ESC/POS thermal printer via Android USB Host API",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/"
13
+ ],
14
+ "author": "posx team",
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/niceit/capacitor-usb-printer.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/niceit/capacitor-usb-printer/issues"
22
+ },
23
+ "keywords": [
24
+ "capacitor",
25
+ "plugin",
26
+ "native"
27
+ ],
28
+ "devDependencies": {
29
+ "@capacitor/android": "^5.7.8",
30
+ "@capacitor/core": "^5.7.8",
31
+ "@capacitor/docgen": "^0.3.1",
32
+ "@ionic/eslint-config": "^0.4.0",
33
+ "@ionic/prettier-config": "^4.0.0",
34
+ "eslint": "^8.57.1",
35
+ "prettier": "^3.6.2",
36
+ "prettier-plugin-java": "^2.7.7",
37
+ "rimraf": "^6.1.0",
38
+ "rollup": "^4.53.2",
39
+ "typescript": "^5.9.3"
40
+ },
41
+ "peerDependencies": {
42
+ "@capacitor/core": ">=5.0.0"
43
+ },
44
+ "prettier": "@ionic/prettier-config",
45
+ "eslintConfig": {
46
+ "extends": "@ionic/eslint-config/recommended"
47
+ },
48
+ "capacitor": {
49
+ "android": {
50
+ "src": "android"
51
+ }
52
+ },
53
+ "scripts": {
54
+ "verify": "npm run verify:android && npm run verify:web",
55
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
56
+ "verify:web": "npm run build",
57
+ "lint": "npm run eslint && npm run prettier -- --check",
58
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write",
59
+ "eslint": "eslint . --ext ts",
60
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
61
+ "docgen": "docgen --api UsbPrinterPlugin --output-readme README.md --output-json dist/docs.json",
62
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
63
+ "clean": "rimraf ./dist",
64
+ "p": "npm run build && npm publish",
65
+ "bump": "npm version patch",
66
+ "watch": "tsc --watch"
67
+ }
68
+ }