Browse Source

feat: zpl print over bluetooth

Raihan Rizal 5 months ago
parent
commit
f129660b6b

+ 13 - 2
android/src/main/java/id/kalanusa/flutter_zsdk/FlutterZsdkPlugin.java

@@ -22,6 +22,7 @@ import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHa
22 22
 import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
23 23
 import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothDiscoveryHandler;
24 24
 import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothPrinter;
25
+import id.kalanusa.flutter_zsdk.zplprinthandler.ZplPrintHandler;
25 26
 import io.flutter.Log;
26 27
 import io.flutter.embedding.engine.dart.DartExecutor;
27 28
 import io.flutter.embedding.engine.plugins.FlutterPlugin;
@@ -54,6 +55,7 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
54 55
   public BluetoothConnectionHolder bluetoothConnectionHolder;
55 56
 
56 57
   public BluetoothConnectionHandler bluetoothConnectionHandler;
58
+  public ZplPrintHandler zplPrintHandler;
57 59
 
58 60
 
59 61
   @Override
@@ -64,6 +66,8 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
64 66
     dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
65 67
     binaryMessenger = dartExecutor.getBinaryMessenger();
66 68
 
69
+
70
+    bluetoothConnectionHolder = new BluetoothConnectionHolder(null);
67 71
   }
68 72
 
69 73
   @Override
@@ -90,19 +94,26 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
90 94
 
91 95
   @Override
92 96
   public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
93
-    Log.w("From Native (Android)", "ini di FLutterZsdkPlugin");
97
+    Log.w("From Native (Android)", "ini di FlutterZsdkPlugin");
94 98
 
95 99
     switch(call.method) {
96 100
       case "bluetoothOpenConnection":
97 101
       case "isConnected":
98 102
       case "bluetoothCloseConnection":
99 103
         if (bluetoothConnectionHandler == null) {
100
-          bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
104
+          bluetoothConnectionHandler = new BluetoothConnectionHandler();
101 105
         }
102 106
 
103 107
         bluetoothConnectionHandler.handle(call, result);
104 108
         break;
105 109
 
110
+      case "printZplOverBluetooth":
111
+        if (zplPrintHandler == null) {
112
+          zplPrintHandler = new ZplPrintHandler();
113
+        }
114
+
115
+        zplPrintHandler.handle(call, result);
116
+        break;
106 117
     }
107 118
   }
108 119
 

+ 5 - 10
android/src/main/java/id/kalanusa/flutter_zsdk/bluetoothconnectionhandler/BluetoothConnectionHandler.java

@@ -13,19 +13,14 @@ import io.flutter.plugin.common.MethodCall;
13 13
 import io.flutter.plugin.common.MethodChannel;
14 14
 
15 15
 public class BluetoothConnectionHandler {
16
-    BluetoothConnectionHolder bluetoothConnectionHolder;
17 16
     String macAddress;
18 17
 
19
-    public BluetoothConnectionHandler(BluetoothConnectionHolder bluetoothConnectionHolder) {
20
-        this.bluetoothConnectionHolder = bluetoothConnectionHolder;
21
-    }
22
-
23 18
     public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
24 19
 
25 20
         switch (call.method) {
26 21
             case "bluetoothOpenConnection":
27
-                if (bluetoothConnectionHolder == null) {
28
-                    bluetoothConnectionHolder = new BluetoothConnectionHolder(new BluetoothConnectionInsecure(call.argument("macAddress")));
22
+                if (BluetoothConnectionHolder.connection == null) {
23
+                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(call.argument("macAddress")));
29 24
                 }
30 25
 
31 26
                 openConnection(result);
@@ -47,7 +42,7 @@ public class BluetoothConnectionHandler {
47 42
             public void run() {
48 43
                 try {
49 44
                     Looper.prepare();
50
-                    bluetoothConnectionHolder.connection.open();
45
+                    BluetoothConnectionHolder.connection.open();
51 46
                     Looper.myLooper().quit();
52 47
                     result.success(null);
53 48
                     Log.w("From Native (Android)", "open connection finished");
@@ -63,7 +58,7 @@ public class BluetoothConnectionHandler {
63 58
         new Thread(new Runnable() {
64 59
             @Override
65 60
             public void run() {
66
-                boolean isConnected = bluetoothConnectionHolder.connection.isConnected();
61
+                boolean isConnected = BluetoothConnectionHolder.connection.isConnected();
67 62
 
68 63
                 result.success(isConnected);
69 64
 
@@ -77,7 +72,7 @@ public class BluetoothConnectionHandler {
77 72
             @Override
78 73
             public void run() {
79 74
                 try {
80
-                    bluetoothConnectionHolder.connection.close();
75
+                    BluetoothConnectionHolder.connection.close();
81 76
                     result.success(null);
82 77
                 } catch (ConnectionException e) {
83 78
                     Log.w("From Native (Android)", "something went wrong when closing connection");

+ 3 - 3
android/src/main/java/id/kalanusa/flutter_zsdk/bluetoothconnectionhandler/BluetoothConnectionHolder.java

@@ -5,13 +5,13 @@ import androidx.annotation.NonNull;
5 5
 import com.zebra.sdk.comm.Connection;
6 6
 
7 7
 public class BluetoothConnectionHolder {
8
-    public Connection connection;
8
+    static public Connection connection;
9 9
 
10 10
     public BluetoothConnectionHolder(@NonNull Connection connection) {
11 11
         this.connection = connection;
12 12
     }
13 13
 
14
-    public void updateConnection(@NonNull Connection connection) {
15
-        this.connection = connection;
14
+    static public void updateConnection(@NonNull Connection connection) {
15
+        BluetoothConnectionHolder.connection = connection;
16 16
     }
17 17
 }

+ 35 - 0
android/src/main/java/id/kalanusa/flutter_zsdk/zplprinthandler/ZplPrintHandler.java

@@ -0,0 +1,35 @@
1
+package id.kalanusa.flutter_zsdk.zplprinthandler;
2
+
3
+import androidx.annotation.NonNull;
4
+
5
+import com.zebra.sdk.comm.ConnectionException;
6
+
7
+import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHandler;
8
+import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
9
+import io.flutter.Log;
10
+import io.flutter.plugin.common.MethodCall;
11
+import io.flutter.plugin.common.MethodChannel;
12
+
13
+public class ZplPrintHandler {
14
+
15
+    public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
16
+        printZplOverBluetooth(result);
17
+    }
18
+
19
+
20
+    public void printZplOverBluetooth(@NonNull MethodChannel.Result result) {
21
+        new Thread(new Runnable() {
22
+            @Override
23
+            public void run() {
24
+//                Log.w("From Native (Android)", "isConnected from zplPrinterHandler " + BluetoothConnectionHolder.connection.isConnected() );
25
+                try {
26
+                    String zplData = "^XA^PW624^LL800^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^FO20,60^BQ , 60,10 , , , A ^FD 1234ABC.^FS^XZ";
27
+                    BluetoothConnectionHolder.connection.write(zplData.getBytes());
28
+                } catch (ConnectionException e) {
29
+                    Log.w("From Native (Android)", "something went wrong when sending data to printer");
30
+                    result.error("WRITE_ERROR", "Something went wrong when sending data to printer", e.getMessage());
31
+                }
32
+            }
33
+        }).start();
34
+    }
35
+}

+ 24 - 0
example/lib/main.dart

@@ -151,6 +151,24 @@ class _MyAppState extends State<MyApp> {
151 151
     setState(() {});
152 152
   }
153 153
 
154
+  Future<void> printZplOverBluetooth() async {
155
+    print('invoked printZplOverBluetooth from dart');
156
+    try {
157
+      await _flutterZsdkPlugin.printZplOverBluetooth();
158
+      print('printZplOverBluetooth successfully from dart');
159
+      
160
+    } on FlutterZsdkException catch (e) {
161
+      inspect(e);
162
+      showSnackBar(e.message);
163
+
164
+    } catch (e) {
165
+      inspect(e);
166
+      showSnackBar('Unexpected error when send data to printers');
167
+    }
168
+
169
+    setState(() {});
170
+  }
171
+
154 172
   @override
155 173
   Widget build(BuildContext context) {
156 174
     return Scaffold(
@@ -178,6 +196,12 @@ class _MyAppState extends State<MyApp> {
178 196
             ),
179 197
             SizedBox(height: 20),
180 198
 
199
+            ElevatedButton(
200
+              child: Text('Print zpl over bluetooth'),
201
+              onPressed: _connectedPrinter == null ? null : printZplOverBluetooth,
202
+            ),
203
+            SizedBox(height: 20),
204
+
181 205
             ElevatedButton(
182 206
               child: Text('Check connection status'),
183 207
               onPressed: () async {

+ 4 - 0
lib/flutter_zsdk.dart

@@ -24,4 +24,8 @@ class FlutterZsdk {
24 24
   Future<void> closeConnection() {
25 25
     return FlutterZsdkPlatform.instance.closeConnection();
26 26
   }
27
+
28
+  Future<void> printZplOverBluetooth() {
29
+    return FlutterZsdkPlatform.instance.printZplOverBluetooth();
30
+  }
27 31
 }

+ 9 - 0
lib/src/flutter_zsdk_method_channel.dart

@@ -113,4 +113,13 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
113 113
       throw FlutterZsdkException("Failed to close connection to printer: $e");
114 114
     }
115 115
   }
116
+
117
+  @override
118
+  Future<void> printZplOverBluetooth() async {
119
+    try {
120
+      await methodChannel.invokeMethod('printZplOverBluetooth');
121
+    } catch (e) {
122
+      throw FlutterZsdkException("Failed to print ZPL over Bluetooth: $e");
123
+    }
124
+  }
116 125
 }

+ 4 - 0
lib/src/flutter_zsdk_platform_interface.dart

@@ -43,4 +43,8 @@ abstract class FlutterZsdkPlatform extends PlatformInterface {
43 43
   Future<void> closeConnection() {
44 44
     throw UnimplementedError('closeConnection() has not been implemented.');
45 45
   }
46
+
47
+  Future<void> printZplOverBluetooth() {
48
+    throw UnimplementedError('printZplOverBluetooth() has not been implemented.');
49
+  }
46 50
 }