|
@@ -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);
|