@posx/capacitor-usb-printer 0.0.3 → 0.0.6

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.
@@ -10,6 +10,7 @@ import android.os.Build;
10
10
  import android.util.Log;
11
11
 
12
12
  import java.util.ArrayList;
13
+ import java.util.Collections;
13
14
  import java.util.List;
14
15
  import java.util.Map;
15
16
  import java.util.concurrent.ConcurrentHashMap;
@@ -24,9 +25,11 @@ public class UsbPrinter {
24
25
 
25
26
  public UsbPrinter(Context context) {
26
27
  usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
28
+ if (usbManager == null) Log.e(TAG, "USB service unavailable on this device");
27
29
  }
28
30
 
29
31
  public List<UsbDevice> listDevices() {
32
+ if (usbManager == null) return Collections.emptyList();
30
33
  return new ArrayList<>(usbManager.getDeviceList().values());
31
34
  }
32
35
 
@@ -39,6 +42,7 @@ public class UsbPrinter {
39
42
  }
40
43
 
41
44
  public void requestPermission(Context context, UsbDevice device, PermissionCallback callback) {
45
+ if (usbManager == null) { callback.onResult(false); return; }
42
46
  if (usbManager.hasPermission(device)) {
43
47
  callback.onResult(true);
44
48
  return;
@@ -70,6 +74,7 @@ public class UsbPrinter {
70
74
  }
71
75
 
72
76
  public boolean connect(UsbDevice device) {
77
+ if (usbManager == null) return false;
73
78
  int id = device.getDeviceId();
74
79
  if (connections.containsKey(id)) return true;
75
80
 
@@ -101,7 +106,11 @@ public class UsbPrinter {
101
106
  }
102
107
 
103
108
  try {
104
- conn.claimInterface(usbInterface, true);
109
+ if (!conn.claimInterface(usbInterface, true)) {
110
+ conn.close();
111
+ Log.e(TAG, "Failed to claim interface for device " + id);
112
+ return false;
113
+ }
105
114
  String name = device.getProductName() != null ? device.getProductName() : device.getDeviceName();
106
115
  String serial = getSerialNumber(device);
107
116
  connections.put(id, new DeviceConnection(conn, bulkOut, usbInterface, name, serial));
@@ -129,8 +138,8 @@ public class UsbPrinter {
129
138
  DeviceConnection dc = connections.get(deviceId);
130
139
  if (dc == null) return false;
131
140
  int result = dc.connection.bulkTransfer(dc.bulkOut, data, data.length, 10000);
132
- if (result < 0) Log.e(TAG, "Transfer failed for device " + deviceId + ": " + result);
133
- return result >= 0;
141
+ if (result <= 0) Log.e(TAG, "Transfer failed for device " + deviceId + ": " + result);
142
+ return result > 0;
134
143
  }
135
144
 
136
145
  public DeviceConnection getConnection(int deviceId) {
@@ -138,6 +147,7 @@ public class UsbPrinter {
138
147
  }
139
148
 
140
149
  public UsbDevice findDevice(int deviceId) {
150
+ if (usbManager == null) return null;
141
151
  for (UsbDevice device : usbManager.getDeviceList().values()) {
142
152
  if (device.getDeviceId() == deviceId) return device;
143
153
  }
@@ -148,8 +158,12 @@ public class UsbPrinter {
148
158
  if (hex.length() % 2 != 0) throw new IllegalArgumentException("Hex string length must be even");
149
159
  int len = hex.length();
150
160
  byte[] data = new byte[len / 2];
151
- for (int i = 0; i < len; i += 2)
152
- data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
161
+ for (int i = 0; i < len; i += 2) {
162
+ int hi = Character.digit(hex.charAt(i), 16);
163
+ int lo = Character.digit(hex.charAt(i + 1), 16);
164
+ if (hi < 0 || lo < 0) throw new IllegalArgumentException("Invalid hex char at position " + i);
165
+ data[i / 2] = (byte) ((hi << 4) + lo);
166
+ }
153
167
  return data;
154
168
  }
155
169
 
@@ -39,7 +39,7 @@ public class UsbPrinterPlugin extends Plugin {
39
39
  notifyListeners("deviceDetached", data);
40
40
  } else {
41
41
  // request permission first so serialNumber is readable
42
- implementation.requestPermission(ctx, device, granted -> {
42
+ implementation.requestPermission(getContext(), device, granted -> {
43
43
  data.put("serialNumber", implementation.getSerialNumber(device));
44
44
  notifyListeners("deviceAttached", data);
45
45
  });
@@ -55,6 +55,19 @@ public class UsbPrinterPlugin extends Plugin {
55
55
  getContext().registerReceiver(usbReceiver, filter);
56
56
  }
57
57
 
58
+ @Override
59
+ public void handleOnNewIntent(Intent intent) {
60
+ super.handleOnNewIntent(intent);
61
+ if (!UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) return;
62
+ UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
63
+ if (device == null) return;
64
+ JSObject data = deviceToJs(device);
65
+ implementation.requestPermission(getContext(), device, granted -> {
66
+ data.put("serialNumber", implementation.getSerialNumber(device));
67
+ notifyListeners("deviceAttached", data);
68
+ });
69
+ }
70
+
58
71
  @Override
59
72
  protected void handleOnDestroy() {
60
73
  if (usbReceiver != null) {
@@ -160,6 +173,8 @@ public class UsbPrinterPlugin extends Plugin {
160
173
  boolean success = implementation.print(deviceId, bytes);
161
174
  if (success) call.resolve();
162
175
  else call.reject("Not connected or transfer failed");
176
+ } catch (IllegalArgumentException e) {
177
+ call.reject("Invalid hex data: " + e.getMessage());
163
178
  } catch (Exception e) {
164
179
  call.reject("Failed to print", e);
165
180
  }
package/package.json CHANGED
@@ -1,63 +1,63 @@
1
- {
2
- "name": "@posx/capacitor-usb-printer",
3
- "version": "0.0.3",
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
- "keywords": [
17
- "capacitor",
18
- "plugin",
19
- "native"
20
- ],
21
- "scripts": {
22
- "verify": "npm run verify:android && npm run verify:web",
23
- "verify:android": "cd android && ./gradlew clean build test && cd ..",
24
- "verify:web": "npm run build",
25
- "lint": "npm run eslint && npm run prettier -- --check",
26
- "fmt": "npm run eslint -- --fix && npm run prettier -- --write",
27
- "eslint": "eslint . --ext ts",
28
- "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
29
- "docgen": "docgen --api UsbPrinterPlugin --output-readme README.md --output-json dist/docs.json",
30
- "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
31
- "clean": "rimraf ./dist",
32
- "p": "npm run build && npm publish",
33
- "bump": "bumpp patch --commit --tag --push --yes",
34
- "watch": "tsc --watch",
35
- "prepublishOnly": "npm run build"
36
- },
37
- "devDependencies": {
38
- "@capacitor/android": "^5.7.8",
39
- "@capacitor/core": "^5.7.8",
40
- "@capacitor/docgen": "^0.3.1",
41
- "@ionic/eslint-config": "^0.4.0",
42
- "@ionic/prettier-config": "^4.0.0",
43
- "bumpp": "10.4.1",
44
- "eslint": "^8.57.1",
45
- "prettier": "^3.6.2",
46
- "prettier-plugin-java": "^2.7.7",
47
- "rimraf": "^6.1.0",
48
- "rollup": "^4.53.2",
49
- "typescript": "^5.9.3"
50
- },
51
- "peerDependencies": {
52
- "@capacitor/core": ">=5.0.0"
53
- },
54
- "prettier": "@ionic/prettier-config",
55
- "eslintConfig": {
56
- "extends": "@ionic/eslint-config/recommended"
57
- },
58
- "capacitor": {
59
- "android": {
60
- "src": "android"
61
- }
62
- }
63
- }
1
+ {
2
+ "name": "@posx/capacitor-usb-printer",
3
+ "version": "0.0.6",
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
+ "keywords": [
17
+ "capacitor",
18
+ "plugin",
19
+ "native"
20
+ ],
21
+ "scripts": {
22
+ "verify": "npm run verify:android && npm run verify:web",
23
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
24
+ "verify:web": "npm run build",
25
+ "lint": "npm run eslint && npm run prettier -- --check",
26
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write",
27
+ "eslint": "eslint . --ext ts",
28
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
29
+ "docgen": "docgen --api UsbPrinterPlugin --output-readme README.md --output-json dist/docs.json",
30
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
31
+ "clean": "rimraf ./dist",
32
+ "p": "npm run build && npm publish",
33
+ "bump": "bumpp patch --commit --tag --push --yes",
34
+ "watch": "tsc --watch",
35
+ "prepublishOnly": "npm run build"
36
+ },
37
+ "devDependencies": {
38
+ "@capacitor/android": "^5.7.8",
39
+ "@capacitor/core": "^5.7.8",
40
+ "@capacitor/docgen": "^0.3.1",
41
+ "@ionic/eslint-config": "^0.4.0",
42
+ "@ionic/prettier-config": "^4.0.0",
43
+ "bumpp": "10.4.1",
44
+ "eslint": "^8.57.1",
45
+ "prettier": "^3.6.2",
46
+ "prettier-plugin-java": "^2.7.7",
47
+ "rimraf": "^6.1.0",
48
+ "rollup": "^4.53.2",
49
+ "typescript": "^5.9.3"
50
+ },
51
+ "peerDependencies": {
52
+ "@capacitor/core": ">=5.0.0"
53
+ },
54
+ "prettier": "@ionic/prettier-config",
55
+ "eslintConfig": {
56
+ "extends": "@ionic/eslint-config/recommended"
57
+ },
58
+ "capacitor": {
59
+ "android": {
60
+ "src": "android"
61
+ }
62
+ }
63
+ }