瀏覽代碼

feat: change bluetooth printer connection

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

+ 38 - 3
android/src/main/java/id/kalanusa/flutter_zsdk/bluetoothconnectionhandler/BluetoothConnectionHandler.java

@@ -19,11 +19,25 @@ public class BluetoothConnectionHandler {
19 19
 
20 20
         switch (call.method) {
21 21
             case "bluetoothOpenConnection":
22
-                if (BluetoothConnectionHolder.connection == null) {
23
-                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(call.argument("macAddress")));
22
+                String macAddressArgument = call.argument("macAddress");
23
+
24
+                if (BluetoothConnectionHolder.connection != null) {
25
+                    String connectionInfo = BluetoothConnectionHolder.connection.getSimpleConnectionName();
26
+                    String currentConnectionMacAddress = connectionInfo.substring(0, 17);
27
+
28
+                    if (currentConnectionMacAddress != macAddressArgument) {
29
+                        closeAndReopenConnection(result, macAddressArgument);
30
+
31
+                    }
32
+                    break;
24 33
                 }
25 34
 
26
-                openConnection(result);
35
+                if (BluetoothConnectionHolder.connection == null) {
36
+                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(macAddressArgument));
37
+
38
+                    openConnection(result);
39
+
40
+                }
27 41
                 break;
28 42
 
29 43
             case "isConnected":
@@ -54,10 +68,31 @@ public class BluetoothConnectionHandler {
54 68
         }).start();
55 69
     }
56 70
 
71
+    public void closeAndReopenConnection(@NonNull MethodChannel.Result result, String newMacAddress) {
72
+        new Thread(new Runnable() {
73
+            @Override
74
+            public void run() {
75
+                try {
76
+                    Looper.prepare();
77
+                    BluetoothConnectionHolder.connection.close();
78
+                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(newMacAddress));
79
+                    BluetoothConnectionHolder.connection.open();
80
+                    Looper.myLooper().quit();
81
+                    result.success(null);
82
+                    Log.w("From Native (Android)", "close and reopen connection finished");
83
+                } catch (ConnectionException e) {
84
+                    Log.w("From Native (Android)", "Connection Exception threw " + e);
85
+                    result.error("CLOSE_AND_REOPEN_CONNECTION_ERROR", "Cant close and connect to new printers", e.getMessage());
86
+                }
87
+            }
88
+        }).start();
89
+    }
90
+
57 91
     public void checkConnection(@NonNull MethodChannel.Result result) {
58 92
         new Thread(new Runnable() {
59 93
             @Override
60 94
             public void run() {
95
+                BluetoothConnectionHolder.getSimpleConnectionName();
61 96
                 boolean isConnected = BluetoothConnectionHolder.connection.isConnected();
62 97
 
63 98
                 result.success(isConnected);

+ 11 - 0
android/src/main/java/id/kalanusa/flutter_zsdk/bluetoothconnectionhandler/BluetoothConnectionHolder.java

@@ -4,6 +4,8 @@ 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 10
     static public Connection connection;
9 11
 
@@ -14,4 +16,13 @@ public class BluetoothConnectionHolder {
14 16
     static public void updateConnection(@NonNull Connection connection) {
15 17
         BluetoothConnectionHolder.connection = connection;
16 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);
27
+    }
17 28
 }

+ 1 - 1
example/lib/main.dart

@@ -186,7 +186,7 @@ class _MyAppState extends State<MyApp> {
186 186
         
187 187
             ElevatedButton(
188 188
               child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
189
-              onPressed: _selectedBluetoothPrinterMacAddress == null || _connectedPrinter != null ? null : openConnection,
189
+              onPressed: _selectedBluetoothPrinterMacAddress == null ? null : openConnection,
190 190
             ),
191 191
             SizedBox(height: 20),
192 192