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
     switch(call.method) {
95
     switch(call.method) {
96
       case "bluetoothOpenConnection":
96
       case "bluetoothOpenConnection":
97
       case "isConnected":
97
       case "isConnected":
98
+      case "bluetoothCloseConnection":
98
         if (bluetoothConnectionHandler == null) {
99
         if (bluetoothConnectionHandler == null) {
99
           bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
100
           bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
100
         }
101
         }

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

34
             case "isConnected":
34
             case "isConnected":
35
                 checkConnection(result);
35
                 checkConnection(result);
36
                 break;
36
                 break;
37
+
38
+            case "bluetoothCloseConnection":
39
+                closeConnection(result);
40
+                break;
37
         }
41
         }
38
     }
42
     }
39
 
43
 
68
         }).start();
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
   String? _connectedPrinter;
26
   String? _connectedPrinter;
27
   final _flutterZsdkPlugin = FlutterZsdk();
27
   final _flutterZsdkPlugin = FlutterZsdk();
28
   String? _selectedBluetoothPrinterMacAddress;
28
   String? _selectedBluetoothPrinterMacAddress;
29
+  bool _disconnecting = false;
29
 
30
 
30
   @override
31
   @override
31
   void initState() {
32
   void initState() {
127
     setState(() {});
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
   @override
154
   @override
131
   Widget build(BuildContext context) {
155
   Widget build(BuildContext context) {
132
     return Scaffold(
156
     return Scaffold(
148
             ),
172
             ),
149
             SizedBox(height: 20),
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
             ElevatedButton(
181
             ElevatedButton(
152
               child: Text('Check connection status'),
182
               child: Text('Check connection status'),
153
               onPressed: () async {
183
               onPressed: () async {

+ 4 - 0
lib/flutter_zsdk.dart

20
   Future<bool> isConnected() {
20
   Future<bool> isConnected() {
21
     return FlutterZsdkPlatform.instance.isConnected();
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
 
104
 
105
     return isConnected;
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
   Future<bool> isConnected() {
39
   Future<bool> isConnected() {
40
     throw UnimplementedError('isConnected() has not been implemented.');
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
 }