ns-bxl-label 1.0.5 → 1.0.7
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/index.js +109 -4
- package/package.json +6 -29
- package/platforms/android/include.gradle +5 -4
- package/platforms/android/{src/main/java → java}/com/rivercon/appprintbixolon/PrinterControl/BixolonPrinterConnector.java +18 -2
- package/platforms/android/{src/main/java → java}/com/rivercon/appprintbixolon/PrinterControl/BixolonPrinterManager.java +21 -39
- package/platforms/android/{src/main/java → java}/com/rivercon/appprintbixolon/PrinterControl/LabelPrinterHelper.java +20 -2
- package/platforms/android/{src/main/java/com/rivercon/appprintbixolon/PrinterControl/PrinterDialog.java → java/com/rivercon/appprintbixolon/PrinterControl/PrinterDialogManager.java} +13 -0
- package/platforms/android/{src/main/java → java}/com/rivercon/appprintbixolon/PrinterControl/PrinterStateListener.java +5 -19
- package/index.d.ts +0 -16
- package/platforms/android/libs/libcommon_V1.4.0.jar +0 -0
- package/plugin.xml +0 -34
- package/src/index.js +0 -116
- package/src/index.ts +0 -147
package/index.js
CHANGED
|
@@ -1,5 +1,110 @@
|
|
|
1
|
-
|
|
1
|
+
// index.js
|
|
2
|
+
// Plugin NativeScript 7+ — JS + JAR Bixolon + tus clases Java
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const app = require("@nativescript/core/application");
|
|
5
|
+
|
|
6
|
+
// -----------------------------------------------------------------------------
|
|
7
|
+
// 1. Listener JS equivalente a PrinterStateListener
|
|
8
|
+
// -----------------------------------------------------------------------------
|
|
9
|
+
function createPrinterStateListener(callbacks) {
|
|
10
|
+
return com.rivercon.appprintbixolon.PrinterControl.PrinterStateListener.extend({
|
|
11
|
+
|
|
12
|
+
onConnectionStateChanged(status) {
|
|
13
|
+
if (callbacks && callbacks.onConnectionStateChanged) {
|
|
14
|
+
callbacks.onConnectionStateChanged(status);
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
showPrintingDialog() {
|
|
19
|
+
if (callbacks && callbacks.showPrintingDialog) {
|
|
20
|
+
callbacks.showPrintingDialog();
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
hidePrintingDialog() {
|
|
25
|
+
if (callbacks && callbacks.hidePrintingDialog) {
|
|
26
|
+
callbacks.hidePrintingDialog();
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
onPrintError(errorMessage) {
|
|
31
|
+
if (callbacks && callbacks.onPrintError) {
|
|
32
|
+
callbacks.onPrintError(errorMessage);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// -----------------------------------------------------------------------------
|
|
39
|
+
// 2. Instancias internas (solo Android)
|
|
40
|
+
// -----------------------------------------------------------------------------
|
|
41
|
+
let _manager = null;
|
|
42
|
+
let _dialogManager = null;
|
|
43
|
+
|
|
44
|
+
// -----------------------------------------------------------------------------
|
|
45
|
+
// 3. Inicialización del plugin
|
|
46
|
+
// -----------------------------------------------------------------------------
|
|
47
|
+
function init(callbacks) {
|
|
48
|
+
if (!app.android || !app.android.context) {
|
|
49
|
+
throw new Error("Este plugin solo funciona en Android.");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const context = app.android.context;
|
|
53
|
+
|
|
54
|
+
const ListenerImpl = createPrinterStateListener(callbacks);
|
|
55
|
+
|
|
56
|
+
_manager = new com.rivercon.appprintbixolon.PrinterControl.BixolonPrinterManager(
|
|
57
|
+
context,
|
|
58
|
+
new ListenerImpl()
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// PrinterDialogManager no lo enviaste, pero lo integro porque tu MainActivity lo usa
|
|
62
|
+
_dialogManager = new com.rivercon.appprintbixolon.PrinterControl.PrinterDialogManager(
|
|
63
|
+
context
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// -----------------------------------------------------------------------------
|
|
68
|
+
// 4. Mostrar diálogo Bluetooth (equivalente a showBluetoothDeviceDialog)
|
|
69
|
+
// -----------------------------------------------------------------------------
|
|
70
|
+
function selectPrinter(callback) {
|
|
71
|
+
if (!_dialogManager) {
|
|
72
|
+
throw new Error("Debes llamar init() primero.");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_dialogManager.showBluetoothDeviceDialog(
|
|
76
|
+
new com.rivercon.appprintbixolon.PrinterControl.PrinterDialogManager
|
|
77
|
+
.OnDeviceSelectedListener({
|
|
78
|
+
onDeviceSelected(address) {
|
|
79
|
+
if (callback) callback(address);
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// -----------------------------------------------------------------------------
|
|
86
|
+
// 5. Iniciar secuencia completa de impresión
|
|
87
|
+
// -----------------------------------------------------------------------------
|
|
88
|
+
function startFullSequence(address, zplString) {
|
|
89
|
+
if (!_manager) {
|
|
90
|
+
throw new Error("Debes llamar init() primero.");
|
|
91
|
+
}
|
|
92
|
+
_manager.startFullSequence(address, zplString);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// -----------------------------------------------------------------------------
|
|
96
|
+
// 6. Desconectar impresora
|
|
97
|
+
// -----------------------------------------------------------------------------
|
|
98
|
+
function disconnect() {
|
|
99
|
+
if (_manager) {
|
|
100
|
+
_manager.disconnect();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// -----------------------------------------------------------------------------
|
|
105
|
+
// 7. API pública del plugin
|
|
106
|
+
// -----------------------------------------------------------------------------
|
|
107
|
+
exports.init = init;
|
|
108
|
+
exports.selectPrinter = selectPrinter;
|
|
109
|
+
exports.startFullSequence = startFullSequence;
|
|
110
|
+
exports.disconnect = disconnect;
|
package/package.json
CHANGED
|
@@ -1,37 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ns-bxl-label",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"main": "index.js",
|
|
5
|
-
"types": "index.d.ts",
|
|
6
|
-
"nativescript": {
|
|
7
|
-
"platforms": {
|
|
8
|
-
"android": "8.0.0"
|
|
9
|
-
},
|
|
10
|
-
"plugin": {
|
|
11
|
-
"name": "ns-bxl-label"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"peerDependencies": {
|
|
15
|
-
"@nativescript/core": "^8.0.0"
|
|
16
|
-
},
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"nativescript-permissions": "^1.3.0"
|
|
19
|
-
},
|
|
20
5
|
"files": [
|
|
21
6
|
"index.js",
|
|
22
|
-
"
|
|
23
|
-
"src/",
|
|
24
|
-
"platforms/",
|
|
25
|
-
"plugin.xml",
|
|
26
|
-
"package.json",
|
|
27
|
-
"README.md"
|
|
28
|
-
],
|
|
29
|
-
"keywords": [
|
|
30
|
-
"bxl"
|
|
7
|
+
"platforms/"
|
|
31
8
|
],
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
"nativescript": {
|
|
10
|
+
"platforms": {
|
|
11
|
+
"android": "7.0.0"
|
|
12
|
+
}
|
|
36
13
|
}
|
|
37
14
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
android {
|
|
2
2
|
defaultConfig {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
generatedDensities = []
|
|
4
|
+
}
|
|
5
|
+
aaptOptions {
|
|
6
|
+
additionalParameters "--no-version-vectors"
|
|
5
7
|
}
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
dependencies {
|
|
9
11
|
implementation files('libs/BixolonLabelPrinterLibrary_V2.0.9.jar')
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package com.rivercon.appprintbixolon.PrinterControl;
|
|
2
2
|
|
|
3
|
+
// Fichero: app/src/main/java/com/bixolon/labelprintersample/BixolonPrinterConnector.java
|
|
3
4
|
import android.content.Context;
|
|
4
5
|
import android.os.Handler;
|
|
5
6
|
import android.os.Message;
|
|
@@ -8,17 +9,25 @@ import com.bixolon.labelprinter.BixolonLabelPrinter;
|
|
|
8
9
|
|
|
9
10
|
public class BixolonPrinterConnector {
|
|
10
11
|
|
|
11
|
-
private static final String TAG = "BixolonPrinterConnector";
|
|
12
|
+
private static final String TAG = "BixolonPrinterConnector"; // Tag para los logs
|
|
12
13
|
private final BixolonLabelPrinter mBixolonLabelPrinter;
|
|
13
14
|
|
|
14
15
|
public BixolonPrinterConnector(Context context, Handler handler) {
|
|
16
|
+
// La instancia de la impresora se crea aquí y se gestiona dentro de esta clase.
|
|
15
17
|
this.mBixolonLabelPrinter = new BixolonLabelPrinter(context, handler, null);
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Devuelve la instancia de la impresora para que otras clases puedan usarla.
|
|
22
|
+
*/
|
|
18
23
|
public BixolonLabelPrinter getPrinter() {
|
|
19
24
|
return mBixolonLabelPrinter;
|
|
20
25
|
}
|
|
21
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Inicia la conexión con una dirección de dispositivo Bluetooth.
|
|
29
|
+
* @param address La dirección MAC del dispositivo Bluetooth.
|
|
30
|
+
*/
|
|
22
31
|
public void connect(String address) {
|
|
23
32
|
if (mBixolonLabelPrinter.isConnected()) {
|
|
24
33
|
Log.d(TAG, "La impresora ya está conectada.");
|
|
@@ -32,6 +41,9 @@ public class BixolonPrinterConnector {
|
|
|
32
41
|
mBixolonLabelPrinter.connect(address);
|
|
33
42
|
}
|
|
34
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Desconecta la impresora si está conectada.
|
|
46
|
+
*/
|
|
35
47
|
public void disconnect() {
|
|
36
48
|
if (mBixolonLabelPrinter.isConnected()) {
|
|
37
49
|
Log.d(TAG, "Desconectando la impresora.");
|
|
@@ -39,6 +51,10 @@ public class BixolonPrinterConnector {
|
|
|
39
51
|
}
|
|
40
52
|
}
|
|
41
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Procesa los mensajes del Handler relacionados con el estado de la conexión
|
|
56
|
+
* y los muestra en el Logcat.
|
|
57
|
+
*/
|
|
42
58
|
public void handleConnectionMessages(Message msg) {
|
|
43
59
|
if (msg.what == BixolonLabelPrinter.MESSAGE_STATE_CHANGE) {
|
|
44
60
|
switch (msg.arg1) {
|
|
@@ -55,4 +71,4 @@ public class BixolonPrinterConnector {
|
|
|
55
71
|
}
|
|
56
72
|
}
|
|
57
73
|
}
|
|
58
|
-
}
|
|
74
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
package com.rivercon.appprintbixolon.PrinterControl;
|
|
2
|
+
// Fichero: app/src/main/java/com/bixolon/labelprintersample/BixolonPrinterManager.java
|
|
2
3
|
|
|
3
4
|
import android.annotation.SuppressLint;
|
|
4
5
|
import android.content.Context;
|
|
@@ -20,27 +21,30 @@ public class BixolonPrinterManager {
|
|
|
20
21
|
private final PrinterStateListener mStateListener;
|
|
21
22
|
|
|
22
23
|
private boolean isAutoPrintSequenceActive = false;
|
|
23
|
-
private String mBase64PdfToPrint; //
|
|
24
|
+
private String mBase64PdfToPrint; // Variable para almacenar el PDF a imprimir
|
|
24
25
|
|
|
25
26
|
@SuppressLint("HandlerLeak")
|
|
26
27
|
public BixolonPrinterManager(Context context, PrinterStateListener stateListener) {
|
|
27
28
|
this.mStateListener = stateListener;
|
|
28
|
-
|
|
29
29
|
this.mUiHandler = new Handler(Looper.getMainLooper());
|
|
30
|
-
|
|
31
|
-
// Handler asociado explícitamente al Looper principal para recibir los mensajes del SDK
|
|
32
|
-
this.mHandler = new Handler(Looper.getMainLooper(), this::dispatchMessage);
|
|
33
|
-
|
|
30
|
+
this.mHandler = new Handler(this::dispatchMessage);
|
|
34
31
|
this.mPrinterConnector = new BixolonPrinterConnector(context, mHandler);
|
|
35
32
|
this.mLabelPrinterHelper = new LabelPrinterHelper(context, mPrinterConnector.getPrinter());
|
|
36
33
|
}
|
|
37
34
|
|
|
38
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Nuevo método público que inicia la secuencia completa.
|
|
37
|
+
* @param address La dirección MAC a la que conectar.
|
|
38
|
+
* @param base64Pdf El string Base64 del PDF que se va a imprimir.
|
|
39
|
+
*/
|
|
40
|
+
public void startFullSequence(String address, String base64Pdf) {
|
|
39
41
|
if (isAutoPrintSequenceActive) {
|
|
40
42
|
Log.w(TAG, "Ya hay una secuencia de impresión en curso.");
|
|
41
43
|
return;
|
|
42
44
|
}
|
|
43
|
-
|
|
45
|
+
|
|
46
|
+
// Guarda el PDF que se necesita imprimir cuando la conexión esté lista
|
|
47
|
+
this.mBase64PdfToPrint = base64Pdf;
|
|
44
48
|
isAutoPrintSequenceActive = true;
|
|
45
49
|
mPrinterConnector.connect(address);
|
|
46
50
|
}
|
|
@@ -51,64 +55,42 @@ public class BixolonPrinterManager {
|
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
private boolean dispatchMessage(Message msg) {
|
|
54
|
-
// Mantienes tu logging de estados
|
|
55
58
|
mPrinterConnector.handleConnectionMessages(msg);
|
|
56
59
|
|
|
57
60
|
if (msg.what == BixolonLabelPrinter.MESSAGE_STATE_CHANGE) {
|
|
58
61
|
switch (msg.arg1) {
|
|
59
62
|
case BixolonLabelPrinter.STATE_CONNECTING:
|
|
60
|
-
|
|
63
|
+
mStateListener.onConnectionStateChanged("Estado: Conectando...");
|
|
61
64
|
break;
|
|
62
|
-
|
|
63
65
|
case BixolonLabelPrinter.STATE_CONNECTED:
|
|
64
66
|
String deviceName = msg.getData().getString(BixolonLabelPrinter.DEVICE_NAME, "desconocido");
|
|
65
|
-
|
|
67
|
+
mStateListener.onConnectionStateChanged("Estado: Conectado a " + deviceName);
|
|
66
68
|
if (isAutoPrintSequenceActive) {
|
|
67
69
|
executePrintSequence();
|
|
68
70
|
}
|
|
69
71
|
break;
|
|
70
|
-
|
|
71
72
|
case BixolonLabelPrinter.STATE_NONE:
|
|
72
|
-
|
|
73
|
-
postToUi(() -> mStateListener.onConnectionStateChanged("Desconectado"));
|
|
74
|
-
if (isAutoPrintSequenceActive) {
|
|
75
|
-
// Interpretamos como fallo de conexión si nunca llegó a CONNECTED
|
|
76
|
-
postToUi(() -> mStateListener.onConnectionFailed("No se pudo mantener la conexión con la impresora."));
|
|
77
|
-
}
|
|
73
|
+
mStateListener.onConnectionStateChanged("Estado: Desconectado");
|
|
78
74
|
isAutoPrintSequenceActive = false;
|
|
79
75
|
break;
|
|
80
76
|
}
|
|
81
77
|
}
|
|
82
|
-
|
|
83
|
-
// Manejo básico de pérdida de conexión explícita, si el SDK envía este mensaje
|
|
84
|
-
if (msg.what == BixolonLabelPrinter.MESSAGE_CONNECTION_LOST) {
|
|
85
|
-
Log.w(TAG, "Conexión con la impresora perdida.");
|
|
86
|
-
postToUi(mStateListener::onConnectionLost);
|
|
87
|
-
isAutoPrintSequenceActive = false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Aquí podrías manejar otros mensajes (TOAST, ERROR, STATUS) y mapearlos a onPrintError/onConnectionFailed según necesites
|
|
91
|
-
|
|
92
78
|
return true;
|
|
93
79
|
}
|
|
94
80
|
|
|
95
81
|
private void executePrintSequence() {
|
|
96
82
|
new Thread(() -> {
|
|
97
|
-
|
|
83
|
+
mUiHandler.post(mStateListener::showPrintingDialog);
|
|
98
84
|
try {
|
|
85
|
+
// Pasa el PDF guardado al helper de impresión
|
|
99
86
|
mLabelPrinterHelper.printLabelSample2(mBase64PdfToPrint);
|
|
100
|
-
postToUi(mStateListener::onPrintSuccess);
|
|
101
87
|
} catch (Exception e) {
|
|
102
|
-
Log.e(TAG, "
|
|
103
|
-
|
|
88
|
+
//Log.e(TAG, "Ocurrió un error durante la impresión", e);
|
|
89
|
+
//mUiHandler.post(() -> mStateListener.onPrintError("Error: " + e.getMessage()));
|
|
104
90
|
} finally {
|
|
105
|
-
|
|
91
|
+
mUiHandler.post(mStateListener::hidePrintingDialog);
|
|
106
92
|
disconnect();
|
|
107
93
|
}
|
|
108
94
|
}).start();
|
|
109
95
|
}
|
|
110
|
-
|
|
111
|
-
private void postToUi(Runnable runnable) {
|
|
112
|
-
mUiHandler.post(runnable);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
96
|
+
}
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
package com.rivercon.appprintbixolon.PrinterControl;
|
|
2
|
+
// Fichero: app/src/main/java/com/bixolon/labelprintersample/LabelPrinterHelper.java
|
|
2
3
|
|
|
3
4
|
import android.content.Context;
|
|
5
|
+
import android.graphics.Bitmap;
|
|
6
|
+
import android.graphics.BitmapFactory;
|
|
7
|
+
import android.graphics.pdf.PdfRenderer;
|
|
8
|
+
import android.os.ParcelFileDescriptor;
|
|
9
|
+
import android.util.Base64;
|
|
4
10
|
import android.util.Log;
|
|
11
|
+
|
|
5
12
|
import com.bixolon.labelprinter.BixolonLabelPrinter;
|
|
13
|
+
import com.bixolon.labelprinter.ResponseType;
|
|
14
|
+
|
|
15
|
+
import java.io.*;
|
|
6
16
|
import java.nio.charset.StandardCharsets;
|
|
7
17
|
|
|
8
18
|
public class LabelPrinterHelper {
|
|
@@ -16,6 +26,11 @@ public class LabelPrinterHelper {
|
|
|
16
26
|
this.mBixolonLabelPrinter = printer;
|
|
17
27
|
}
|
|
18
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Imprime una etiqueta a partir de un PDF codificado en Base64.
|
|
31
|
+
* Ahora acepta el Base64 del PDF como parámetro.
|
|
32
|
+
* @param command El string Base64 que representa el PDF a imprimir.
|
|
33
|
+
*/
|
|
19
34
|
public void printLabelSample2(String command) {
|
|
20
35
|
if (!mBixolonLabelPrinter.isConnected()) {
|
|
21
36
|
Log.e(TAG, "Impresora no conectada. Abortando impresión.");
|
|
@@ -29,11 +44,14 @@ public class LabelPrinterHelper {
|
|
|
29
44
|
|
|
30
45
|
Log.d(TAG, "Iniciando transacción de impresión en la impresora.");
|
|
31
46
|
|
|
47
|
+
|
|
32
48
|
mBixolonLabelPrinter.beginTransactionPrint();
|
|
33
|
-
|
|
49
|
+
|
|
50
|
+
mBixolonLabelPrinter.executeDirectIo(command.getBytes(StandardCharsets.UTF_8), false,0);
|
|
51
|
+
|
|
34
52
|
mBixolonLabelPrinter.print(1, 0);
|
|
35
53
|
mBixolonLabelPrinter.endTransactionPrint();
|
|
36
|
-
|
|
37
54
|
Log.d(TAG, "Transacción de impresión finalizada.");
|
|
38
55
|
}
|
|
56
|
+
|
|
39
57
|
}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
package com.rivercon.appprintbixolon.PrinterControl;
|
|
2
|
+
// Fichero: app/src/main/java/com/bixolon/labelprintersample/PrinterDialogManager.java
|
|
2
3
|
|
|
3
4
|
import android.app.AlertDialog;
|
|
4
5
|
import android.app.ProgressDialog;
|
|
5
6
|
import android.bluetooth.BluetoothAdapter;
|
|
6
7
|
import android.bluetooth.BluetoothDevice;
|
|
7
8
|
import android.content.Context;
|
|
9
|
+
import android.content.DialogInterface;
|
|
8
10
|
import android.widget.Toast;
|
|
11
|
+
|
|
9
12
|
import java.util.ArrayList;
|
|
10
13
|
import java.util.Set;
|
|
11
14
|
import java.util.function.Consumer;
|
|
12
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Gestiona la creación y visualización de todos los diálogos de la impresora.
|
|
18
|
+
*/
|
|
13
19
|
public class PrinterDialogManager {
|
|
14
20
|
|
|
15
21
|
private ProgressDialog mPrintingDialog;
|
|
@@ -21,6 +27,11 @@ public class PrinterDialogManager {
|
|
|
21
27
|
this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
22
28
|
}
|
|
23
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Muestra un diálogo con la lista de dispositivos Bluetooth pareados.
|
|
32
|
+
* @param onDeviceSelected Callback que se ejecuta cuando el usuario selecciona un dispositivo.
|
|
33
|
+
* Devuelve la dirección MAC del dispositivo.
|
|
34
|
+
*/
|
|
24
35
|
public void showBluetoothDeviceDialog(Consumer<String> onDeviceSelected) {
|
|
25
36
|
if (mBluetoothAdapter == null) {
|
|
26
37
|
Toast.makeText(mContext, "Este dispositivo no soporta Bluetooth", Toast.LENGTH_SHORT).show();
|
|
@@ -42,6 +53,7 @@ public class PrinterDialogManager {
|
|
|
42
53
|
new AlertDialog.Builder(mContext)
|
|
43
54
|
.setTitle("Seleccione una Impresora")
|
|
44
55
|
.setItems(deviceNames, (dialog, which) -> {
|
|
56
|
+
// El usuario seleccionó un dispositivo, devolvemos su dirección MAC.
|
|
45
57
|
String selectedAddress = deviceList.get(which).getAddress();
|
|
46
58
|
onDeviceSelected.accept(selectedAddress);
|
|
47
59
|
})
|
|
@@ -49,6 +61,7 @@ public class PrinterDialogManager {
|
|
|
49
61
|
.show();
|
|
50
62
|
}
|
|
51
63
|
|
|
64
|
+
|
|
52
65
|
public void showPrintingDialog() {
|
|
53
66
|
mPrintingDialog = new ProgressDialog(mContext);
|
|
54
67
|
mPrintingDialog.setMessage("Imprimiendo...");
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
package com.rivercon.appprintbixolon.PrinterControl;
|
|
2
2
|
|
|
3
|
+
// Fichero: app/src/main/java/com/bixolon/labelprintersample/PrinterStateListener.java
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Interfaz para comunicar eventos desde BixolonPrinterManager
|
|
5
|
-
* hacia la capa de la UI (
|
|
7
|
+
* hacia la capa de la UI (MainActivity).
|
|
6
8
|
*/
|
|
7
9
|
public interface PrinterStateListener {
|
|
8
10
|
/**
|
|
@@ -12,7 +14,7 @@ public interface PrinterStateListener {
|
|
|
12
14
|
void onConnectionStateChanged(String status);
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
|
-
* Pide a la UI que muestre un diálogo de impresión
|
|
17
|
+
* Pide a la UI que muestre un diálogo de impresión.
|
|
16
18
|
*/
|
|
17
19
|
void showPrintingDialog();
|
|
18
20
|
|
|
@@ -26,20 +28,4 @@ public interface PrinterStateListener {
|
|
|
26
28
|
* @param errorMessage Mensaje del error.
|
|
27
29
|
*/
|
|
28
30
|
void onPrintError(String errorMessage);
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Se llama cuando la conexión falla antes de establecerse.
|
|
32
|
-
* @param errorMessage Mensaje descriptivo del error.
|
|
33
|
-
*/
|
|
34
|
-
void onConnectionFailed(String errorMessage);
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Se llama cuando la conexión se pierde después de haber estado conectada.
|
|
38
|
-
*/
|
|
39
|
-
void onConnectionLost();
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Se llama cuando la impresión ha terminado exitosamente.
|
|
43
|
-
*/
|
|
44
|
-
void onPrintSuccess();
|
|
45
|
-
}
|
|
31
|
+
}
|
package/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export interface PrinterCallbacks {
|
|
2
|
-
onConnectionStateChanged?: (status: string) => void;
|
|
3
|
-
onPrintStarted?: () => void;
|
|
4
|
-
onPrintSuccess?: () => void;
|
|
5
|
-
onPrintError?: (error: string) => void;
|
|
6
|
-
onConnectionFailed?: (error: string) => void;
|
|
7
|
-
onConnectionLost?: () => void;
|
|
8
|
-
showPrintingDialog?: () => void;
|
|
9
|
-
hidePrintingDialog?: () => void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function startBluetoothPrintFlow(
|
|
13
|
-
printerAddress: string,
|
|
14
|
-
zplCommand: string,
|
|
15
|
-
callbacks?: PrinterCallbacks
|
|
16
|
-
): void;
|
|
Binary file
|
package/plugin.xml
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<plugin id="ns-bxl-label" version="1.0.5" xmlns="http://www.nativescript.org/ns/plugins">
|
|
3
|
-
|
|
4
|
-
<platform name="android">
|
|
5
|
-
|
|
6
|
-
<!-- Gradle -->
|
|
7
|
-
<framework src="platforms/android/include.gradle" />
|
|
8
|
-
|
|
9
|
-
<!-- Archivos Java -->
|
|
10
|
-
<source-file src="platforms/android/src/main/java/com/rivercon/appprintbixolon/PrinterControl/BixolonPrinterConnector.java" />
|
|
11
|
-
<source-file src="platforms/android/src/main/java/com/rivercon/appprintbixolon/PrinterControl/BixolonPrinterManager.java" />
|
|
12
|
-
<source-file src="platforms/android/src/main/java/com/rivercon/appprintbixolon/PrinterControl/LabelPrinterHelper.java" />
|
|
13
|
-
<source-file src="platforms/android/src/main/java/com/rivercon/appprintbixolon/PrinterControl/PrinterDialog.java" />
|
|
14
|
-
<source-file src="platforms/android/src/main/java/com/rivercon/appprintbixolon/PrinterControl/PrinterStateListener.java" />
|
|
15
|
-
|
|
16
|
-
<!-- JARs -->
|
|
17
|
-
<source-file src="platforms/android/libs/BixolonLabelPrinterLibrary_V2.0.9.jar" />
|
|
18
|
-
<source-file src="platforms/android/libs/libcommon_V1.4.0.jar" />
|
|
19
|
-
|
|
20
|
-
<!-- Permisos -->
|
|
21
|
-
<config-file target="AndroidManifest.xml" parent="/manifest">
|
|
22
|
-
<uses-permission android:name="android.permission.BLUETOOTH" />
|
|
23
|
-
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
|
24
|
-
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
|
25
|
-
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
|
|
26
|
-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
|
27
|
-
</config-file>
|
|
28
|
-
|
|
29
|
-
</platform>
|
|
30
|
-
|
|
31
|
-
<!-- API JS -->
|
|
32
|
-
<js-module name="index" path="index.js" />
|
|
33
|
-
|
|
34
|
-
</plugin>
|
package/src/index.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
const application = require("@nativescript/core/application");
|
|
2
|
-
const Dialogs = require("@nativescript/core").Dialogs;
|
|
3
|
-
const permissions = require("nativescript-permissions");
|
|
4
|
-
|
|
5
|
-
function PrinterStateListenerImpl(callbacks) {
|
|
6
|
-
this.callbacks = callbacks || {};
|
|
7
|
-
return global.__native(this);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
PrinterStateListenerImpl.prototype = Object.create(java.lang.Object.prototype);
|
|
11
|
-
PrinterStateListenerImpl.prototype.constructor = PrinterStateListenerImpl;
|
|
12
|
-
|
|
13
|
-
PrinterStateListenerImpl.prototype.onConnectionStateChanged = function (status) {
|
|
14
|
-
if (this.callbacks.onConnectionStateChanged) {
|
|
15
|
-
this.callbacks.onConnectionStateChanged(status);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
PrinterStateListenerImpl.prototype.showPrintingDialog = function () {
|
|
20
|
-
if (this.callbacks.showPrintingDialog) {
|
|
21
|
-
this.callbacks.showPrintingDialog();
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
PrinterStateListenerImpl.prototype.hidePrintingDialog = function () {
|
|
26
|
-
if (this.callbacks.hidePrintingDialog) {
|
|
27
|
-
this.callbacks.hidePrintingDialog();
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
PrinterStateListenerImpl.prototype.onPrintError = function (errorMessage) {
|
|
32
|
-
if (this.callbacks.onPrintError) {
|
|
33
|
-
this.callbacks.onPrintError(errorMessage);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
PrinterStateListenerImpl.prototype.onConnectionFailed = function (errorMessage) {
|
|
38
|
-
if (this.callbacks.onConnectionFailed) {
|
|
39
|
-
this.callbacks.onConnectionFailed(errorMessage);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
PrinterStateListenerImpl.prototype.onConnectionLost = function () {
|
|
44
|
-
if (this.callbacks.onConnectionLost) {
|
|
45
|
-
this.callbacks.onConnectionLost();
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
PrinterStateListenerImpl.prototype.onPrintSuccess = function () {
|
|
50
|
-
if (this.callbacks.onPrintSuccess) {
|
|
51
|
-
this.callbacks.onPrintSuccess();
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
function startBluetoothPrintFlow(printerAddress, zplCommand, callbacks) {
|
|
56
|
-
callbacks = callbacks || {};
|
|
57
|
-
const context = application.android.context;
|
|
58
|
-
|
|
59
|
-
if (!printerAddress || printerAddress.trim() === "") {
|
|
60
|
-
Dialogs.alert("No se ha especificado la dirección de la impresora.");
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!zplCommand || zplCommand.trim() === "") {
|
|
65
|
-
Dialogs.alert("No hay ZPL para imprimir.");
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const requiredPermissions = [
|
|
70
|
-
android.Manifest.permission.BLUETOOTH_CONNECT,
|
|
71
|
-
android.Manifest.permission.BLUETOOTH_SCAN,
|
|
72
|
-
android.Manifest.permission.ACCESS_FINE_LOCATION
|
|
73
|
-
];
|
|
74
|
-
|
|
75
|
-
permissions.requestPermissions(requiredPermissions, "Se requieren permisos para imprimir")
|
|
76
|
-
.then(() => {
|
|
77
|
-
try {
|
|
78
|
-
if (callbacks.onPrintStarted) {
|
|
79
|
-
callbacks.onPrintStarted();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const listener = new PrinterStateListenerImpl({
|
|
83
|
-
onConnectionStateChanged: callbacks.onConnectionStateChanged,
|
|
84
|
-
showPrintingDialog: callbacks.showPrintingDialog,
|
|
85
|
-
hidePrintingDialog: callbacks.hidePrintingDialog,
|
|
86
|
-
onPrintError: callbacks.onPrintError,
|
|
87
|
-
onConnectionFailed: callbacks.onConnectionFailed,
|
|
88
|
-
onConnectionLost: callbacks.onConnectionLost,
|
|
89
|
-
onPrintSuccess: callbacks.onPrintSuccess
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const Manager = com.rivercon.appprintbixolon.PrinterControl.BixolonPrinterManager;
|
|
93
|
-
const manager = new Manager(context, listener);
|
|
94
|
-
|
|
95
|
-
manager.startFullSequence(printerAddress, zplCommand);
|
|
96
|
-
|
|
97
|
-
} catch (e) {
|
|
98
|
-
if (callbacks.onPrintError) {
|
|
99
|
-
callbacks.onPrintError("Error general en flujo de impresión: " + e.message);
|
|
100
|
-
} else {
|
|
101
|
-
Dialogs.alert("Error general en flujo de impresión: " + e.message);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
})
|
|
105
|
-
.catch((err) => {
|
|
106
|
-
if (callbacks.onPrintError) {
|
|
107
|
-
callbacks.onPrintError("Permisos denegados: " + err);
|
|
108
|
-
} else {
|
|
109
|
-
Dialogs.alert("Permisos denegados: " + err);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
module.exports = {
|
|
115
|
-
startBluetoothPrintFlow
|
|
116
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { android as androidApp } from '@nativescript/core/application';
|
|
2
|
-
import { Dialogs } from '@nativescript/core';
|
|
3
|
-
import * as permissions from 'nativescript-permissions';
|
|
4
|
-
|
|
5
|
-
declare const com: any;
|
|
6
|
-
|
|
7
|
-
export interface PrinterCallbacks {
|
|
8
|
-
onConnectionStateChanged?: (status: string) => void;
|
|
9
|
-
onPrintStarted?: () => void;
|
|
10
|
-
onPrintSuccess?: () => void;
|
|
11
|
-
onPrintError?: (error: string) => void;
|
|
12
|
-
onConnectionFailed?: (error: string) => void;
|
|
13
|
-
onConnectionLost?: () => void;
|
|
14
|
-
showPrintingDialog?: () => void;
|
|
15
|
-
hidePrintingDialog?: () => void;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
@JavaProxy('com.rivercon.appprintbixolon.PrinterControl.PrinterStateListener')
|
|
19
|
-
class PrinterStateListenerImpl extends java.lang.Object implements com.rivercon.appprintbixolon.PrinterControl.PrinterStateListener {
|
|
20
|
-
|
|
21
|
-
private callbacks: PrinterCallbacks;
|
|
22
|
-
|
|
23
|
-
constructor(callbacks: PrinterCallbacks) {
|
|
24
|
-
super();
|
|
25
|
-
this.callbacks = callbacks || {};
|
|
26
|
-
return global.__native(this);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public onConnectionStateChanged(status: string): void {
|
|
30
|
-
if (this.callbacks.onConnectionStateChanged) {
|
|
31
|
-
this.callbacks.onConnectionStateChanged(status);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public showPrintingDialog(): void {
|
|
36
|
-
if (this.callbacks.showPrintingDialog) {
|
|
37
|
-
this.callbacks.showPrintingDialog();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public hidePrintingDialog(): void {
|
|
42
|
-
if (this.callbacks.hidePrintingDialog) {
|
|
43
|
-
this.callbacks.hidePrintingDialog();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public onPrintError(errorMessage: string): void {
|
|
48
|
-
if (this.callbacks.onPrintError) {
|
|
49
|
-
this.callbacks.onPrintError(errorMessage);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public onConnectionFailed(errorMessage: string): void {
|
|
54
|
-
if (this.callbacks.onConnectionFailed) {
|
|
55
|
-
this.callbacks.onConnectionFailed(errorMessage);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public onConnectionLost(): void {
|
|
60
|
-
if (this.callbacks.onConnectionLost) {
|
|
61
|
-
this.callbacks.onConnectionLost();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public onPrintSuccess(): void {
|
|
66
|
-
if (this.callbacks.onPrintSuccess) {
|
|
67
|
-
this.callbacks.onPrintSuccess();
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Inicia el flujo de impresión Bluetooth sin UI nativa.
|
|
74
|
-
* MDK se encarga de:
|
|
75
|
-
* - Obtener la dirección MAC de la impresora.
|
|
76
|
-
* - Mostrar diálogos / loaders / errores al usuario.
|
|
77
|
-
*/
|
|
78
|
-
export function startBluetoothPrintFlow(
|
|
79
|
-
printerAddress: string,
|
|
80
|
-
zplCommand: string,
|
|
81
|
-
callbacks: PrinterCallbacks = {}
|
|
82
|
-
): void {
|
|
83
|
-
const context = androidApp.context;
|
|
84
|
-
|
|
85
|
-
if (!printerAddress || printerAddress.trim() === '') {
|
|
86
|
-
Dialogs.alert('No se ha especificado la dirección de la impresora.');
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (!zplCommand || zplCommand.trim() === '') {
|
|
91
|
-
Dialogs.alert('No hay ZPL para imprimir.');
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const requiredPermissions = [
|
|
96
|
-
android.Manifest.permission.BLUETOOTH_CONNECT,
|
|
97
|
-
android.Manifest.permission.BLUETOOTH_SCAN,
|
|
98
|
-
android.Manifest.permission.ACCESS_FINE_LOCATION
|
|
99
|
-
];
|
|
100
|
-
|
|
101
|
-
permissions
|
|
102
|
-
.requestPermissions(requiredPermissions, 'Se requieren permisos para imprimir')
|
|
103
|
-
.then(() => {
|
|
104
|
-
try {
|
|
105
|
-
// Notificamos que la impresión está por iniciar (opcional)
|
|
106
|
-
if (callbacks.onPrintStarted) {
|
|
107
|
-
callbacks.onPrintStarted();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const listener = new PrinterStateListenerImpl({
|
|
111
|
-
onConnectionStateChanged: callbacks.onConnectionStateChanged,
|
|
112
|
-
showPrintingDialog: callbacks.showPrintingDialog,
|
|
113
|
-
hidePrintingDialog: callbacks.hidePrintingDialog,
|
|
114
|
-
onPrintError: callbacks.onPrintError,
|
|
115
|
-
onConnectionFailed: callbacks.onConnectionFailed,
|
|
116
|
-
onConnectionLost: callbacks.onConnectionLost,
|
|
117
|
-
onPrintSuccess: callbacks.onPrintSuccess
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
const Manager = com.rivercon.appprintbixolon.PrinterControl.BixolonPrinterManager;
|
|
121
|
-
const manager = new Manager(context, listener);
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
manager.startFullSequence(printerAddress, zplCommand);
|
|
125
|
-
} catch (e) {
|
|
126
|
-
if (callbacks.onPrintError) {
|
|
127
|
-
callbacks.onPrintError('Error al iniciar impresión: ' + e.message);
|
|
128
|
-
} else {
|
|
129
|
-
Dialogs.alert('Error al iniciar impresión: ' + e.message);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
} catch (e) {
|
|
133
|
-
if (callbacks.onPrintError) {
|
|
134
|
-
callbacks.onPrintError('Error general en flujo de impresión: ' + e.message);
|
|
135
|
-
} else {
|
|
136
|
-
Dialogs.alert('Error general en flujo de impresión: ' + e.message);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
.catch((err) => {
|
|
141
|
-
if (callbacks.onPrintError) {
|
|
142
|
-
callbacks.onPrintError('Permisos denegados: ' + err);
|
|
143
|
-
} else {
|
|
144
|
-
Dialogs.alert('Permisos denegados: ' + err);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|