@simplymobile/capacitor-bixolon-sdk-printer 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.
@@ -0,0 +1,983 @@
1
+ package pl.simplymobile.plugins.bixolonsdkprinter;
2
+
3
+ import android.annotation.SuppressLint;
4
+ import android.app.AlertDialog;
5
+ import android.bluetooth.BluetoothDevice;
6
+ import android.content.DialogInterface;
7
+ import android.graphics.Bitmap;
8
+ import android.graphics.BitmapFactory;
9
+ import android.os.Build;
10
+ import android.os.Bundle;
11
+ import android.os.Handler;
12
+ import android.os.Message;
13
+ import android.util.Base64;
14
+ import android.widget.Toast;
15
+ import com.bixolon.printer.BixolonPrinter;
16
+ import com.getcapacitor.JSArray;
17
+ import com.getcapacitor.JSObject;
18
+ import com.getcapacitor.PermissionState;
19
+ import com.getcapacitor.Plugin;
20
+ import com.getcapacitor.PluginCall;
21
+ import com.getcapacitor.PluginMethod;
22
+ import com.getcapacitor.annotation.CapacitorPlugin;
23
+ import com.getcapacitor.annotation.Permission;
24
+ import com.getcapacitor.annotation.PermissionCallback;
25
+ import java.util.HashMap;
26
+ import java.util.Map;
27
+ import java.util.Set;
28
+ import org.json.JSONException;
29
+ import org.json.JSONObject;
30
+
31
+ @CapacitorPlugin(
32
+ name = "BixolonSdkPrinter",
33
+ permissions = {
34
+ @Permission(
35
+ alias = BixolonSdkPrinterPlugin.BLUETOOTH_ALIAS,
36
+ strings = { "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_SCAN" }
37
+ )
38
+ }
39
+ )
40
+ public class BixolonSdkPrinterPlugin extends Plugin {
41
+
42
+ public static final String BLUETOOTH_ALIAS = "bluetooth";
43
+
44
+ // Actions
45
+ private static final String ACTION_PRINT_TEXT = "printText";
46
+ private static final String ACTION_PRINT_1D_BARCODE = "printBarCode";
47
+ private static final String ACTION_PRINT_QRCODE = "printQRCode";
48
+ private static final String ACTION_GET_STATUS = "getStatus";
49
+ private static final String ACTION_CUT_PAPER = "cutPaper";
50
+ private static final String ACTION_PRINT_BITMAP = "printBitmap";
51
+ private static final String ACTION_RECONNECT = "reconnect";
52
+ private static final String ACTION_DISCONNECT = "disconnect";
53
+ private static final String ACTION_START_MSR_READER_LISTENER = "startMsrReaderListener";
54
+ private static final String ACTION_START_CONNECTION_LISTENER = "startConnectionListener";
55
+
56
+ // Alignment strings
57
+ private static final String ALIGNMENT_LEFT = "LEFT";
58
+ private static final String ALIGNMENT_CENTER = "CENTER";
59
+ private static final String ALIGNMENT_RIGHT = "RIGHT";
60
+
61
+ @SuppressWarnings("serial")
62
+ private static final Map<String, Integer> MAX_COL = new HashMap<String, Integer>() {
63
+ {
64
+ put("SPP-R200", 0);
65
+ put("SPP-100", 0);
66
+ put("SRP-F310", 0);
67
+ put("SRP-350II", 0);
68
+ put("SRP-350plusII", 0);
69
+ put("SRP-F312", 0);
70
+ put("SRP-350IIK", 0);
71
+ put("SPP-R200II", 0);
72
+ put("SPP-R300", 48);
73
+ put("SPP-R400", 69);
74
+ }
75
+ };
76
+
77
+ private static final int STATUS_STEP_GET_STATUS = 0;
78
+ private static final int STATUS_STEP_GET_BATTERY_STATUS = 1;
79
+ private static final int STATUS_STEP_GET_PRINTER_ID_FIRMWARE_VERSION = 2;
80
+ private static final int STATUS_STEP_GET_PRINTER_ID_MANUFACTURER = 3;
81
+ private static final int STATUS_STEP_GET_PRINTER_ID_PRINTER_MODEL = 4;
82
+ private static final int STATUS_STEP_GET_PRINTER_ID_CODE_PAGE = 5;
83
+ private static final int STATUS_STEP_COMPLETE = 10;
84
+
85
+ static BixolonPrinter mBixolonPrinter;
86
+
87
+ private PluginCall currentCall;
88
+ private String lastActionName;
89
+ private String actionSuccess;
90
+ private String actionError;
91
+
92
+ private boolean isValidAction;
93
+ private boolean optAutoConnect;
94
+ private boolean optToastMessage;
95
+
96
+ private int statusStep = 0;
97
+
98
+ private String mConnectedDeviceName;
99
+ private String mConnectedDeviceAddress;
100
+ private JSObject mConnectedDeviceStatus;
101
+ private boolean mIsConnected;
102
+
103
+ // Listener flags. The Cordova plugin used keep-callbacks; here we emit Capacitor events.
104
+ private boolean msrListening;
105
+ private boolean connectionListening;
106
+
107
+ @Override
108
+ public void load() {
109
+ super.load();
110
+ mBixolonPrinter = new BixolonPrinter(getActivity(), mHandler, null);
111
+ mIsConnected = false;
112
+ mConnectedDeviceName = null;
113
+ mConnectedDeviceAddress = null;
114
+ mConnectedDeviceStatus = null;
115
+ actionSuccess = null;
116
+ actionError = null;
117
+ lastActionName = null;
118
+ }
119
+
120
+ @Override
121
+ protected void handleOnDestroy() {
122
+ super.handleOnDestroy();
123
+ isValidAction = false;
124
+ if (mBixolonPrinter != null) {
125
+ mBixolonPrinter.disconnect();
126
+ }
127
+ }
128
+
129
+ // region Permission handling
130
+
131
+ private boolean ensureBluetoothPermission(PluginCall call) {
132
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
133
+ return true;
134
+ }
135
+ if (getPermissionState(BLUETOOTH_ALIAS) == PermissionState.GRANTED) {
136
+ return true;
137
+ }
138
+ requestPermissionForAlias(BLUETOOTH_ALIAS, call, "bluetoothPermsCallback");
139
+ return false;
140
+ }
141
+
142
+ @PermissionCallback
143
+ private void bluetoothPermsCallback(PluginCall call) {
144
+ if (getPermissionState(BLUETOOTH_ALIAS) != PermissionState.GRANTED) {
145
+ call.reject("Bluetooth permission denied");
146
+ return;
147
+ }
148
+ switch (call.getMethodName()) {
149
+ case ACTION_PRINT_TEXT:
150
+ printText(call);
151
+ break;
152
+ case ACTION_PRINT_1D_BARCODE:
153
+ printBarCode(call);
154
+ break;
155
+ case ACTION_PRINT_QRCODE:
156
+ printQRCode(call);
157
+ break;
158
+ case ACTION_PRINT_BITMAP:
159
+ printBitmap(call);
160
+ break;
161
+ case ACTION_CUT_PAPER:
162
+ cutPaper(call);
163
+ break;
164
+ case ACTION_GET_STATUS:
165
+ getStatus(call);
166
+ break;
167
+ case ACTION_RECONNECT:
168
+ reconnect(call);
169
+ break;
170
+ case ACTION_START_MSR_READER_LISTENER:
171
+ startMsrReaderListener(call);
172
+ break;
173
+ case ACTION_START_CONNECTION_LISTENER:
174
+ startConnectionListener(call);
175
+ break;
176
+ default:
177
+ call.reject("Unknown method after permission grant: " + call.getMethodName());
178
+ break;
179
+ }
180
+ }
181
+
182
+ // endregion
183
+
184
+ // region Plugin methods
185
+
186
+ @PluginMethod
187
+ public void printText(PluginCall call) {
188
+ if (!ensureBluetoothPermission(call)) {
189
+ return;
190
+ }
191
+ beginAction(call, ACTION_PRINT_TEXT, configAutoConnect(call), configToastMessage(call));
192
+ }
193
+
194
+ @PluginMethod
195
+ public void printBarCode(PluginCall call) {
196
+ if (!ensureBluetoothPermission(call)) {
197
+ return;
198
+ }
199
+ beginAction(call, ACTION_PRINT_1D_BARCODE, configAutoConnect(call), configToastMessage(call));
200
+ }
201
+
202
+ @PluginMethod
203
+ public void printQRCode(PluginCall call) {
204
+ if (!ensureBluetoothPermission(call)) {
205
+ return;
206
+ }
207
+ beginAction(call, ACTION_PRINT_QRCODE, configAutoConnect(call), configToastMessage(call));
208
+ }
209
+
210
+ @PluginMethod
211
+ public void printBitmap(PluginCall call) {
212
+ if (!ensureBluetoothPermission(call)) {
213
+ return;
214
+ }
215
+ if (call.getString("base64Image") == null) {
216
+ call.reject("Missing required parameter: base64Image");
217
+ return;
218
+ }
219
+ beginAction(call, ACTION_PRINT_BITMAP, true, true);
220
+ }
221
+
222
+ @PluginMethod
223
+ public void cutPaper(PluginCall call) {
224
+ if (!ensureBluetoothPermission(call)) {
225
+ return;
226
+ }
227
+ beginAction(call, ACTION_CUT_PAPER, configAutoConnect(call), configToastMessage(call));
228
+ }
229
+
230
+ @PluginMethod
231
+ public void getStatus(PluginCall call) {
232
+ if (!ensureBluetoothPermission(call)) {
233
+ return;
234
+ }
235
+ beginAction(call, ACTION_GET_STATUS, configAutoConnect(call), configToastMessage(call));
236
+ }
237
+
238
+ @PluginMethod
239
+ public void reconnect(PluginCall call) {
240
+ if (!ensureBluetoothPermission(call)) {
241
+ return;
242
+ }
243
+ beginAction(call, ACTION_RECONNECT, true, true);
244
+ }
245
+
246
+ @PluginMethod
247
+ public void disconnect(PluginCall call) {
248
+ currentCall = call;
249
+ lastActionName = ACTION_DISCONNECT;
250
+ if (!mIsConnected) {
251
+ currentCall = null;
252
+ call.resolve();
253
+ return;
254
+ }
255
+ mBixolonPrinter.disconnect();
256
+ }
257
+
258
+ @PluginMethod
259
+ public void startMsrReaderListener(PluginCall call) {
260
+ if (!ensureBluetoothPermission(call)) {
261
+ return;
262
+ }
263
+ if (msrListening) {
264
+ call.reject("MSR Reader listener already started.");
265
+ return;
266
+ }
267
+ msrListening = true;
268
+ beginAction(call, ACTION_START_MSR_READER_LISTENER, true, true);
269
+ }
270
+
271
+ @PluginMethod
272
+ public void stopMsrReaderListener(PluginCall call) {
273
+ msrListening = false;
274
+ call.resolve();
275
+ }
276
+
277
+ @PluginMethod
278
+ public void startConnectionListener(PluginCall call) {
279
+ if (!ensureBluetoothPermission(call)) {
280
+ return;
281
+ }
282
+ if (connectionListening) {
283
+ call.reject("Connection listener already started.");
284
+ return;
285
+ }
286
+ connectionListening = true;
287
+ beginAction(call, ACTION_START_CONNECTION_LISTENER, true, true);
288
+ }
289
+
290
+ @PluginMethod
291
+ public void stopConnectionListener(PluginCall call) {
292
+ connectionListening = false;
293
+ call.resolve();
294
+ }
295
+
296
+ // endregion
297
+
298
+ // region Action lifecycle
299
+
300
+ private void beginAction(PluginCall call, String action, boolean autoConnect, boolean toastMessage) {
301
+ isValidAction = true;
302
+ currentCall = call;
303
+ lastActionName = action;
304
+ optAutoConnect = autoConnect;
305
+ optToastMessage = toastMessage;
306
+ connect();
307
+ }
308
+
309
+ private void connect() {
310
+ if (mIsConnected) {
311
+ onConnect();
312
+ } else {
313
+ mBixolonPrinter.findBluetoothPrinters();
314
+ }
315
+ }
316
+
317
+ private void onConnect() {
318
+ mIsConnected = true;
319
+
320
+ if (ACTION_PRINT_TEXT.equals(lastActionName)) {
321
+ doPrintText();
322
+ } else if (ACTION_PRINT_1D_BARCODE.equals(lastActionName)) {
323
+ doPrintBarCode();
324
+ } else if (ACTION_PRINT_QRCODE.equals(lastActionName)) {
325
+ doPrintQRCode();
326
+ } else if (ACTION_CUT_PAPER.equals(lastActionName)) {
327
+ doCutPaper();
328
+ } else if (ACTION_GET_STATUS.equals(lastActionName)) {
329
+ doGetStatus();
330
+ } else if (ACTION_PRINT_BITMAP.equals(lastActionName)) {
331
+ doPrintBitmap();
332
+ } else if (ACTION_RECONNECT.equals(lastActionName)) {
333
+ resolveCurrentMessage("reconnected");
334
+ } else if (ACTION_START_MSR_READER_LISTENER.equals(lastActionName) || ACTION_START_CONNECTION_LISTENER.equals(lastActionName)) {
335
+ // Listener established; keep the connection open and resolve the start call.
336
+ if (currentCall != null) {
337
+ currentCall.resolve();
338
+ currentCall = null;
339
+ }
340
+ }
341
+
342
+ if (connectionListening) {
343
+ notifyListeners("connectionChanged", createConnectionData(null));
344
+ }
345
+ }
346
+
347
+ private void disconnect() {
348
+ mBixolonPrinter.disconnect();
349
+ }
350
+
351
+ private void onDisconnect() {
352
+ String action = lastActionName;
353
+ String error = actionError;
354
+ String success = actionSuccess;
355
+ JSObject status = mConnectedDeviceStatus;
356
+
357
+ if (!mIsConnected && isValidAction) {
358
+ if (currentCall != null) {
359
+ currentCall.reject("Connection failed");
360
+ currentCall = null;
361
+ }
362
+ return;
363
+ }
364
+
365
+ mIsConnected = false;
366
+ mConnectedDeviceName = null;
367
+ mConnectedDeviceAddress = null;
368
+ mConnectedDeviceStatus = null;
369
+ actionSuccess = null;
370
+ actionError = null;
371
+ lastActionName = null;
372
+ statusStep = STATUS_STEP_GET_STATUS;
373
+
374
+ if (currentCall != null) {
375
+ if (error != null) {
376
+ currentCall.reject(error);
377
+ } else if (ACTION_GET_STATUS.equals(action)) {
378
+ currentCall.resolve(status != null ? status : new JSObject());
379
+ } else if (ACTION_DISCONNECT.equals(action)) {
380
+ currentCall.resolve();
381
+ } else {
382
+ JSObject ret = new JSObject();
383
+ ret.put("message", success);
384
+ currentCall.resolve(ret);
385
+ }
386
+ currentCall = null;
387
+ }
388
+
389
+ if (connectionListening) {
390
+ notifyListeners("connectionChanged", createConnectionData(null));
391
+ }
392
+ }
393
+
394
+ private void onPrintComplete() {
395
+ disconnect();
396
+ }
397
+
398
+ // endregion
399
+
400
+ // region Action implementations
401
+
402
+ private void doPrintText() {
403
+ String hrBCode = "[hr]";
404
+ int paperWidth = 0;
405
+
406
+ JSArray lines = currentCall.getArray("lines");
407
+ JSObject config = configOf(currentCall);
408
+ boolean formFeed = config.optBoolean("formFeed", true);
409
+ int lineFeed = config.optInt("lineFeed", 0);
410
+ int codePage = config.optInt("codePage", 16);
411
+
412
+ if (lines == null) {
413
+ failAction("print error: Missing required parameter: lines");
414
+ return;
415
+ }
416
+
417
+ if (mConnectedDeviceName != null && MAX_COL.containsKey(mConnectedDeviceName)) {
418
+ paperWidth = MAX_COL.get(mConnectedDeviceName);
419
+ }
420
+
421
+ mBixolonPrinter.setSingleByteFont(codePage);
422
+
423
+ int arlength = lines.length();
424
+ for (int i = 0; i < arlength; i++) {
425
+ try {
426
+ JSONObject textLine = lines.getJSONObject(i);
427
+ String text = textLine.optString("text");
428
+ String align = textLine.optString("textAlign");
429
+ int width = textLine.optInt("textWidth");
430
+ int height = textLine.optInt("textHeight");
431
+ String fontType = textLine.optString("fontType");
432
+ String fontStyle = textLine.optString("fontStyle");
433
+
434
+ if (hrBCode.equals(text.substring(0, Math.min(4, text.length())))) {
435
+ String hrStr = text.substring(text.length() - 1);
436
+ StringBuilder sb = new StringBuilder();
437
+ for (int j = 0; j < paperWidth; j++) {
438
+ sb.append(hrStr);
439
+ }
440
+ sb.append("\r\n");
441
+ text = sb.toString();
442
+ }
443
+
444
+ int textAlignment = getAlignment(align);
445
+ int textAttribute = getAttribute(fontType, fontStyle);
446
+ int textSize = getTextSize(width, height);
447
+
448
+ mBixolonPrinter.printText(text, textAlignment, textAttribute, textSize, false);
449
+ } catch (JSONException e2) {
450
+ failAction("print error: " + e2.getMessage());
451
+ return;
452
+ }
453
+ }
454
+
455
+ if (formFeed) {
456
+ mBixolonPrinter.formFeed(false);
457
+ } else {
458
+ mBixolonPrinter.lineFeed(lineFeed, false);
459
+ }
460
+
461
+ mBixolonPrinter.cutPaper(true);
462
+ mBixolonPrinter.kickOutDrawer(BixolonPrinter.DRAWER_CONNECTOR_PIN5);
463
+
464
+ actionSuccess = "print success";
465
+ }
466
+
467
+ private void doPrintBarCode() {
468
+ JSObject data = currentCall.getObject("data");
469
+ JSObject config = configOf(currentCall);
470
+ boolean formFeed = config.optBoolean("formFeed", true);
471
+
472
+ if (data == null) {
473
+ failAction("print error: Missing required parameter: data");
474
+ return;
475
+ }
476
+
477
+ String text = data.optString("text");
478
+ String align = data.optString("alignment");
479
+ int width = data.optInt("width");
480
+ int height = data.optInt("height");
481
+ int barCodeSystem = data.optInt("barcodeSystem");
482
+ int barcodeAlignment = getAlignment(align);
483
+ int characterPosition = data.optInt("characterPosition");
484
+
485
+ try {
486
+ if (formFeed) {
487
+ mBixolonPrinter.print1dBarcode(text, barCodeSystem, barcodeAlignment, width, height, characterPosition, false);
488
+ mBixolonPrinter.formFeed(true);
489
+ } else {
490
+ mBixolonPrinter.print1dBarcode(text, barCodeSystem, barcodeAlignment, width, height, characterPosition, true);
491
+ }
492
+ } catch (Exception e2) {
493
+ failAction("print error: " + e2.getMessage());
494
+ return;
495
+ }
496
+
497
+ actionSuccess = "print success";
498
+ }
499
+
500
+ private void doPrintQRCode() {
501
+ JSObject data = currentCall.getObject("data");
502
+ JSObject config = configOf(currentCall);
503
+ boolean formFeed = config.optBoolean("formFeed", true);
504
+
505
+ if (data == null) {
506
+ failAction("print error: Missing required parameter: data");
507
+ return;
508
+ }
509
+
510
+ String text = data.optString("text");
511
+ String align = data.optString("alignment");
512
+ int size = data.optInt("size");
513
+ int model = data.optInt("model");
514
+ int qrcodeAlignment = getAlignment(align);
515
+
516
+ try {
517
+ if (formFeed) {
518
+ mBixolonPrinter.printQrCode(text, qrcodeAlignment, model, size, false);
519
+ mBixolonPrinter.formFeed(true);
520
+ } else {
521
+ mBixolonPrinter.printQrCode(text, qrcodeAlignment, model, size, true);
522
+ }
523
+ } catch (Exception e2) {
524
+ failAction("print error: " + e2.getMessage());
525
+ return;
526
+ }
527
+
528
+ actionSuccess = "print success";
529
+ }
530
+
531
+ private void doPrintBitmap() {
532
+ String base64Image = currentCall.getString("base64Image");
533
+ int width = currentCall.getInt("width", 100);
534
+ int brightness = currentCall.getInt("brightness", 50);
535
+ int alignment = currentCall.getInt("alignment", -2);
536
+
537
+ try {
538
+ Bitmap bmp = decodeBase64Image(base64Image);
539
+ mBixolonPrinter.printBitmap(bmp, alignment, width, brightness, false);
540
+ } catch (Exception e2) {
541
+ failAction("print error: " + e2.getMessage());
542
+ return;
543
+ }
544
+
545
+ actionSuccess = "print success";
546
+ }
547
+
548
+ private void doCutPaper() {
549
+ JSObject config = configOf(currentCall);
550
+ boolean formFeed = config.optBoolean("formFeed", true);
551
+ int lineFeed = config.optInt("lineFeed", 0);
552
+
553
+ if (formFeed) {
554
+ mBixolonPrinter.formFeed(false);
555
+ } else {
556
+ mBixolonPrinter.lineFeed(lineFeed, false);
557
+ }
558
+
559
+ mBixolonPrinter.cutPaper(true);
560
+ mBixolonPrinter.kickOutDrawer(BixolonPrinter.DRAWER_CONNECTOR_PIN5);
561
+
562
+ actionSuccess = "cut paper success";
563
+ }
564
+
565
+ private void doGetStatus() {
566
+ switch (statusStep) {
567
+ case STATUS_STEP_GET_STATUS:
568
+ mConnectedDeviceStatus = new JSObject();
569
+ mConnectedDeviceStatus.put("printerName", mConnectedDeviceName);
570
+ mConnectedDeviceStatus.put("printerAddress", mConnectedDeviceAddress);
571
+ mBixolonPrinter.getStatus();
572
+ return;
573
+ case STATUS_STEP_GET_BATTERY_STATUS:
574
+ mBixolonPrinter.getBatteryStatus();
575
+ return;
576
+ case STATUS_STEP_GET_PRINTER_ID_FIRMWARE_VERSION:
577
+ mBixolonPrinter.getPrinterId(BixolonPrinter.PRINTER_ID_FIRMWARE_VERSION);
578
+ return;
579
+ case STATUS_STEP_GET_PRINTER_ID_MANUFACTURER:
580
+ mBixolonPrinter.getPrinterId(BixolonPrinter.PRINTER_ID_MANUFACTURER);
581
+ return;
582
+ case STATUS_STEP_GET_PRINTER_ID_PRINTER_MODEL:
583
+ mBixolonPrinter.getPrinterId(BixolonPrinter.PRINTER_ID_PRINTER_MODEL);
584
+ return;
585
+ case STATUS_STEP_GET_PRINTER_ID_CODE_PAGE:
586
+ mBixolonPrinter.getPrinterId(BixolonPrinter.PRINTER_ID_CODE_PAGE);
587
+ return;
588
+ case STATUS_STEP_COMPLETE:
589
+ statusStep = STATUS_STEP_GET_STATUS;
590
+ break;
591
+ default:
592
+ break;
593
+ }
594
+
595
+ boolean printStatus = currentCall != null && Boolean.TRUE.equals(currentCall.getBoolean("printStatus", false));
596
+
597
+ if (printStatus) {
598
+ String text =
599
+ "Cover: " +
600
+ mConnectedDeviceStatus.optString("cover") +
601
+ "\n" +
602
+ "Paper: " +
603
+ mConnectedDeviceStatus.optString("paper") +
604
+ "\n" +
605
+ "Battery: " +
606
+ mConnectedDeviceStatus.optString("battery") +
607
+ "\n" +
608
+ "Firmware Version: " +
609
+ mConnectedDeviceStatus.optString("firmwareVersion") +
610
+ "\n" +
611
+ "Manufacturer: " +
612
+ mConnectedDeviceStatus.optString("manufacturer") +
613
+ "\n" +
614
+ "Printer Model: " +
615
+ mConnectedDeviceStatus.optString("printerModel") +
616
+ "\n" +
617
+ "Printer Name: " +
618
+ mConnectedDeviceStatus.optString("printerName") +
619
+ "\n" +
620
+ "Printer Address: " +
621
+ mConnectedDeviceStatus.optString("printerAddress") +
622
+ "\n" +
623
+ "Code Page: " +
624
+ mConnectedDeviceStatus.optString("codePage");
625
+
626
+ mBixolonPrinter.printText(text + "\r\n", 0, 0, 0, false);
627
+ mBixolonPrinter.lineFeed(3, false);
628
+ mBixolonPrinter.cutPaper(true);
629
+ } else {
630
+ disconnect();
631
+ }
632
+ }
633
+
634
+ private void failAction(String error) {
635
+ isValidAction = false;
636
+ actionError = error;
637
+ disconnect();
638
+ }
639
+
640
+ private void resolveCurrentMessage(String message) {
641
+ if (currentCall != null) {
642
+ JSObject ret = new JSObject();
643
+ ret.put("message", message);
644
+ currentCall.resolve(ret);
645
+ currentCall = null;
646
+ }
647
+ }
648
+
649
+ // endregion
650
+
651
+ // region Status / MSR message handling
652
+
653
+ private void onMessageRead(Message msg) {
654
+ boolean isMsrTrackDatas = false;
655
+
656
+ switch (msg.arg1) {
657
+ case BixolonPrinter.PROCESS_GET_STATUS:
658
+ if (msg.arg2 == BixolonPrinter.STATUS_NORMAL) {
659
+ mConnectedDeviceStatus.put("cover", "OPENED");
660
+ mConnectedDeviceStatus.put("paper", "FILL");
661
+ } else {
662
+ if ((msg.arg2 & BixolonPrinter.STATUS_COVER_OPEN) == BixolonPrinter.STATUS_COVER_OPEN) {
663
+ mConnectedDeviceStatus.put("cover", "OPENED");
664
+ } else {
665
+ mConnectedDeviceStatus.put("cover", "CLOSED");
666
+ }
667
+
668
+ if ((msg.arg2 & BixolonPrinter.STATUS_PAPER_NOT_PRESENT) == BixolonPrinter.STATUS_PAPER_NOT_PRESENT) {
669
+ mConnectedDeviceStatus.put("paper", "EMPTY");
670
+ } else {
671
+ mConnectedDeviceStatus.put("paper", "FILL");
672
+ }
673
+ }
674
+ statusStep = STATUS_STEP_GET_BATTERY_STATUS;
675
+ break;
676
+ case BixolonPrinter.PROCESS_GET_BATTERY_STATUS:
677
+ switch (msg.arg2) {
678
+ case BixolonPrinter.STATUS_BATTERY_FULL:
679
+ mConnectedDeviceStatus.put("battery", "FULL");
680
+ break;
681
+ case BixolonPrinter.STATUS_BATTERY_HIGH:
682
+ mConnectedDeviceStatus.put("battery", "HIGH");
683
+ break;
684
+ case BixolonPrinter.STATUS_BATTERY_MIDDLE:
685
+ mConnectedDeviceStatus.put("battery", "MIDDLE");
686
+ break;
687
+ case BixolonPrinter.STATUS_BATTERY_LOW:
688
+ mConnectedDeviceStatus.put("battery", "LOW");
689
+ break;
690
+ default:
691
+ break;
692
+ }
693
+ statusStep = STATUS_STEP_GET_PRINTER_ID_FIRMWARE_VERSION;
694
+ break;
695
+ case BixolonPrinter.PROCESS_GET_PRINTER_ID:
696
+ Bundle data = msg.getData();
697
+ String id = data.getString(BixolonPrinter.KEY_STRING_PRINTER_ID);
698
+ switch (statusStep) {
699
+ case STATUS_STEP_GET_PRINTER_ID_FIRMWARE_VERSION:
700
+ mConnectedDeviceStatus.put("firmwareVersion", id);
701
+ statusStep = STATUS_STEP_GET_PRINTER_ID_MANUFACTURER;
702
+ break;
703
+ case STATUS_STEP_GET_PRINTER_ID_MANUFACTURER:
704
+ mConnectedDeviceStatus.put("manufacturer", id);
705
+ statusStep = STATUS_STEP_GET_PRINTER_ID_PRINTER_MODEL;
706
+ break;
707
+ case STATUS_STEP_GET_PRINTER_ID_PRINTER_MODEL:
708
+ mConnectedDeviceStatus.put("printerModel", id);
709
+ statusStep = STATUS_STEP_GET_PRINTER_ID_CODE_PAGE;
710
+ break;
711
+ case STATUS_STEP_GET_PRINTER_ID_CODE_PAGE:
712
+ mConnectedDeviceStatus.put("codePage", id);
713
+ statusStep = STATUS_STEP_COMPLETE;
714
+ break;
715
+ default:
716
+ break;
717
+ }
718
+ break;
719
+ case BixolonPrinter.PROCESS_MSR_TRACK:
720
+ Bundle bundle = msg.getData();
721
+ isMsrTrackDatas = true;
722
+ if (msrListening) {
723
+ notifyListeners("msrData", getMsrTrackData(bundle));
724
+ }
725
+ break;
726
+ default:
727
+ break;
728
+ }
729
+
730
+ if (!isMsrTrackDatas) {
731
+ doGetStatus();
732
+ }
733
+ }
734
+
735
+ private JSObject createConnectionData(String message) {
736
+ JSObject obj = new JSObject();
737
+ obj.put("isConnected", mIsConnected);
738
+ obj.put("message", message);
739
+ return obj;
740
+ }
741
+
742
+ private JSObject getMsrTrackData(Bundle bundle) {
743
+ JSObject obj = new JSObject();
744
+ byte[] track1 = bundle.getByteArray(BixolonPrinter.KEY_STRING_MSR_TRACK1);
745
+ byte[] track2 = bundle.getByteArray(BixolonPrinter.KEY_STRING_MSR_TRACK2);
746
+ byte[] track3 = bundle.getByteArray(BixolonPrinter.KEY_STRING_MSR_TRACK3);
747
+ obj.put("msrTrack1", track1 == null ? "" : new String(track1));
748
+ obj.put("msrTrack2", track2 == null ? "" : new String(track2));
749
+ obj.put("msrTrack3", track3 == null ? "" : new String(track3));
750
+ return obj;
751
+ }
752
+
753
+ // endregion
754
+
755
+ // region Helpers
756
+
757
+ private JSObject configOf(PluginCall call) {
758
+ JSObject config = call.getObject("config");
759
+ return config != null ? config : new JSObject();
760
+ }
761
+
762
+ private boolean configAutoConnect(PluginCall call) {
763
+ return configOf(call).optBoolean("autoConnect", true);
764
+ }
765
+
766
+ private boolean configToastMessage(PluginCall call) {
767
+ return configOf(call).optBoolean("toastMessage", true);
768
+ }
769
+
770
+ /**
771
+ * Decode a Base64 image string into a Bitmap.
772
+ * Based on https://github.com/itsKaynine/cordova-plugin-bixolon-printing
773
+ */
774
+ public static Bitmap decodeBase64Image(String input) {
775
+ String trimmed = input
776
+ .replace("data:image/png;base64,", "")
777
+ .replace("data:image/jpg;base64,", "")
778
+ .replace("data:image/jpeg;base64,", "");
779
+
780
+ byte[] arr = Base64.decode(trimmed, 0);
781
+ return BitmapFactory.decodeByteArray(arr, 0, arr.length);
782
+ }
783
+
784
+ @SuppressLint("DefaultLocale")
785
+ private int getAttribute(String fontType, String fontStyle) {
786
+ int attribute = 0;
787
+
788
+ if (fontType != null) {
789
+ if (fontType.toUpperCase().equals("A")) {
790
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_FONT_A;
791
+ }
792
+ if (fontType.toUpperCase().equals("B")) {
793
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_FONT_B;
794
+ }
795
+ if (fontType.toUpperCase().equals("C")) {
796
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_FONT_C;
797
+ }
798
+ }
799
+
800
+ if (fontStyle != null) {
801
+ if (fontStyle.toUpperCase().equals("UNDERLINE")) {
802
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_UNDERLINE1;
803
+ }
804
+ if (fontStyle.toUpperCase().equals("UNDERLINE2")) {
805
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_UNDERLINE2;
806
+ }
807
+ if (fontStyle.toUpperCase().equals("BOLD")) {
808
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_EMPHASIZED;
809
+ }
810
+ if (fontStyle.toUpperCase().equals("REVERSE") || fontStyle.toUpperCase().equals("REVERSED")) {
811
+ attribute |= BixolonPrinter.TEXT_ATTRIBUTE_REVERSE;
812
+ }
813
+ }
814
+
815
+ return attribute;
816
+ }
817
+
818
+ @SuppressLint("DefaultLocale")
819
+ private int getAlignment(String align) {
820
+ int alignment = BixolonPrinter.ALIGNMENT_LEFT;
821
+ if (align != null) {
822
+ if (ALIGNMENT_LEFT.equals(align.toUpperCase())) {
823
+ alignment = BixolonPrinter.ALIGNMENT_LEFT;
824
+ } else if (ALIGNMENT_CENTER.equals(align.toUpperCase())) {
825
+ alignment = BixolonPrinter.ALIGNMENT_CENTER;
826
+ } else if (ALIGNMENT_RIGHT.equals(align.toUpperCase())) {
827
+ alignment = BixolonPrinter.ALIGNMENT_RIGHT;
828
+ }
829
+ }
830
+ return alignment;
831
+ }
832
+
833
+ private int getTextSize(int width, int height) {
834
+ int size = 0;
835
+
836
+ switch (width) {
837
+ case 0:
838
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL1;
839
+ break;
840
+ case 1:
841
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL2;
842
+ break;
843
+ case 2:
844
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL3;
845
+ break;
846
+ case 3:
847
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL4;
848
+ break;
849
+ case 4:
850
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL5;
851
+ break;
852
+ case 5:
853
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL6;
854
+ break;
855
+ case 6:
856
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL7;
857
+ break;
858
+ case 7:
859
+ size = BixolonPrinter.TEXT_SIZE_HORIZONTAL8;
860
+ break;
861
+ default:
862
+ break;
863
+ }
864
+
865
+ switch (height) {
866
+ case 0:
867
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL1;
868
+ break;
869
+ case 1:
870
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL2;
871
+ break;
872
+ case 2:
873
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL3;
874
+ break;
875
+ case 3:
876
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL4;
877
+ break;
878
+ case 4:
879
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL5;
880
+ break;
881
+ case 5:
882
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL6;
883
+ break;
884
+ case 6:
885
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL7;
886
+ break;
887
+ case 7:
888
+ size |= BixolonPrinter.TEXT_SIZE_VERTICAL8;
889
+ break;
890
+ default:
891
+ break;
892
+ }
893
+
894
+ return size;
895
+ }
896
+
897
+ // endregion
898
+
899
+ private final Handler mHandler = new Handler(
900
+ new Handler.Callback() {
901
+ @Override
902
+ public boolean handleMessage(Message msg) {
903
+ switch (msg.what) {
904
+ case BixolonPrinter.MESSAGE_BLUETOOTH_DEVICE_SET:
905
+ if (msg.obj == null) {
906
+ if (currentCall != null) {
907
+ currentCall.reject("Printer not found");
908
+ currentCall = null;
909
+ }
910
+ } else {
911
+ @SuppressWarnings("unchecked")
912
+ final Set<BluetoothDevice> pairedDevices = (Set<BluetoothDevice>) msg.obj;
913
+ final String[] itemsAddr = new String[pairedDevices.size()];
914
+
915
+ int index = 0;
916
+ for (BluetoothDevice device : pairedDevices) {
917
+ itemsAddr[index] = device.getAddress();
918
+ index++;
919
+ }
920
+
921
+ if (optAutoConnect) {
922
+ if (itemsAddr.length > 0) {
923
+ mConnectedDeviceAddress = itemsAddr[0];
924
+ mBixolonPrinter.connect(itemsAddr[0]);
925
+ } else if (currentCall != null) {
926
+ currentCall.reject("Printer not found");
927
+ currentCall = null;
928
+ }
929
+ } else {
930
+ new AlertDialog.Builder(getActivity())
931
+ .setTitle("Available printers")
932
+ .setItems(
933
+ itemsAddr,
934
+ new DialogInterface.OnClickListener() {
935
+ public void onClick(DialogInterface dialog, int which) {
936
+ mConnectedDeviceAddress = itemsAddr[which];
937
+ mBixolonPrinter.connect(itemsAddr[which]);
938
+ }
939
+ }
940
+ )
941
+ .show();
942
+ }
943
+ }
944
+ return true;
945
+ case BixolonPrinter.MESSAGE_STATE_CHANGE:
946
+ switch (msg.arg1) {
947
+ case BixolonPrinter.STATE_CONNECTED:
948
+ onConnect();
949
+ break;
950
+ case BixolonPrinter.STATE_CONNECTING:
951
+ break;
952
+ case BixolonPrinter.STATE_NONE:
953
+ onDisconnect();
954
+ break;
955
+ default:
956
+ break;
957
+ }
958
+ return true;
959
+ case BixolonPrinter.MESSAGE_DEVICE_NAME:
960
+ mConnectedDeviceName = msg.getData().getString(BixolonPrinter.KEY_STRING_DEVICE_NAME);
961
+ return true;
962
+ case BixolonPrinter.MESSAGE_TOAST:
963
+ if (optToastMessage) {
964
+ Toast.makeText(
965
+ getActivity(),
966
+ msg.getData().getString(BixolonPrinter.KEY_STRING_TOAST),
967
+ Toast.LENGTH_SHORT
968
+ ).show();
969
+ }
970
+ return true;
971
+ case BixolonPrinter.MESSAGE_PRINT_COMPLETE:
972
+ onPrintComplete();
973
+ return true;
974
+ case BixolonPrinter.MESSAGE_READ:
975
+ onMessageRead(msg);
976
+ return true;
977
+ default:
978
+ return false;
979
+ }
980
+ }
981
+ }
982
+ );
983
+ }