瀏覽代碼

feat: zpl print over bluetooth

Raihan Rizal 5 月之前
父節點
當前提交
f129660b6b

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

22
 import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
22
 import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
23
 import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothDiscoveryHandler;
23
 import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothDiscoveryHandler;
24
 import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothPrinter;
24
 import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothPrinter;
25
+import id.kalanusa.flutter_zsdk.zplprinthandler.ZplPrintHandler;
25
 import io.flutter.Log;
26
 import io.flutter.Log;
26
 import io.flutter.embedding.engine.dart.DartExecutor;
27
 import io.flutter.embedding.engine.dart.DartExecutor;
27
 import io.flutter.embedding.engine.plugins.FlutterPlugin;
28
 import io.flutter.embedding.engine.plugins.FlutterPlugin;
54
   public BluetoothConnectionHolder bluetoothConnectionHolder;
55
   public BluetoothConnectionHolder bluetoothConnectionHolder;
55
 
56
 
56
   public BluetoothConnectionHandler bluetoothConnectionHandler;
57
   public BluetoothConnectionHandler bluetoothConnectionHandler;
58
+  public ZplPrintHandler zplPrintHandler;
57
 
59
 
58
 
60
 
59
   @Override
61
   @Override
64
     dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
66
     dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
65
     binaryMessenger = dartExecutor.getBinaryMessenger();
67
     binaryMessenger = dartExecutor.getBinaryMessenger();
66
 
68
 
69
+
70
+    bluetoothConnectionHolder = new BluetoothConnectionHolder(null);
67
   }
71
   }
68
 
72
 
69
   @Override
73
   @Override
90
 
94
 
91
   @Override
95
   @Override
92
   public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
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
     switch(call.method) {
99
     switch(call.method) {
96
       case "bluetoothOpenConnection":
100
       case "bluetoothOpenConnection":
97
       case "isConnected":
101
       case "isConnected":
98
       case "bluetoothCloseConnection":
102
       case "bluetoothCloseConnection":
99
         if (bluetoothConnectionHandler == null) {
103
         if (bluetoothConnectionHandler == null) {
100
-          bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
104
+          bluetoothConnectionHandler = new BluetoothConnectionHandler();
101
         }
105
         }
102
 
106
 
103
         bluetoothConnectionHandler.handle(call, result);
107
         bluetoothConnectionHandler.handle(call, result);
104
         break;
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
 import io.flutter.plugin.common.MethodChannel;
13
 import io.flutter.plugin.common.MethodChannel;
14
 
14
 
15
 public class BluetoothConnectionHandler {
15
 public class BluetoothConnectionHandler {
16
-    BluetoothConnectionHolder bluetoothConnectionHolder;
17
     String macAddress;
16
     String macAddress;
18
 
17
 
19
-    public BluetoothConnectionHandler(BluetoothConnectionHolder bluetoothConnectionHolder) {
20
-        this.bluetoothConnectionHolder = bluetoothConnectionHolder;
21
-    }
22
-
23
     public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
18
     public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
24
 
19
 
25
         switch (call.method) {
20
         switch (call.method) {
26
             case "bluetoothOpenConnection":
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
                 openConnection(result);
26
                 openConnection(result);
47
             public void run() {
42
             public void run() {
48
                 try {
43
                 try {
49
                     Looper.prepare();
44
                     Looper.prepare();
50
-                    bluetoothConnectionHolder.connection.open();
45
+                    BluetoothConnectionHolder.connection.open();
51
                     Looper.myLooper().quit();
46
                     Looper.myLooper().quit();
52
                     result.success(null);
47
                     result.success(null);
53
                     Log.w("From Native (Android)", "open connection finished");
48
                     Log.w("From Native (Android)", "open connection finished");
63
         new Thread(new Runnable() {
58
         new Thread(new Runnable() {
64
             @Override
59
             @Override
65
             public void run() {
60
             public void run() {
66
-                boolean isConnected = bluetoothConnectionHolder.connection.isConnected();
61
+                boolean isConnected = BluetoothConnectionHolder.connection.isConnected();
67
 
62
 
68
                 result.success(isConnected);
63
                 result.success(isConnected);
69
 
64
 
77
             @Override
72
             @Override
78
             public void run() {
73
             public void run() {
79
                 try {
74
                 try {
80
-                    bluetoothConnectionHolder.connection.close();
75
+                    BluetoothConnectionHolder.connection.close();
81
                     result.success(null);
76
                     result.success(null);
82
                 } catch (ConnectionException e) {
77
                 } catch (ConnectionException e) {
83
                     Log.w("From Native (Android)", "something went wrong when closing connection");
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
 import com.zebra.sdk.comm.Connection;
5
 import com.zebra.sdk.comm.Connection;
6
 
6
 
7
 public class BluetoothConnectionHolder {
7
 public class BluetoothConnectionHolder {
8
-    public Connection connection;
8
+    static public Connection connection;
9
 
9
 
10
     public BluetoothConnectionHolder(@NonNull Connection connection) {
10
     public BluetoothConnectionHolder(@NonNull Connection connection) {
11
         this.connection = connection;
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

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
     setState(() {});
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
   @override
172
   @override
155
   Widget build(BuildContext context) {
173
   Widget build(BuildContext context) {
156
     return Scaffold(
174
     return Scaffold(
178
             ),
196
             ),
179
             SizedBox(height: 20),
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
             ElevatedButton(
205
             ElevatedButton(
182
               child: Text('Check connection status'),
206
               child: Text('Check connection status'),
183
               onPressed: () async {
207
               onPressed: () async {

+ 4 - 0
lib/flutter_zsdk.dart

24
   Future<void> closeConnection() {
24
   Future<void> closeConnection() {
25
     return FlutterZsdkPlatform.instance.closeConnection();
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
       throw FlutterZsdkException("Failed to close connection to printer: $e");
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
   Future<void> closeConnection() {
43
   Future<void> closeConnection() {
44
     throw UnimplementedError('closeConnection() has not been implemented.');
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
 }