Browse Source

feat: close connection

Raihan Rizal 6 months ago
parent
commit
e1f339d0da

+ 1 - 0
android/src/main/java/id/kalanusa/flutter_zsdk/FlutterZsdkPlugin.java

@@ -95,6 +95,7 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
95 95
     switch(call.method) {
96 96
       case "bluetoothOpenConnection":
97 97
       case "isConnected":
98
+      case "bluetoothCloseConnection":
98 99
         if (bluetoothConnectionHandler == null) {
99 100
           bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
100 101
         }

+ 19 - 0
android/src/main/java/id/kalanusa/flutter_zsdk/bluetoothconnectionhandler/BluetoothConnectionHandler.java

@@ -34,6 +34,10 @@ public class BluetoothConnectionHandler {
34 34
             case "isConnected":
35 35
                 checkConnection(result);
36 36
                 break;
37
+
38
+            case "bluetoothCloseConnection":
39
+                closeConnection(result);
40
+                break;
37 41
         }
38 42
     }
39 43
 
@@ -68,5 +72,20 @@ public class BluetoothConnectionHandler {
68 72
         }).start();
69 73
     }
70 74
 
75
+    public void closeConnection(@NonNull MethodChannel.Result result) {
76
+        new Thread(new Runnable() {
77
+            @Override
78
+            public void run() {
79
+                try {
80
+                    bluetoothConnectionHolder.connection.close();
81
+                    result.success(null);
82
+                } catch (ConnectionException e) {
83
+                    Log.w("From Native (Android)", "something went wrong when closing connection");
84
+                    result.error("CLOSE_CONNECTION_ERROR", "Something went wrong while closing the connection", e.getMessage());
85
+                }
86
+            }
87
+        }).start();
88
+    }
89
+
71 90
 
72 91
 }

+ 30 - 0
example/lib/main.dart

@@ -26,6 +26,7 @@ class _MyAppState extends State<MyApp> {
26 26
   String? _connectedPrinter;
27 27
   final _flutterZsdkPlugin = FlutterZsdk();
28 28
   String? _selectedBluetoothPrinterMacAddress;
29
+  bool _disconnecting = false;
29 30
 
30 31
   @override
31 32
   void initState() {
@@ -127,6 +128,29 @@ class _MyAppState extends State<MyApp> {
127 128
     setState(() {});
128 129
   }
129 130
 
131
+  Future<void> closeConnection() async {
132
+    print('invoked closeConnection from dart');
133
+    try {
134
+      setState(() {
135
+        _disconnecting = true;
136
+      });
137
+      await _flutterZsdkPlugin.closeConnection(); 
138
+      _connectedPrinter = null;
139
+      _disconnecting = false;
140
+      print('Connection closed successfully from dart');
141
+      
142
+    } on FlutterZsdkException catch (e) {
143
+      inspect(e);
144
+      showSnackBar(e.message);
145
+
146
+    } catch (e) {
147
+      inspect(e);
148
+      showSnackBar('Unexpected error while disconnecting bluetooth printers');
149
+    }
150
+
151
+    setState(() {});
152
+  }
153
+
130 154
   @override
131 155
   Widget build(BuildContext context) {
132 156
     return Scaffold(
@@ -148,6 +172,12 @@ class _MyAppState extends State<MyApp> {
148 172
             ),
149 173
             SizedBox(height: 20),
150 174
 
175
+            ElevatedButton(
176
+              child: Text(_disconnecting ? 'Disconnecting...' : 'Disconnect from $_connectedPrinter'),
177
+              onPressed: _connectedPrinter == null || _disconnecting ? null : closeConnection,
178
+            ),
179
+            SizedBox(height: 20),
180
+
151 181
             ElevatedButton(
152 182
               child: Text('Check connection status'),
153 183
               onPressed: () async {

+ 4 - 0
lib/flutter_zsdk.dart

@@ -20,4 +20,8 @@ class FlutterZsdk {
20 20
   Future<bool> isConnected() {
21 21
     return FlutterZsdkPlatform.instance.isConnected();
22 22
   }
23
+
24
+  Future<void> closeConnection() {
25
+    return FlutterZsdkPlatform.instance.closeConnection();
26
+  }
23 27
 }

+ 9 - 0
lib/src/flutter_zsdk_method_channel.dart

@@ -104,4 +104,13 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
104 104
 
105 105
     return isConnected;
106 106
   }
107
+
108
+  @override
109
+  Future<void> closeConnection() async {
110
+    try {
111
+      await methodChannel.invokeMethod('bluetoothCloseConnection');
112
+    } catch (e) {
113
+      throw FlutterZsdkException("Failed to close connection to printer: $e");
114
+    }
115
+  }
107 116
 }

+ 4 - 0
lib/src/flutter_zsdk_platform_interface.dart

@@ -39,4 +39,8 @@ abstract class FlutterZsdkPlatform extends PlatformInterface {
39 39
   Future<bool> isConnected() {
40 40
     throw UnimplementedError('isConnected() has not been implemented.');
41 41
   }
42
+
43
+  Future<void> closeConnection() {
44
+    throw UnimplementedError('closeConnection() has not been implemented.');
45
+  }
42 46
 }