@bricks-soft/capacitor-thermal-printer 0.0.0-stage → 8.0.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.
Files changed (35) hide show
  1. package/BricksSoftCapacitorThermalPrinter.podspec +17 -0
  2. package/LICENSE +201 -0
  3. package/Package.swift +13 -0
  4. package/README.md +80 -2
  5. package/android/build.gradle +40 -0
  6. package/android/src/main/AndroidManifest.xml +8 -0
  7. package/android/src/main/java/com/brickssoft/thermalprinter/ThermalPrinterPlugin.java +463 -0
  8. package/dist/esm/definitions.d.ts +73 -0
  9. package/dist/esm/definitions.js +1 -0
  10. package/dist/esm/index.d.ts +3 -0
  11. package/dist/esm/index.js +23 -0
  12. package/dist/esm/web.d.ts +13 -0
  13. package/dist/esm/web.js +33 -0
  14. package/dist/plugin.cjs.js +65 -0
  15. package/dist/plugin.js +68 -0
  16. package/ios/Sources/ThermalPrinter/EscPosBarcode.swift +121 -0
  17. package/ios/Sources/ThermalPrinter/EscPosBitmap.swift +178 -0
  18. package/ios/Sources/ThermalPrinter/EscPosBluetooth.swift +341 -0
  19. package/ios/Sources/ThermalPrinter/EscPosBluetoothConnection.swift +298 -0
  20. package/ios/Sources/ThermalPrinter/EscPosBluetoothRules.swift +78 -0
  21. package/ios/Sources/ThermalPrinter/EscPosCharsetEncoding.swift +115 -0
  22. package/ios/Sources/ThermalPrinter/EscPosCoreBluetoothLink.swift +287 -0
  23. package/ios/Sources/ThermalPrinter/EscPosDeviceConnection.swift +47 -0
  24. package/ios/Sources/ThermalPrinter/EscPosJavaCompat.swift +213 -0
  25. package/ios/Sources/ThermalPrinter/EscPosKnownPrinterStore.swift +44 -0
  26. package/ios/Sources/ThermalPrinter/EscPosPrinter.swift +55 -0
  27. package/ios/Sources/ThermalPrinter/EscPosPrinterCommands.swift +367 -0
  28. package/ios/Sources/ThermalPrinter/EscPosPrinterSize.swift +53 -0
  29. package/ios/Sources/ThermalPrinter/EscPosQRCode.swift +70 -0
  30. package/ios/Sources/ThermalPrinter/EscPosQueue.swift +30 -0
  31. package/ios/Sources/ThermalPrinter/EscPosTcpConnection.swift +265 -0
  32. package/ios/Sources/ThermalPrinter/PrinterTextParser.swift +622 -0
  33. package/ios/Sources/ThermalPrinter/ThermalPrinterPlugin.swift +85 -0
  34. package/ios/Sources/ThermalPrinter/ThermalPrinterService.swift +380 -0
  35. package/package.json +61 -4
@@ -0,0 +1,463 @@
1
+ package com.brickssoft.thermalprinter;
2
+
3
+
4
+ import android.Manifest;
5
+ import android.app.PendingIntent;
6
+ import android.bluetooth.BluetoothAdapter;
7
+ import android.bluetooth.BluetoothDevice;
8
+ import android.content.BroadcastReceiver;
9
+ import android.content.Context;
10
+ import android.content.Intent;
11
+ import android.content.IntentFilter;
12
+ import android.graphics.Bitmap;
13
+ import android.graphics.BitmapFactory;
14
+ import android.hardware.usb.UsbDevice;
15
+ import android.hardware.usb.UsbManager;
16
+ import android.util.Base64;
17
+
18
+ import com.dantsu.escposprinter.EscPosCharsetEncoding;
19
+ import com.dantsu.escposprinter.EscPosPrinter;
20
+ import com.dantsu.escposprinter.connection.DeviceConnection;
21
+ import com.dantsu.escposprinter.connection.bluetooth.BluetoothConnection;
22
+ import com.dantsu.escposprinter.connection.bluetooth.BluetoothConnections;
23
+ import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections;
24
+ import com.dantsu.escposprinter.connection.tcp.TcpConnection;
25
+ import com.dantsu.escposprinter.connection.usb.UsbConnection;
26
+ import com.dantsu.escposprinter.connection.usb.UsbConnections;
27
+ import com.dantsu.escposprinter.exceptions.EscPosConnectionException;
28
+ import com.dantsu.escposprinter.textparser.PrinterTextParserImg;
29
+
30
+ import org.json.JSONArray;
31
+ import org.json.JSONException;
32
+ import org.json.JSONObject;
33
+
34
+ import java.util.ArrayList;
35
+ import java.util.HashMap;
36
+ import java.util.Objects;
37
+
38
+ import android.os.Build;
39
+ import android.content.pm.PackageManager;
40
+
41
+ import com.getcapacitor.JSObject;
42
+ import com.getcapacitor.Plugin;
43
+ import com.getcapacitor.PluginCall;
44
+ import com.getcapacitor.PluginMethod;
45
+ import com.getcapacitor.PermissionState;
46
+ import com.getcapacitor.annotation.CapacitorPlugin;
47
+ import com.getcapacitor.annotation.Permission;
48
+ import com.getcapacitor.annotation.PermissionCallback;
49
+ import java.util.concurrent.ExecutorService;
50
+ import java.util.concurrent.Executors;
51
+
52
+ @CapacitorPlugin(name = "ThermalPrinter", permissions = {
53
+ @Permission(alias = "bluetooth", strings = { Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN }),
54
+ @Permission(alias = "bluetoothLegacy", strings = { Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN })
55
+ })
56
+ public class ThermalPrinterPlugin extends Plugin {
57
+ private final HashMap<String, DeviceConnection> connections = new HashMap<>();
58
+ private final ExecutorService executor = Executors.newSingleThreadExecutor();
59
+
60
+ private static final class Result {
61
+ private final PluginCall call;
62
+ private boolean completed;
63
+ Result(PluginCall call) { this.call = call; }
64
+ synchronized void success() { success(null); }
65
+ synchronized void success(Object value) {
66
+ if (completed) return;
67
+ completed = true;
68
+ call.resolve(new JSObject().put("value", value));
69
+ }
70
+ synchronized void error(Object value) {
71
+ if (completed) return;
72
+ completed = true;
73
+ String message = value instanceof JSONObject ? ((JSONObject) value).optString("error", value.toString()) : String.valueOf(value);
74
+ JSObject data = new JSObject();
75
+ if (value instanceof JSONObject) {
76
+ JSONObject object = (JSONObject) value;
77
+ java.util.Iterator<String> keys = object.keys();
78
+ while (keys.hasNext()) {
79
+ String key = keys.next();
80
+ data.put(key, object.opt(key));
81
+ }
82
+ }
83
+ call.reject(message, data);
84
+ }
85
+ }
86
+
87
+ private void run(String action, PluginCall call) {
88
+ executor.execute(() -> {
89
+ Result result = new Result(call);
90
+ JSONObject data = call.getData();
91
+ try {
92
+ switch (action) {
93
+ case "listPrinters": listPrinters(result, data); break;
94
+ case "printFormattedText":
95
+ case "printFormattedTextAndCut": printFormattedText(result, action, data); break;
96
+ case "getEncoding": getEncoding(result, data); break;
97
+ case "disconnectPrinter": disconnectPrinter(result, data); break;
98
+ case "requestUSBPermissions": requestUSBPermissions(result, data); break;
99
+ case "bitmapToHexadecimalString": bitmapToHexadecimalString(result, data); break;
100
+ case "bytesToHexadecimalString":
101
+ JSONArray bytes = data.getJSONArray("bytes");
102
+ byte[] decoded = new byte[bytes.length()];
103
+ for (int i = 0; i < decoded.length; i++) {
104
+ int value = bytes.getInt(i);
105
+ if (value < 0 || value > 255) throw new JSONException("Invalid image byte");
106
+ decoded[i] = (byte) value;
107
+ }
108
+ data.put("bytes", decoded);
109
+ bytesToHexadecimalString(result, data);
110
+ break;
111
+ default: result.error("Invalid action");
112
+ }
113
+ } catch (Exception e) {
114
+ result.error(e.getMessage());
115
+ }
116
+ });
117
+ }
118
+
119
+ @PluginMethod
120
+ public void listPrinters(PluginCall call) { run("listPrinters", call); }
121
+
122
+ @PluginMethod
123
+ public void printFormattedText(PluginCall call) { run("printFormattedText", call); }
124
+
125
+ @PluginMethod
126
+ public void printFormattedTextAndCut(PluginCall call) { run("printFormattedTextAndCut", call); }
127
+
128
+ @PluginMethod
129
+ public void getEncoding(PluginCall call) { run("getEncoding", call); }
130
+
131
+ @PluginMethod
132
+ public void disconnectPrinter(PluginCall call) { run("disconnectPrinter", call); }
133
+
134
+ @Override @PluginMethod
135
+ public void requestPermissions(PluginCall call) { run("requestUSBPermissions", call); }
136
+
137
+ @PluginMethod
138
+ public void requestUSBPermissions(PluginCall call) { run("requestUSBPermissions", call); }
139
+
140
+ @PluginMethod
141
+ public void bitmapToHexadecimalString(PluginCall call) { run("bitmapToHexadecimalString", call); }
142
+
143
+ @PluginMethod
144
+ public void bytesToHexadecimalString(PluginCall call) { run("bytesToHexadecimalString", call); }
145
+
146
+ @PluginMethod
147
+ public void requestBTPermissions(PluginCall call) {
148
+ String alias = Build.VERSION.SDK_INT >= 31 ? "bluetooth" : "bluetoothLegacy";
149
+ if (getPermissionState(alias) == PermissionState.GRANTED) {
150
+ bluetoothPermissionResult(call);
151
+ } else {
152
+ requestPermissionForAlias(alias, call, "bluetoothPermissionResult");
153
+ }
154
+ }
155
+
156
+ @PermissionCallback
157
+ private void bluetoothPermissionResult(PluginCall call) {
158
+ boolean granted = getPermissionState(Build.VERSION.SDK_INT >= 31 ? "bluetooth" : "bluetoothLegacy") == PermissionState.GRANTED;
159
+ JSObject result = new JSObject().put("granted", granted);
160
+ for (String name : new String[] { "BLUETOOTH", "BLUETOOTH_ADMIN", "BLUETOOTH_CONNECT", "BLUETOOTH_SCAN" }) {
161
+ result.put(name, getActivity().checkSelfPermission("android.permission." + name) == PackageManager.PERMISSION_GRANTED);
162
+ }
163
+ call.resolve(new JSObject().put("value", result));
164
+ }
165
+
166
+ @Override
167
+ protected void handleOnDestroy() {
168
+ executor.execute(() -> {
169
+ for (DeviceConnection connection : connections.values()) {
170
+ if (connection != null) connection.disconnect();
171
+ }
172
+ connections.clear();
173
+ });
174
+ executor.shutdown();
175
+ }
176
+
177
+ private void bitmapToHexadecimalString(Result callbackContext, JSONObject data) throws JSONException {
178
+ String encodedString = data.getString("base64");
179
+ byte[] decodedString = Base64.decode(encodedString.contains(",")
180
+ ? encodedString.substring(encodedString.indexOf(",") + 1) : encodedString, Base64.DEFAULT);
181
+ data.put("bytes", decodedString);
182
+ this.bytesToHexadecimalString(callbackContext, data);
183
+ }
184
+
185
+ private void bytesToHexadecimalString(Result callbackContext, JSONObject data) throws JSONException {
186
+ EscPosPrinter printer = this.getPrinter(callbackContext, data);
187
+ try {
188
+ byte[] bytes = (byte[]) data.get("bytes");
189
+ Bitmap decodedByte = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
190
+ callbackContext.success(PrinterTextParserImg.bitmapToHexadecimalString(printer, decodedByte));
191
+ } catch (Exception e) {
192
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
193
+ put("error", e.getMessage());
194
+ }}));
195
+ }
196
+ }
197
+
198
+ private void requestUSBPermissions(Result callbackContext, JSONObject data) throws JSONException {
199
+ DeviceConnection connection = ThermalPrinterPlugin.this.getPrinterConnection(callbackContext, data);
200
+ if (!(connection instanceof UsbConnection)) {
201
+ throw new JSONException("USB printer required");
202
+ }
203
+ if (connection != null) {
204
+ String intentName = "thermalPrinterUSBRequest" + ((UsbConnection) connection).getDevice().getDeviceId();
205
+
206
+ PendingIntent permissionIntent = PendingIntent.getBroadcast(
207
+ getActivity().getBaseContext(),
208
+ 0,
209
+ new Intent(intentName).setPackage(getActivity().getPackageName()),
210
+ Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0
211
+ );
212
+
213
+ ArrayList<BroadcastReceiver> broadcastReceiverArrayList = new ArrayList<>();
214
+ BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
215
+ @Override
216
+ public void onReceive(Context context, Intent intent) {
217
+ String action = intent.getAction();
218
+ if (action != null && action.equals(intentName)) {
219
+ for (BroadcastReceiver br : broadcastReceiverArrayList) {
220
+ if (br != null) {
221
+ getActivity().unregisterReceiver(br);
222
+ }
223
+ }
224
+ synchronized (this) {
225
+ UsbManager usbManager = (UsbManager) ThermalPrinterPlugin.this.getActivity().getSystemService(Context.USB_SERVICE);
226
+ UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
227
+ if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
228
+ if (usbManager != null && usbDevice != null) {
229
+ callbackContext.success(new JSONObject(new HashMap<String, Object>() {{
230
+ put("granted", true);
231
+ }}));
232
+ return;
233
+ }
234
+ }
235
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
236
+ put("granted", false);
237
+ }}));
238
+ }
239
+ }
240
+ }
241
+ };
242
+
243
+ IntentFilter filter = new IntentFilter(intentName);
244
+ if (Build.VERSION.SDK_INT >= 33) {
245
+ getActivity().registerReceiver(broadcastReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
246
+ } else {
247
+ getActivity().registerReceiver(broadcastReceiver, filter);
248
+ }
249
+ broadcastReceiverArrayList.add(broadcastReceiver);
250
+
251
+ UsbManager usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
252
+ if (usbManager != null) {
253
+ usbManager.requestPermission(((UsbConnection) connection).getDevice(), permissionIntent);
254
+ }
255
+ }
256
+ }
257
+
258
+ private void listPrinters(Result callbackContext, JSONObject data) throws JSONException {
259
+ JSONArray printers = new JSONArray();
260
+
261
+ String type = data.getString("type");
262
+ if (type.equals("bluetooth")) {
263
+ if (!this.checkBluetooth(callbackContext)) {
264
+ return;
265
+ }
266
+ try {
267
+ BluetoothConnections printerConnections = new BluetoothConnections();
268
+ for (BluetoothConnection bluetoothConnection : printerConnections.getList()) {
269
+ BluetoothDevice bluetoothDevice = bluetoothConnection.getDevice();
270
+ JSONObject printerObj = new JSONObject();
271
+ try { printerObj.put("address", bluetoothDevice.getAddress()); } catch (Exception ignored) {}
272
+ try { printerObj.put("bondState", bluetoothDevice.getBondState()); } catch (Exception ignored) {}
273
+ try { printerObj.put("name", bluetoothDevice.getName()); } catch (Exception ignored) {}
274
+ try { printerObj.put("type", bluetoothDevice.getType()); } catch (Exception ignored) {}
275
+ try { printerObj.put("features", bluetoothDevice.getUuids()); } catch (Exception ignored) {}
276
+ try { printerObj.put("deviceClass", bluetoothDevice.getBluetoothClass().getDeviceClass()); } catch (Exception ignored) {}
277
+ try { printerObj.put("majorDeviceClass", bluetoothDevice.getBluetoothClass().getMajorDeviceClass()); } catch (Exception ignored) {}
278
+ printers.put(printerObj);
279
+ }
280
+ } catch (Exception e) {
281
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
282
+ put("error", e.getMessage());
283
+ }}));
284
+ }
285
+ } else {
286
+ UsbConnections printerConnections = new UsbConnections(getActivity());
287
+ for (UsbConnection usbConnection : printerConnections.getList()) {
288
+ UsbDevice usbDevice = usbConnection.getDevice();
289
+ JSONObject printerObj = new JSONObject();
290
+ try { printerObj.put("productName", Objects.requireNonNull(usbDevice.getProductName()).trim()); } catch (Exception ignored) {}
291
+ try { printerObj.put("manufacturerName", usbDevice.getManufacturerName()); } catch (Exception ignored) {}
292
+ try { printerObj.put("deviceId", usbDevice.getDeviceId()); } catch (Exception ignored) {}
293
+ try { printerObj.put("serialNumber", usbDevice.getSerialNumber()); } catch (Exception ignored) {}
294
+ try { printerObj.put("vendorId", usbDevice.getVendorId()); } catch (Exception ignored) {}
295
+ printers.put(printerObj);
296
+ }
297
+ }
298
+
299
+ callbackContext.success(printers);
300
+ }
301
+
302
+ private void printFormattedText(Result callbackContext, String action, JSONObject data) throws JSONException {
303
+ EscPosPrinter printer = this.getPrinter(callbackContext, data);
304
+ try {
305
+ int dotsFeedPaper = data.has("mmFeedPaper")
306
+ ? printer.mmToPx((float) data.getDouble("mmFeedPaper"))
307
+ : data.optInt("dotsFeedPaper", 20);
308
+ if (action.endsWith("Cut")) {
309
+ printer.printFormattedTextAndCut(data.getString("text"), dotsFeedPaper);
310
+ } else {
311
+ printer.printFormattedText(data.getString("text"), dotsFeedPaper);
312
+ }
313
+ callbackContext.success();
314
+ } catch (EscPosConnectionException e) {
315
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
316
+ put("error", e.getMessage());
317
+ }}));
318
+ } catch (Exception e) {
319
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
320
+ put("error", e.getMessage());
321
+ }}));
322
+ }
323
+ }
324
+
325
+ private void getEncoding(Result callbackContext, JSONObject data) throws JSONException {
326
+ EscPosPrinter printer = this.getPrinter(callbackContext, data);
327
+ EscPosCharsetEncoding encoding = printer.getEncoding();
328
+ if (encoding == null) {
329
+ callbackContext.success("null");
330
+ } else {
331
+ JSONArray command = new JSONArray();
332
+ for (byte value : encoding.getCommand()) command.put((int) value);
333
+ callbackContext.success(new JSONObject().put("name", encoding.getName()).put("command", command));
334
+ }
335
+ }
336
+
337
+ private void disconnectPrinter(Result callbackContext, JSONObject data) throws JSONException {
338
+ EscPosPrinter printer = this.getPrinter(callbackContext, data);
339
+ printer.disconnectPrinter();
340
+ callbackContext.success();
341
+ }
342
+
343
+ private DeviceConnection getDevice(Result callbackContext, String type, String id, String address, int port) {
344
+ String hashKey = type + "-" + id;
345
+ if (this.connections.containsKey(hashKey)) {
346
+ DeviceConnection connection = this.connections.get(hashKey);
347
+ if (connection != null) {
348
+ if (connection.isConnected()) {
349
+ return connection;
350
+ } else {
351
+ this.connections.remove(hashKey);
352
+ }
353
+ }
354
+ }
355
+
356
+ if (type.equals("bluetooth")) {
357
+ if (!this.checkBluetooth(callbackContext)) {
358
+ return null;
359
+ }
360
+ if (id.equals("first")) {
361
+ return BluetoothPrintersConnections.selectFirstPaired();
362
+ }
363
+ BluetoothConnections printerConnections = new BluetoothConnections();
364
+ for (BluetoothConnection bluetoothConnection : printerConnections.getList()) {
365
+ BluetoothDevice bluetoothDevice = bluetoothConnection.getDevice();
366
+ try { if (bluetoothDevice.getAddress().equals(id)) { return bluetoothConnection; } } catch (Exception ignored) {}
367
+ try { if (bluetoothDevice.getName().equals(id)) { return bluetoothConnection; } } catch (Exception ignored) {}
368
+ }
369
+ } else if (type.equals("tcp")) {
370
+ return new TcpConnection(address, port);
371
+ } else {
372
+ UsbConnections printerConnections = new UsbConnections(getActivity());
373
+ for (UsbConnection usbConnection : printerConnections.getList()) {
374
+ UsbDevice usbDevice = usbConnection.getDevice();
375
+ try { if (usbDevice.getDeviceId() == Integer.parseInt(id)) { return usbConnection; } } catch (Exception ignored) {}
376
+ try { if (Objects.requireNonNull(usbDevice.getProductName()).trim().equals(id)) { return usbConnection; } } catch (Exception ignored) {}
377
+ }
378
+ }
379
+
380
+ return null;
381
+ }
382
+
383
+ private EscPosPrinter getPrinter(Result callbackContext, JSONObject data) throws JSONException {
384
+ DeviceConnection deviceConnection = this.getPrinterConnection(callbackContext, data);
385
+ if (deviceConnection == null) {
386
+ throw new JSONException("Device not found");
387
+ }
388
+
389
+ EscPosCharsetEncoding charsetEncoding = null;
390
+ try {
391
+ if (data.optJSONObject("charsetEncoding") != null) {
392
+ JSONObject charsetEncodingData = data.optJSONObject("charsetEncoding");
393
+ if (charsetEncodingData == null) {
394
+ charsetEncodingData = new JSONObject();
395
+ }
396
+ charsetEncoding = new EscPosCharsetEncoding(
397
+ charsetEncodingData.optString("charsetName", "windows-1252"),
398
+ charsetEncodingData.optInt("charsetId", 16)
399
+ );
400
+ }
401
+ } catch (Exception exception) {
402
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
403
+ put("error", exception.getMessage());
404
+ }}));
405
+ throw new JSONException(exception.getMessage());
406
+ }
407
+
408
+ try {
409
+ return new EscPosPrinter(
410
+ deviceConnection,
411
+ data.optInt("printerDpi", 203),
412
+ (float) data.optDouble("printerWidthMM", 48f),
413
+ data.optInt("printerNbrCharactersPerLine", 32),
414
+ charsetEncoding
415
+ );
416
+ } catch (Exception e) {
417
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
418
+ put("error", e.getMessage());
419
+ }}));
420
+ throw new JSONException(e.getMessage());
421
+ }
422
+ }
423
+
424
+ private DeviceConnection getPrinterConnection(Result callbackContext, JSONObject data) throws JSONException {
425
+ String type = data.getString("type");
426
+ String id = data.getString("id");
427
+ String hashKey = type + "-" + id;
428
+ DeviceConnection deviceConnection = this.getDevice(
429
+ callbackContext,
430
+ data.getString("type"),
431
+ data.optString("id"),
432
+ data.optString("address"),
433
+ data.optInt("port", 9100)
434
+ );
435
+ if (deviceConnection == null) {
436
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
437
+ put("error", "Device not found or not connected!");
438
+ put("type", type);
439
+ put("id", id);
440
+ }}));
441
+ }
442
+ if (!this.connections.containsKey(hashKey)) {
443
+ this.connections.put(hashKey, deviceConnection);
444
+ }
445
+ return deviceConnection;
446
+ }
447
+
448
+ private boolean checkBluetooth(Result callbackContext) {
449
+ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
450
+ if (mBluetoothAdapter == null) {
451
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
452
+ put("error", "Device doesn't support Bluetooth!");
453
+ }}));
454
+ return false;
455
+ } else if (!mBluetoothAdapter.isEnabled()) {
456
+ callbackContext.error(new JSONObject(new HashMap<String, Object>() {{
457
+ put("error", "Device not enabled Bluetooth!");
458
+ }}));
459
+ return false;
460
+ }
461
+ return true;
462
+ }
463
+ }
@@ -0,0 +1,73 @@
1
+ export interface Printer {
2
+ address?: string;
3
+ bondState?: number;
4
+ name?: string;
5
+ type?: number;
6
+ features?: string[];
7
+ deviceClass?: number;
8
+ majorDeviceClass?: number;
9
+ productName?: string;
10
+ manufacturerName?: string;
11
+ deviceId?: number;
12
+ serialNumber?: string;
13
+ vendorId?: number;
14
+ }
15
+ export interface PrinterToUse {
16
+ type: 'bluetooth' | 'tcp' | 'usb';
17
+ id: string | number;
18
+ address?: string;
19
+ port?: number;
20
+ mmFeedPaper?: number;
21
+ dotsFeedPaper?: number;
22
+ printerDpi?: number;
23
+ printerWidthMM?: number;
24
+ printerNbrCharactersPerLine?: number;
25
+ charsetEncoding?: {
26
+ charsetName: string;
27
+ charsetId: number;
28
+ };
29
+ }
30
+ export interface PrintFormattedText extends PrinterToUse {
31
+ text: string;
32
+ }
33
+ export interface BitmapToHexadecimalString extends PrinterToUse {
34
+ base64: string;
35
+ }
36
+ export interface BytesToHexadecimalString extends PrinterToUse {
37
+ bytes: number[];
38
+ }
39
+ export interface RequestPermissionsResult {
40
+ granted: boolean;
41
+ BLUETOOTH?: boolean;
42
+ BLUETOOTH_ADMIN?: boolean;
43
+ BLUETOOTH_CONNECT?: boolean;
44
+ BLUETOOTH_SCAN?: boolean;
45
+ }
46
+ export interface GetEncodingResult {
47
+ name: string;
48
+ command: number[];
49
+ }
50
+ export interface ErrorResult {
51
+ error?: string;
52
+ }
53
+ export interface ThermalPrinterPlugin {
54
+ listPrinters(data: {
55
+ type: 'bluetooth' | 'usb';
56
+ }): Promise<Printer[]>;
57
+ printFormattedText(data: PrintFormattedText): Promise<void>;
58
+ printFormattedTextAndCut(data: PrintFormattedText): Promise<void>;
59
+ getEncoding(data: PrinterToUse): Promise<GetEncodingResult | 'null'>;
60
+ disconnectPrinter(data: PrinterToUse): Promise<void>;
61
+ requestPermissions(data: PrinterToUse): Promise<RequestPermissionsResult>;
62
+ requestBTPermissions(data: {
63
+ type: 'bluetooth';
64
+ }): Promise<RequestPermissionsResult>;
65
+ requestUSBPermissions(data: PrinterToUse): Promise<RequestPermissionsResult>;
66
+ bitmapToHexadecimalString(data: BitmapToHexadecimalString): Promise<string>;
67
+ bytesToHexadecimalString(data: BytesToHexadecimalString): Promise<string>;
68
+ }
69
+ export type ThermalPrinterNativePlugin = {
70
+ [K in keyof ThermalPrinterPlugin]: (data: Parameters<ThermalPrinterPlugin[K]>[0]) => Promise<{
71
+ value: Awaited<ReturnType<ThermalPrinterPlugin[K]>>;
72
+ }>;
73
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { ThermalPrinterPlugin } from './definitions';
2
+ export declare const ThermalPrinter: ThermalPrinterPlugin;
3
+ export * from './definitions';
@@ -0,0 +1,23 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const native = registerPlugin('ThermalPrinter', {
3
+ web: () => import('./web').then(m => new m.ThermalPrinterWeb()),
4
+ });
5
+ const unwrap = (request) => request.then(result => result.value).catch(error => {
6
+ if (error && typeof error === 'object' && error.data && typeof error.data === 'object') {
7
+ Object.assign(error, error.data);
8
+ }
9
+ throw error;
10
+ });
11
+ export const ThermalPrinter = {
12
+ listPrinters: data => unwrap(native.listPrinters(data)),
13
+ printFormattedText: data => unwrap(native.printFormattedText(data)),
14
+ printFormattedTextAndCut: data => unwrap(native.printFormattedTextAndCut(data)),
15
+ getEncoding: data => unwrap(native.getEncoding(data)),
16
+ disconnectPrinter: data => unwrap(native.disconnectPrinter(data)),
17
+ requestPermissions: data => unwrap(native.requestPermissions(data)),
18
+ requestBTPermissions: data => unwrap(native.requestBTPermissions(data)),
19
+ requestUSBPermissions: data => unwrap(native.requestUSBPermissions(data)),
20
+ bitmapToHexadecimalString: data => unwrap(native.bitmapToHexadecimalString(data)),
21
+ bytesToHexadecimalString: data => unwrap(native.bytesToHexadecimalString(data)),
22
+ };
23
+ export * from './definitions';
@@ -0,0 +1,13 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ export declare class ThermalPrinterWeb extends WebPlugin {
3
+ listPrinters(): Promise<never>;
4
+ printFormattedText(): Promise<never>;
5
+ printFormattedTextAndCut(): Promise<never>;
6
+ getEncoding(): Promise<never>;
7
+ disconnectPrinter(): Promise<never>;
8
+ requestPermissions(): Promise<never>;
9
+ requestBTPermissions(): Promise<never>;
10
+ requestUSBPermissions(): Promise<never>;
11
+ bitmapToHexadecimalString(): Promise<never>;
12
+ bytesToHexadecimalString(): Promise<never>;
13
+ }
@@ -0,0 +1,33 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ export class ThermalPrinterWeb extends WebPlugin {
3
+ async listPrinters() {
4
+ throw this.unavailable('Thermal printing is unavailable on the web.');
5
+ }
6
+ async printFormattedText() {
7
+ throw this.unavailable('Thermal printing is unavailable on the web.');
8
+ }
9
+ async printFormattedTextAndCut() {
10
+ throw this.unavailable('Thermal printing is unavailable on the web.');
11
+ }
12
+ async getEncoding() {
13
+ throw this.unavailable('Thermal printing is unavailable on the web.');
14
+ }
15
+ async disconnectPrinter() {
16
+ throw this.unavailable('Thermal printing is unavailable on the web.');
17
+ }
18
+ async requestPermissions() {
19
+ throw this.unavailable('Thermal printing is unavailable on the web.');
20
+ }
21
+ async requestBTPermissions() {
22
+ throw this.unavailable('Thermal printing is unavailable on the web.');
23
+ }
24
+ async requestUSBPermissions() {
25
+ throw this.unavailable('Thermal printing is unavailable on the web.');
26
+ }
27
+ async bitmapToHexadecimalString() {
28
+ throw this.unavailable('Thermal printing is unavailable on the web.');
29
+ }
30
+ async bytesToHexadecimalString() {
31
+ throw this.unavailable('Thermal printing is unavailable on the web.');
32
+ }
33
+ }
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ var core = require('@capacitor/core');
4
+
5
+ const native = core.registerPlugin('ThermalPrinter', {
6
+ web: () => Promise.resolve().then(function () { return web; }).then(m => new m.ThermalPrinterWeb()),
7
+ });
8
+ const unwrap = (request) => request.then(result => result.value).catch(error => {
9
+ if (error && typeof error === 'object' && error.data && typeof error.data === 'object') {
10
+ Object.assign(error, error.data);
11
+ }
12
+ throw error;
13
+ });
14
+ const ThermalPrinter = {
15
+ listPrinters: data => unwrap(native.listPrinters(data)),
16
+ printFormattedText: data => unwrap(native.printFormattedText(data)),
17
+ printFormattedTextAndCut: data => unwrap(native.printFormattedTextAndCut(data)),
18
+ getEncoding: data => unwrap(native.getEncoding(data)),
19
+ disconnectPrinter: data => unwrap(native.disconnectPrinter(data)),
20
+ requestPermissions: data => unwrap(native.requestPermissions(data)),
21
+ requestBTPermissions: data => unwrap(native.requestBTPermissions(data)),
22
+ requestUSBPermissions: data => unwrap(native.requestUSBPermissions(data)),
23
+ bitmapToHexadecimalString: data => unwrap(native.bitmapToHexadecimalString(data)),
24
+ bytesToHexadecimalString: data => unwrap(native.bytesToHexadecimalString(data)),
25
+ };
26
+
27
+ class ThermalPrinterWeb extends core.WebPlugin {
28
+ async listPrinters() {
29
+ throw this.unavailable('Thermal printing is unavailable on the web.');
30
+ }
31
+ async printFormattedText() {
32
+ throw this.unavailable('Thermal printing is unavailable on the web.');
33
+ }
34
+ async printFormattedTextAndCut() {
35
+ throw this.unavailable('Thermal printing is unavailable on the web.');
36
+ }
37
+ async getEncoding() {
38
+ throw this.unavailable('Thermal printing is unavailable on the web.');
39
+ }
40
+ async disconnectPrinter() {
41
+ throw this.unavailable('Thermal printing is unavailable on the web.');
42
+ }
43
+ async requestPermissions() {
44
+ throw this.unavailable('Thermal printing is unavailable on the web.');
45
+ }
46
+ async requestBTPermissions() {
47
+ throw this.unavailable('Thermal printing is unavailable on the web.');
48
+ }
49
+ async requestUSBPermissions() {
50
+ throw this.unavailable('Thermal printing is unavailable on the web.');
51
+ }
52
+ async bitmapToHexadecimalString() {
53
+ throw this.unavailable('Thermal printing is unavailable on the web.');
54
+ }
55
+ async bytesToHexadecimalString() {
56
+ throw this.unavailable('Thermal printing is unavailable on the web.');
57
+ }
58
+ }
59
+
60
+ var web = /*#__PURE__*/Object.freeze({
61
+ __proto__: null,
62
+ ThermalPrinterWeb: ThermalPrinterWeb
63
+ });
64
+
65
+ exports.ThermalPrinter = ThermalPrinter;