Преглед изворни кода

feat: change bluetooth printer connection

Raihan Rizal пре 5 месеци
родитељ
комит
a153758db1

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

19
 
19
 
20
         switch (call.method) {
20
         switch (call.method) {
21
             case "bluetoothOpenConnection":
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
                 break;
41
                 break;
28
 
42
 
29
             case "isConnected":
43
             case "isConnected":
54
         }).start();
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
     public void checkConnection(@NonNull MethodChannel.Result result) {
91
     public void checkConnection(@NonNull MethodChannel.Result result) {
58
         new Thread(new Runnable() {
92
         new Thread(new Runnable() {
59
             @Override
93
             @Override
60
             public void run() {
94
             public void run() {
95
+                BluetoothConnectionHolder.getSimpleConnectionName();
61
                 boolean isConnected = BluetoothConnectionHolder.connection.isConnected();
96
                 boolean isConnected = BluetoothConnectionHolder.connection.isConnected();
62
 
97
 
63
                 result.success(isConnected);
98
                 result.success(isConnected);

+ 11 - 0
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
     static public Connection connection;
10
     static public Connection connection;
9
 
11
 
14
     static public void updateConnection(@NonNull Connection connection) {
16
     static public void updateConnection(@NonNull Connection connection) {
15
         BluetoothConnectionHolder.connection = connection;
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
         
186
         
187
             ElevatedButton(
187
             ElevatedButton(
188
               child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
188
               child: Text('Connect to $_selectedBluetoothPrinterMacAddress'),
189
-              onPressed: _selectedBluetoothPrinterMacAddress == null || _connectedPrinter != null ? null : openConnection,
189
+              onPressed: _selectedBluetoothPrinterMacAddress == null ? null : openConnection,
190
             ),
190
             ),
191
             SizedBox(height: 20),
191
             SizedBox(height: 20),
192
 
192