6 Ревизии e1f339d0da ... 39a97e8bdb

Автор SHA1 Съобщение Дата
  Raihan Rizal 39a97e8bdb fix: error handler, connection logic преди 5 месеца
  Raihan Rizal 173c58f15f feat: update readme преди 5 месеца
  Raihan Rizal a153758db1 feat: change bluetooth printer connection преди 5 месеца
  Raihan Rizal f4ea70d1d3 fix: example zplData преди 5 месеца
  Raihan Rizal 2b5a99fe23 feat: input zplData преди 5 месеца
  Raihan Rizal f129660b6b feat: zpl print over bluetooth преди 5 месеца

+ 102 - 8
README.md

3
 
3
 
4
 ## Getting Started
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
 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;
44
   private MethodChannel channel;
45
   private MethodChannel channel;
45
   private Context context;
46
   private Context context;
46
   private Activity activity;
47
   private Activity activity;
47
-  private ArrayList<BluetoothPrinter> discoveredPrinterList = new ArrayList<BluetoothPrinter>();
48
   public static final String bluetoothDiscoveryEventChannel = "id.kalanusa.flutter_zsdk.channel_events/bluetooth_discovery";
48
   public static final String bluetoothDiscoveryEventChannel = "id.kalanusa.flutter_zsdk.channel_events/bluetooth_discovery";
49
-  private EventChannel.EventSink attachEvent;
50
-  private Handler handler;
51
   private DartExecutor dartExecutor;
49
   private DartExecutor dartExecutor;
52
   private BinaryMessenger binaryMessenger;
50
   private BinaryMessenger binaryMessenger;
53
 
51
 
54
   public BluetoothConnectionHolder bluetoothConnectionHolder;
52
   public BluetoothConnectionHolder bluetoothConnectionHolder;
55
 
53
 
56
   public BluetoothConnectionHandler bluetoothConnectionHandler;
54
   public BluetoothConnectionHandler bluetoothConnectionHandler;
55
+  public ZplPrintHandler zplPrintHandler;
57
 
56
 
58
 
57
 
59
   @Override
58
   @Override
64
     dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
63
     dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
65
     binaryMessenger = dartExecutor.getBinaryMessenger();
64
     binaryMessenger = dartExecutor.getBinaryMessenger();
66
 
65
 
66
+
67
+    bluetoothConnectionHolder = new BluetoothConnectionHolder(null);
67
   }
68
   }
68
 
69
 
69
   @Override
70
   @Override
90
 
91
 
91
   @Override
92
   @Override
92
   public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
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
     switch(call.method) {
96
     switch(call.method) {
96
       case "bluetoothOpenConnection":
97
       case "bluetoothOpenConnection":
97
       case "isConnected":
98
       case "isConnected":
98
       case "bluetoothCloseConnection":
99
       case "bluetoothCloseConnection":
99
         if (bluetoothConnectionHandler == null) {
100
         if (bluetoothConnectionHandler == null) {
100
-          bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
101
+          bluetoothConnectionHandler = new BluetoothConnectionHandler();
101
         }
102
         }
102
 
103
 
103
         bluetoothConnectionHandler.handle(call, result);
104
         bluetoothConnectionHandler.handle(call, result);
104
         break;
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
 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;
18
-
19
-    public BluetoothConnectionHandler(BluetoothConnectionHolder bluetoothConnectionHolder) {
20
-        this.bluetoothConnectionHolder = bluetoothConnectionHolder;
21
-    }
22
 
16
 
23
     public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
17
     public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
24
 
18
 
25
         switch (call.method) {
19
         switch (call.method) {
26
             case "bluetoothOpenConnection":
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
                 break;
44
                 break;
33
 
45
 
34
             case "isConnected":
46
             case "isConnected":
47
             public void run() {
59
             public void run() {
48
                 try {
60
                 try {
49
                     Looper.prepare();
61
                     Looper.prepare();
50
-                    bluetoothConnectionHolder.connection.open();
62
+                    BluetoothConnectionHolder.connection.open();
51
                     Looper.myLooper().quit();
63
                     Looper.myLooper().quit();
52
                     result.success(null);
64
                     result.success(null);
53
                     Log.w("From Native (Android)", "open connection finished");
65
                     Log.w("From Native (Android)", "open connection finished");
59
         }).start();
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
     public void checkConnection(@NonNull MethodChannel.Result result) {
94
     public void checkConnection(@NonNull MethodChannel.Result result) {
63
         new Thread(new Runnable() {
95
         new Thread(new Runnable() {
64
             @Override
96
             @Override
65
             public void run() {
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
         }).start();
117
         }).start();
73
     }
118
     }
77
             @Override
122
             @Override
78
             public void run() {
123
             public void run() {
79
                 try {
124
                 try {
80
-                    bluetoothConnectionHolder.connection.close();
125
+                    BluetoothConnectionHolder.connection.close();
126
+                    BluetoothConnectionHolder.connection = null;
81
                     result.success(null);
127
                     result.success(null);
82
                 } catch (ConnectionException e) {
128
                 } catch (ConnectionException e) {
83
                     Log.w("From Native (Android)", "something went wrong when closing connection");
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
 
4
 
5
 import com.zebra.sdk.comm.Connection;
5
 import com.zebra.sdk.comm.Connection;
6
 
6
 
7
+import io.flutter.Log;
8
+
7
 public class BluetoothConnectionHolder {
9
 public class BluetoothConnectionHolder {
8
-    public Connection connection;
10
+    static public Connection connection;
9
 
11
 
10
     public BluetoothConnectionHolder(@NonNull Connection connection) {
12
     public BluetoothConnectionHolder(@NonNull Connection connection) {
11
         this.connection = connection;
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

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
     setState(() {});
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
   @override
187
   @override
155
   Widget build(BuildContext context) {
188
   Widget build(BuildContext context) {
156
     return Scaffold(
189
     return Scaffold(
168
         
201
         
169
             ElevatedButton(
202
             ElevatedButton(
170
               child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
203
               child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
171
-              onPressed: _selectedBluetoothPrinterMacAddress == null || _connectedPrinter != null ? null : openConnection,
204
+              onPressed: _selectedBluetoothPrinterMacAddress == null ? null : openConnection,
172
             ),
205
             ),
173
             SizedBox(height: 20),
206
             SizedBox(height: 20),
174
 
207
 
178
             ),
211
             ),
179
             SizedBox(height: 20),
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
             ElevatedButton(
220
             ElevatedButton(
182
               child: Text('Check connection status'),
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
             SizedBox(height: 20),
224
             SizedBox(height: 20),
190
             
225
             

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

+ 31 - 9
lib/src/flutter_zsdk_method_channel.dart

7
 import 'package:flutter/services.dart';
7
 import 'package:flutter/services.dart';
8
 import 'package:flutter_blue_plus/flutter_blue_plus.dart';
8
 import 'package:flutter_blue_plus/flutter_blue_plus.dart';
9
 import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
9
 import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
10
+import 'package:flutter_zsdk/src/models/error_code.dart';
10
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
11
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
11
 import 'package:permission_handler/permission_handler.dart';
12
 import 'package:permission_handler/permission_handler.dart';
12
 
13
 
37
     //   print(event);
38
     //   print(event);
38
     // });
39
     // });
39
     if (!await FlutterBluePlus.isSupported) {
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
         try {
47
         try {
47
           await FlutterBluePlus.turnOn();
48
           await FlutterBluePlus.turnOn();
48
         } on FlutterBluePlusException catch (e) {
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
       } else {
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
         return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
76
         return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
76
       } else {
77
       } else {
77
         // throw Exception("Permissions not granted, please allow permission to discover bluetooth printers.");
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
     } else {
82
     } else {
91
 
92
 
92
     } on PlatformException catch (e) {
93
     } on PlatformException catch (e) {
93
       inspect(e);
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
     } catch (e) {
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
   @override
102
   @override
102
   Future<bool> isConnected() async {
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
   @override
115
   @override
109
   Future<void> closeConnection() async {
116
   Future<void> closeConnection() async {
110
     try {
117
     try {
111
       await methodChannel.invokeMethod('bluetoothCloseConnection');
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
     } catch (e) {
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
   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(String zplData) {
48
+    throw UnimplementedError('printZplOverBluetooth() has not been implemented.');
49
+  }
46
 }
50
 }

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

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