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