6 Commity e1f339d0da ... 39a97e8bdb

Autor SHA1 Wiadomość Data
  Raihan Rizal 39a97e8bdb fix: error handler, connection logic 5 miesięcy temu
  Raihan Rizal 173c58f15f feat: update readme 5 miesięcy temu
  Raihan Rizal a153758db1 feat: change bluetooth printer connection 5 miesięcy temu
  Raihan Rizal f4ea70d1d3 fix: example zplData 5 miesięcy temu
  Raihan Rizal 2b5a99fe23 feat: input zplData 5 miesięcy temu
  Raihan Rizal f129660b6b feat: zpl print over bluetooth 5 miesięcy temu

+ 102 - 8
README.md

@@ -3,11 +3,105 @@ Flutter Pluggin for Zebra Multiplatform SDK
3 3
 
4 4
 ## Getting Started
5 5
 
6
-This project is a starting point for a Flutter
7
-[plug-in package](https://flutter.dev/developing-packages/),
8
-a specialized package that includes platform-specific implementation code for
9
-Android and/or iOS.
10
-
11
-For help getting started with Flutter development, view the
12
-[online documentation](https://flutter.dev/docs), which offers tutorials,
13
-samples, guidance on mobile development, and a full API reference.
6
+Add `flutter_zsdk` as a dependency in your `pubspec.yaml` file.
7
+
8
+## Project Setup
9
+
10
+### Android
11
+note: tested using `JDK 17.0.2`, `Gradle 8.0`, `AGP 8.1.3`
12
+
13
+Tambah / ganti `distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip` di android/gradle/wrapper/gradle-wrapper.properties.
14
+
15
+Tambah / ganti `id "com.android.application" version "8.1.3" apply false` di android/settings.gradle.
16
+
17
+Tambah ke file `gradle.properties`.
18
+```
19
+org.gradle.jvmargs=-Xmx4G
20
+android.useAndroidX=true
21
+android.enableJetifier=true
22
+android.jetifier.ignorelist=jackson-core-2.17.2.jar
23
+```
24
+
25
+## Usage
26
+Contoh lengkap bisa dilihat di `example` folder.  
27
+
28
+Pertama buat objek dari plugin
29
+```
30
+import 'package:flutter_zsdk/flutter_zsdk.dart';
31
+
32
+final _flutterZsdkPlugin = FlutterZsdk();
33
+
34
+_flutterZsdk.findBluetoothPrinter();
35
+// etc
36
+```
37
+
38
+### Discover Bluetooth Device
39
+Return nilai dari `_flutterZsdkPlugin.findBluetoothPrinter()` itu `Stream<dynamic>` jadi perlu di handle untuk listen ke stream tsb.
40
+
41
+Default event:
42
+- `SOS` : Start of Stream
43
+- `EOS` : End of Stream (`StreamSubscription` harus di cancel jika tidak diperlukan lagi)
44
+
45
+contoh:
46
+```
47
+Future<void> discoverBluetoothDevices() async {
48
+    try {
49
+      Stream<dynamic> stream = await _flutterZsdkPlugin.findBluetoothPrinters();
50
+      _bluetoothPrinterSubscription = stream.listen((event) {
51
+        print(event);
52
+
53
+        if (event == 'SOS') {
54
+          _isDiscovering = true;
55
+
56
+        }
57
+
58
+        if (event is List) {
59
+          // add to list logic here...
60
+
61
+        }
62
+
63
+        if (event == 'EOS') {
64
+          _isDiscovering = false;
65
+          showSnackBar('Bluetooth discovery finished, found ${_discoveredBluetoothPrinters.length} printers');
66
+          _bluetoothPrinterSubscription?.cancel();
67
+        }
68
+
69
+        setState(() {});
70
+      });
71
+    
72
+    } on PlatformException catch (e) {
73
+      showSnackBar(e.message.toString());
74
+
75
+    } on FlutterZsdkException catch (e) { 
76
+      showSnackBar(e.message);
77
+
78
+    } catch (e) {
79
+      showSnackBar('Unexpected error while discovering bluetooth printers');
80
+
81
+    }
82
+  }
83
+  ```
84
+
85
+
86
+### Connect to Printer
87
+Gunakan `_flutterZsdkPlugin.openConnection(<Mac Address>)`, dimana `<Mac Address>` bisa didapatkan dari `.findBluetoothPrinter()`
88
+
89
+disarankan `.closeConnection()` sebelum melakukan connect ke printer lain. walaupun sudah dihandle disisi plugin.
90
+
91
+### Send ZPL Data to Printer (Print ZPL)
92
+sebelum print, check dulu koneksi ke printer, utk memastikan printer terkoneksi.
93
+
94
+`_flutterZsdkPlugin.isConnected()` utk check koneksi ke printer.
95
+
96
+jika sudah terkoneksi, gunakan `_flutterZsdkPlugin.printZplOverBluetooth(<zpl_data>)` untuk mengirim data ZPL ke printer.
97
+
98
+
99
+### Disconnect from Printer
100
+`_flutterZsdkPlugin.closeConnection()` utk disconnect dari printer.
101
+
102
+</br>
103
+
104
+
105
+note: 
106
+- semua aksi harus dipanggil dalam try-catch block
107
+- `_flutterZsdkPlugin.isConnected()` hanya harus dipanggil setelah `_flutterZsdkPlugin.openConnection()` jika tidak app bisa crash (tested ANDROID)

+ 13 - 5
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;
@@ -44,16 +45,14 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
44 45
   private MethodChannel channel;
45 46
   private Context context;
46 47
   private Activity activity;
47
-  private ArrayList<BluetoothPrinter> discoveredPrinterList = new ArrayList<BluetoothPrinter>();
48 48
   public static final String bluetoothDiscoveryEventChannel = "id.kalanusa.flutter_zsdk.channel_events/bluetooth_discovery";
49
-  private EventChannel.EventSink attachEvent;
50
-  private Handler handler;
51 49
   private DartExecutor dartExecutor;
52 50
   private BinaryMessenger binaryMessenger;
53 51
 
54 52
   public BluetoothConnectionHolder bluetoothConnectionHolder;
55 53
 
56 54
   public BluetoothConnectionHandler bluetoothConnectionHandler;
55
+  public ZplPrintHandler zplPrintHandler;
57 56
 
58 57
 
59 58
   @Override
@@ -64,6 +63,8 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
64 63
     dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
65 64
     binaryMessenger = dartExecutor.getBinaryMessenger();
66 65
 
66
+
67
+    bluetoothConnectionHolder = new BluetoothConnectionHolder(null);
67 68
   }
68 69
 
69 70
   @Override
@@ -90,19 +91,26 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
90 91
 
91 92
   @Override
92 93
   public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
93
-    Log.w("From Native (Android)", "ini di FLutterZsdkPlugin");
94
+    Log.w("From Native (Android)", "ini di FlutterZsdkPlugin");
94 95
 
95 96
     switch(call.method) {
96 97
       case "bluetoothOpenConnection":
97 98
       case "isConnected":
98 99
       case "bluetoothCloseConnection":
99 100
         if (bluetoothConnectionHandler == null) {
100
-          bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
101
+          bluetoothConnectionHandler = new BluetoothConnectionHandler();
101 102
         }
102 103
 
103 104
         bluetoothConnectionHandler.handle(call, result);
104 105
         break;
105 106
 
107
+      case "printZplOverBluetooth":
108
+        if (zplPrintHandler == null) {
109
+          zplPrintHandler = new ZplPrintHandler();
110
+        }
111
+
112
+        zplPrintHandler.handle(call, result);
113
+        break;
106 114
     }
107 115
   }
108 116
 

+ 60 - 14
android/src/main/java/id/kalanusa/flutter_zsdk/bluetoothconnectionhandler/BluetoothConnectionHandler.java

@@ -13,22 +13,34 @@ 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
-    String macAddress;
18
-
19
-    public BluetoothConnectionHandler(BluetoothConnectionHolder bluetoothConnectionHolder) {
20
-        this.bluetoothConnectionHolder = bluetoothConnectionHolder;
21
-    }
22 16
 
23 17
     public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
24 18
 
25 19
         switch (call.method) {
26 20
             case "bluetoothOpenConnection":
27
-                if (bluetoothConnectionHolder == null) {
28
-                    bluetoothConnectionHolder = new BluetoothConnectionHolder(new BluetoothConnectionInsecure(call.argument("macAddress")));
21
+                String macAddressArgument = call.argument("macAddress");
22
+
23
+                if (BluetoothConnectionHolder.connection != null) {
24
+                    String connectionInfo = BluetoothConnectionHolder.connection.getSimpleConnectionName();
25
+                    String currentConnectionMacAddress = connectionInfo.substring(0, 17);
26
+
27
+                    if (!currentConnectionMacAddress.equals(macAddressArgument)) {
28
+                        closeAndReopenConnection(result, macAddressArgument);
29
+
30
+                    } else {
31
+                        openConnection(result);
32
+
33
+                    }
34
+
35
+                    break;
29 36
                 }
30 37
 
31
-                openConnection(result);
38
+                if (BluetoothConnectionHolder.connection == null) {
39
+                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(macAddressArgument));
40
+
41
+                    openConnection(result);
42
+
43
+                }
32 44
                 break;
33 45
 
34 46
             case "isConnected":
@@ -47,7 +59,7 @@ public class BluetoothConnectionHandler {
47 59
             public void run() {
48 60
                 try {
49 61
                     Looper.prepare();
50
-                    bluetoothConnectionHolder.connection.open();
62
+                    BluetoothConnectionHolder.connection.open();
51 63
                     Looper.myLooper().quit();
52 64
                     result.success(null);
53 65
                     Log.w("From Native (Android)", "open connection finished");
@@ -59,15 +71,48 @@ public class BluetoothConnectionHandler {
59 71
         }).start();
60 72
     }
61 73
 
74
+    public void closeAndReopenConnection(@NonNull MethodChannel.Result result, String newMacAddress) {
75
+        new Thread(new Runnable() {
76
+            @Override
77
+            public void run() {
78
+                try {
79
+                    Looper.prepare();
80
+                    BluetoothConnectionHolder.connection.close();
81
+                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(newMacAddress));
82
+                    BluetoothConnectionHolder.connection.open();
83
+                    Looper.myLooper().quit();
84
+                    result.success(null);
85
+                    Log.w("From Native (Android)", "close and reopen connection finished");
86
+                } catch (ConnectionException e) {
87
+                    Log.w("From Native (Android)", "Connection Exception threw " + e);
88
+                    result.error("CLOSE_AND_REOPEN_CONNECTION_ERROR", "Cant close and connect to new printers", e.getMessage());
89
+                }
90
+            }
91
+        }).start();
92
+    }
93
+
62 94
     public void checkConnection(@NonNull MethodChannel.Result result) {
63 95
         new Thread(new Runnable() {
64 96
             @Override
65 97
             public void run() {
66
-                boolean isConnected = bluetoothConnectionHolder.connection.isConnected();
98
+//                BluetoothConnectionHolder.getSimpleConnectionName();
99
+                try {
100
+                    boolean isConnected = BluetoothConnectionHolder.connection.isConnected();
101
+
102
+                    result.success(isConnected);
67 103
 
68
-                result.success(isConnected);
104
+                    Log.w("From Native (Android)", "isConnected: " + isConnected);
105
+                } catch (Exception e) {
106
+                    Log.w("From Native (Android)", "Cant get connection status");
107
+                    Log.w("From Native (Android)", e.getClass().toString());
108
+                    Log.w("From Native (Android)", "is exception nullPointerException: " + (e.getClass() == NullPointerException.class));
69 109
 
70
-                Log.w("From Native (Android)", "isConnected: " + isConnected);
110
+                    if (e.getClass() == NullPointerException.class) {
111
+                        result.error("CONNECTION_HAS_NOT_BEEN_ESTABLISHED", "Cant get connection status, establish connection first", e.getCause());
112
+                    } else {
113
+                        result.error("CHECK_CONNECTION_ERROR", "Cant get connection status", e.getMessage());
114
+                    }
115
+                }
71 116
             }
72 117
         }).start();
73 118
     }
@@ -77,7 +122,8 @@ public class BluetoothConnectionHandler {
77 122
             @Override
78 123
             public void run() {
79 124
                 try {
80
-                    bluetoothConnectionHolder.connection.close();
125
+                    BluetoothConnectionHolder.connection.close();
126
+                    BluetoothConnectionHolder.connection = null;
81 127
                     result.success(null);
82 128
                 } catch (ConnectionException e) {
83 129
                     Log.w("From Native (Android)", "something went wrong when closing connection");

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

@@ -4,14 +4,25 @@ import androidx.annotation.NonNull;
4 4
 
5 5
 import com.zebra.sdk.comm.Connection;
6 6
 
7
+import io.flutter.Log;
8
+
7 9
 public class BluetoothConnectionHolder {
8
-    public Connection connection;
10
+    static public Connection connection;
9 11
 
10 12
     public BluetoothConnectionHolder(@NonNull Connection connection) {
11 13
         this.connection = connection;
12 14
     }
13 15
 
14
-    public void updateConnection(@NonNull Connection connection) {
15
-        this.connection = connection;
16
+    static public void updateConnection(@NonNull Connection connection) {
17
+        BluetoothConnectionHolder.connection = connection;
18
+    }
19
+
20
+    static public void getSimpleConnectionName() {
21
+        String simpleConnectionName = BluetoothConnectionHolder.connection.getSimpleConnectionName();
22
+        String macAddressFromSimpleConnectionName = simpleConnectionName.substring(0, 17);
23
+
24
+
25
+        Log.w("From Native (Android)", "SimpleConnectioName: " + BluetoothConnectionHolder.connection.getSimpleConnectionName());
26
+        Log.w("From Native (Android)", "MacAddressFromSimpleConnectionName: " + macAddressFromSimpleConnectionName);
16 27
     }
17 28
 }

+ 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, call.argument("zplData"));
17
+    }
18
+
19
+
20
+    public void printZplOverBluetooth(@NonNull MethodChannel.Result result, String zplData) {
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
+}

+ 41 - 6
example/lib/main.dart

@@ -151,6 +151,39 @@ 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("^XA^PW624^LL800^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^FO20,60^BQ ,2,10 , , , A ^FDQA,http://192.168.102.170:4218/?q=IDC4C321S023^FS^^FO20,500^A0N,25,25^FDDart Zpl Test.^FS^XZ");
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
+
172
+  Future<void> checkConnectionStatus() async {
173
+    print('invoked checkConnectionStatus from dart');
174
+    try{
175
+      bool isConnected = await _flutterZsdkPlugin.isConnected();
176
+              
177
+      print('isConnected from dart: $isConnected');
178
+    } on FlutterZsdkException catch (e) {
179
+      inspect(e);
180
+      showSnackBar(e.message.toString());
181
+    } catch (e) {
182
+      inspect(e);
183
+      showSnackBar('Unexpected error while checking connection status');
184
+    }
185
+  }
186
+
154 187
   @override
155 188
   Widget build(BuildContext context) {
156 189
     return Scaffold(
@@ -168,7 +201,7 @@ class _MyAppState extends State<MyApp> {
168 201
         
169 202
             ElevatedButton(
170 203
               child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
171
-              onPressed: _selectedBluetoothPrinterMacAddress == null || _connectedPrinter != null ? null : openConnection,
204
+              onPressed: _selectedBluetoothPrinterMacAddress == null ? null : openConnection,
172 205
             ),
173 206
             SizedBox(height: 20),
174 207
 
@@ -178,13 +211,15 @@ class _MyAppState extends State<MyApp> {
178 211
             ),
179 212
             SizedBox(height: 20),
180 213
 
214
+            ElevatedButton(
215
+              child: Text('Print zpl over bluetooth'),
216
+              onPressed: _connectedPrinter == null ? null : printZplOverBluetooth,
217
+            ),
218
+            SizedBox(height: 20),
219
+
181 220
             ElevatedButton(
182 221
               child: Text('Check connection status'),
183
-              onPressed: () async {
184
-                bool isConnected =await _flutterZsdkPlugin.isConnected();
185
-              
186
-                print('isConnected from dart: $isConnected');
187
-              },
222
+              onPressed: checkConnectionStatus,
188 223
             ),
189 224
             SizedBox(height: 20),
190 225
             

+ 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(String zplData) {
29
+    return FlutterZsdkPlatform.instance.printZplOverBluetooth(zplData);
30
+  }
27 31
 }

+ 31 - 9
lib/src/flutter_zsdk_method_channel.dart

@@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';
7 7
 import 'package:flutter/services.dart';
8 8
 import 'package:flutter_blue_plus/flutter_blue_plus.dart';
9 9
 import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
10
+import 'package:flutter_zsdk/src/models/error_code.dart';
10 11
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
11 12
 import 'package:permission_handler/permission_handler.dart';
12 13
 
@@ -37,7 +38,7 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
37 38
     //   print(event);
38 39
     // });
39 40
     if (!await FlutterBluePlus.isSupported) {
40
-      throw FlutterZsdkException("Bluetooth is not supported on this device.");
41
+      throw FlutterZsdkException(ErrorCode.bluetoothIsNotSupported, "Bluetooth is not supported on this device.");
41 42
     }
42 43
 
43 44
 
@@ -46,10 +47,10 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
46 47
         try {
47 48
           await FlutterBluePlus.turnOn();
48 49
         } on FlutterBluePlusException catch (e) {
49
-          throw FlutterZsdkException("Failed to turn on bluetooth: ${e.description}");
50
+          throw FlutterZsdkException(ErrorCode.turnOnBluetoothError, "Failed to turn on bluetooth: ${e.description}");
50 51
         }
51 52
       } else {
52
-        throw FlutterZsdkException("Bluetooth is not turned on on this device.");
53
+        throw FlutterZsdkException(ErrorCode.bluetoothDisabled, "Bluetooth is not turned on on this device.");
53 54
       }
54 55
     }
55 56
 
@@ -75,7 +76,7 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
75 76
         return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
76 77
       } else {
77 78
         // throw Exception("Permissions not granted, please allow permission to discover bluetooth printers.");
78
-        throw FlutterZsdkException("Permissions not granted, please allow permission to discover bluetooth printers.");
79
+        throw FlutterZsdkException(ErrorCode.permissionNotGranted, "Permissions not granted, please allow permission to discover bluetooth printers.");
79 80
       }
80 81
 
81 82
     } else {
@@ -91,26 +92,47 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
91 92
 
92 93
     } on PlatformException catch (e) {
93 94
       inspect(e);
94
-      throw FlutterZsdkException("Failed to open connection to printer: ${e.details}");
95
+      throw FlutterZsdkException(e.code, "Failed to open connection to printer: ${e.details}");
95 96
 
96 97
     } catch (e) {
97
-      throw FlutterZsdkException("Failed to open connection to printer: $e");
98
+      throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
98 99
     }
99 100
   }
100 101
 
101 102
   @override
102 103
   Future<bool> isConnected() async {
103
-    bool isConnected = await methodChannel.invokeMethod('isConnected');
104
+    try {
105
+      bool isConnected = await methodChannel.invokeMethod('isConnected');
106
+      return isConnected;
107
+    } on PlatformException catch (e) {
108
+      throw FlutterZsdkException(e.code, "Failed to check connection , message: ${e.details}");
109
+    } catch (e) {
110
+      throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
111
+    }
104 112
 
105
-    return isConnected;
106 113
   }
107 114
 
108 115
   @override
109 116
   Future<void> closeConnection() async {
110 117
     try {
111 118
       await methodChannel.invokeMethod('bluetoothCloseConnection');
119
+    } on PlatformException catch (e) {
120
+      inspect(e);
121
+      throw FlutterZsdkException(e.code, "Failed to close connection, message: ${e.details}");
122
+    } catch (e) {
123
+      throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
124
+    }
125
+  }
126
+
127
+  @override
128
+  Future<void> printZplOverBluetooth(String zplData) async {
129
+    try {
130
+      await methodChannel.invokeMethod('printZplOverBluetooth', {'zplData': zplData});
131
+    } on PlatformException catch (e) {
132
+      inspect(e);
133
+      throw FlutterZsdkException(e.code, "Failed to print ZPL over Bluetooth, message: ${e.details}");
112 134
     } catch (e) {
113
-      throw FlutterZsdkException("Failed to close connection to printer: $e");
135
+      throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
114 136
     }
115 137
   }
116 138
 }

+ 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(String zplData) {
48
+    throw UnimplementedError('printZplOverBluetooth() has not been implemented.');
49
+  }
46 50
 }

+ 16 - 0
lib/src/models/error_code.dart

@@ -0,0 +1,16 @@
1
+class ErrorCode {
2
+  static const String permissionNotGranted = "PERMISSION_NOT_GRANTED";
3
+  static const String turnOnBluetoothError = "TURN_ON_BLUETOOTH_ERROR";
4
+  static const String bluetoothIsNotSupported = "BLUETOOTH_IS_NOT_SUPPORTED";
5
+  static const String bluetoothDisabled = "BLUETOOTH_DISABLED";
6
+
7
+  static const String discoveryError = "DISCOVERY_ERROR";
8
+  static const String openConnectionError = "OPEN_CONNECTION_ERROR";
9
+  static const String closeAndReopenConnectionError = "CLOSE_AND_REOPEN_CONNECTION_ERROR";
10
+  static const String connectionHasNotBeenEstablished = "CONNECTION_HAS_NOT_BEEN_ESTABLISHED";
11
+  static const String checkConnectionError = "CHECK_CONNECTION_ERROR";
12
+  static const String closeConnectionError = "CLOSE_CONNECTION_ERROR";
13
+  static const String writeError = "WRITE_ERROR";
14
+
15
+  static const String unexpectedError = "UNEXPECTED_ERROR";
16
+}

+ 2 - 1
lib/src/models/flutter_zsdk_exception.dart

@@ -1,7 +1,8 @@
1 1
 class FlutterZsdkException implements Exception {
2
+  String code;
2 3
   String message;
3 4
 
4
-  FlutterZsdkException(this.message);
5
+  FlutterZsdkException(this.code, this.message);
5 6
 
6 7
   @override
7 8
   String toString() {