|
@@ -19,6 +19,7 @@ import java.util.Map;
|
19
|
19
|
import java.util.Objects;
|
20
|
20
|
|
21
|
21
|
import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHandler;
|
|
22
|
+import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
|
22
|
23
|
import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothDiscoveryHandler;
|
23
|
24
|
import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothPrinter;
|
24
|
25
|
import io.flutter.Log;
|
|
@@ -35,7 +36,7 @@ import io.flutter.plugin.common.MethodChannel.Result;
|
35
|
36
|
import io.flutter.plugin.common.PluginRegistry;
|
36
|
37
|
|
37
|
38
|
/** FlutterZsdkPlugin */
|
38
|
|
-public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.RequestPermissionsResultListener {
|
|
39
|
+public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
|
39
|
40
|
/// The MethodChannel that will the communication between Flutter and native Android
|
40
|
41
|
///
|
41
|
42
|
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
|
|
@@ -50,6 +51,11 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
|
50
|
51
|
private DartExecutor dartExecutor;
|
51
|
52
|
private BinaryMessenger binaryMessenger;
|
52
|
53
|
|
|
54
|
+ public BluetoothConnectionHolder bluetoothConnectionHolder;
|
|
55
|
+
|
|
56
|
+ public BluetoothConnectionHandler bluetoothConnectionHandler;
|
|
57
|
+
|
|
58
|
+
|
53
|
59
|
@Override
|
54
|
60
|
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
|
55
|
61
|
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_zsdk");
|
|
@@ -63,7 +69,6 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
|
63
|
69
|
@Override
|
64
|
70
|
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
|
65
|
71
|
activity = binding.getActivity();
|
66
|
|
- binding.addRequestPermissionsResultListener(this);
|
67
|
72
|
|
68
|
73
|
new EventChannel(binaryMessenger, bluetoothDiscoveryEventChannel).setStreamHandler(new BluetoothDiscoveryHandler(context, activity));
|
69
|
74
|
}
|
|
@@ -89,138 +94,19 @@ public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, Acti
|
89
|
94
|
|
90
|
95
|
switch(call.method) {
|
91
|
96
|
case "bluetoothOpenConnection":
|
92
|
|
- new BluetoothConnectionHandler(call.argument("macAddress")).handle(call, result);
|
93
|
|
- }
|
94
|
|
-// switch(call.method) {
|
95
|
|
-// case "findBluetoothPrinters":
|
96
|
|
-// findBluetoothPrinters();
|
97
|
|
-// case "getPlatformVersion":
|
98
|
|
-// Log.w("from native", "TEST");
|
99
|
|
-// case "mengege":
|
100
|
|
-// new BluetoothConnectionHandler(call.argument("macAddress"));
|
101
|
|
-// }
|
102
|
|
- }
|
103
|
|
-
|
104
|
|
- @Override
|
105
|
|
- public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
|
106
|
|
- channel.setMethodCallHandler(null);
|
107
|
|
- }
|
108
|
|
-
|
109
|
|
- @Override
|
110
|
|
- public boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
111
|
|
- if (requestCode == 1001) { // Check if it'sthe request we initiated
|
112
|
|
- if (grantResults.length > 0) {
|
113
|
|
- boolean bluetoothScanGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
114
|
|
- boolean bluetoothConnectGranted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
|
115
|
|
- boolean locationGranted = grantResults[2] == PackageManager.PERMISSION_GRANTED;
|
116
|
|
-
|
117
|
|
- if (bluetoothScanGranted && bluetoothConnectGranted && locationGranted) {
|
118
|
|
- // All permissions granted, proceed with Bluetooth operations
|
119
|
|
- // ... your Bluetooth code here ...
|
120
|
|
- this.startDiscoverBluetoothPrinters();
|
121
|
|
- } else {
|
122
|
|
- System.out.println("Permission Denied, please allow permission to discover bluetooth devices");
|
123
|
|
- // Handle denied permissions
|
124
|
|
- // You might display a messageto the user or disable Bluetooth features
|
125
|
|
- // ... your logic for handling denied permissions ...
|
|
97
|
+ case "isConnected":
|
|
98
|
+ if (bluetoothConnectionHandler == null) {
|
|
99
|
+ bluetoothConnectionHandler = new BluetoothConnectionHandler(bluetoothConnectionHolder);
|
126
|
100
|
}
|
127
|
|
- }
|
128
|
|
- }
|
129
|
|
-
|
130
|
|
- return true;
|
131
|
|
- }
|
132
|
101
|
|
|
102
|
+ bluetoothConnectionHandler.handle(call, result);
|
|
103
|
+ break;
|
133
|
104
|
|
134
|
|
-// Bluetooth discovery
|
135
|
|
- public void findBluetoothPrinters() {
|
136
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
137
|
|
- if (context.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||
|
138
|
|
- context.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED ||
|
139
|
|
- context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
140
|
|
-
|
141
|
|
- activity.requestPermissions(new String[]{
|
142
|
|
- Manifest.permission.BLUETOOTH_SCAN,
|
143
|
|
- Manifest.permission.BLUETOOTH_CONNECT,
|
144
|
|
- Manifest.permission.ACCESS_FINE_LOCATION
|
145
|
|
- }, 1001);
|
146
|
|
- } else {
|
147
|
|
- this.startDiscoverBluetoothPrinters();
|
148
|
|
- }
|
149
|
|
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
150
|
|
- if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
151
|
|
- activity.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1002);
|
152
|
|
- } else {
|
153
|
|
- this.startDiscoverBluetoothPrinters();
|
154
|
|
- }
|
155
|
105
|
}
|
156
|
106
|
}
|
157
|
107
|
|
158
|
|
- public void startDiscoverBluetoothPrinters() {
|
159
|
|
- new EventChannel(dartExecutor, bluetoothDiscoveryEventChannel).setStreamHandler(
|
160
|
|
- new EventChannel.StreamHandler() {
|
161
|
|
-
|
162
|
|
- @Override
|
163
|
|
- public void onListen(Object arguments, EventChannel.EventSink events) {
|
164
|
|
- Log.w("From native", "Adding Listener");
|
165
|
|
- attachEvent = events;
|
166
|
|
- handler = new Handler();
|
167
|
|
- startDiscoverBluetoothPrintersRunnable.run();
|
168
|
|
- }
|
169
|
|
-
|
170
|
|
- @Override
|
171
|
|
- public void onCancel(Object arguments) {
|
172
|
|
- Log.w("From native", "Cancelling Listener");
|
173
|
|
- handler.removeCallbacks(startDiscoverBluetoothPrintersRunnable);
|
174
|
|
- handler = null;
|
175
|
|
- attachEvent = null;
|
176
|
|
- System.out.println("StreamHandler - onCanceled: "); }
|
177
|
|
- }
|
178
|
|
- );
|
|
108
|
+ @Override
|
|
109
|
+ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
|
|
110
|
+ channel.setMethodCallHandler(null);
|
179
|
111
|
}
|
180
|
|
-
|
181
|
|
-// public void startDiscoverBluetoothPrintersRunnable() {
|
182
|
|
-// new Thread(new Runnable() {
|
183
|
|
-// @Override
|
184
|
|
-// public void run() {
|
185
|
|
-//
|
186
|
|
-// });
|
187
|
|
-// }
|
188
|
|
-//
|
189
|
|
- private final Runnable startDiscoverBluetoothPrintersRunnable = new Runnable() {
|
190
|
|
- @Override
|
191
|
|
- public void run() {
|
192
|
|
- ArrayList<BluetoothPrinter> tempDiscoveredPrinterList = new ArrayList<BluetoothPrinter>();
|
193
|
|
- DiscoveryHandler discoveryHandler = new DiscoveryHandler() {
|
194
|
|
- @Override
|
195
|
|
- public void foundPrinter(DiscoveredPrinter discoveredPrinter) {
|
196
|
|
- Map<String, String> discoveredPrinterDataMap = discoveredPrinter.getDiscoveryDataMap();
|
197
|
|
- System.out.println("Found printer " + discoveredPrinterDataMap.get("FRIENDLY_NAME"));
|
198
|
|
-
|
199
|
|
- BluetoothPrinter bluetoothPrinter = new BluetoothPrinter(discoveredPrinterDataMap.get("FRIENDLY_NAME"), discoveredPrinterDataMap.get("MAC_ADDRESS"));
|
200
|
|
- tempDiscoveredPrinterList.add(bluetoothPrinter);
|
201
|
|
- attachEvent.success(tempDiscoveredPrinterList);
|
202
|
|
- }
|
203
|
|
-
|
204
|
|
- @Override
|
205
|
|
- public void discoveryFinished() {
|
206
|
|
- // return value here, this is asynchronous
|
207
|
|
- Log.w("From Native", "Discovery Success, found "+ tempDiscoveredPrinterList.size() + " Printer(s)");
|
208
|
|
- attachEvent.endOfStream();
|
209
|
|
- }
|
210
|
|
-
|
211
|
|
- @Override
|
212
|
|
- public void discoveryError(String s) {
|
213
|
|
- System.out.println("Error when discovering bluetooth device");
|
214
|
|
- System.out.println("message:" + s);
|
215
|
|
- }
|
216
|
|
- };
|
217
|
|
-
|
218
|
|
- try {
|
219
|
|
- BluetoothDiscoverer.findPrinters(context, discoveryHandler);
|
220
|
|
- } catch (ConnectionException e) {
|
221
|
|
- System.out.println("Error when finding printers");
|
222
|
|
-// throw new RuntimeException(e);
|
223
|
|
- }
|
224
|
|
- }
|
225
|
|
- };
|
226
|
112
|
}
|