|
|
@@ -62,12 +62,11 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
62
|
62
|
|
|
63
|
63
|
OnDiscoveryDeviceStreamHandler onDiscoveryDeviceStreamHandler;
|
|
64
|
64
|
private EventChannel onReceiveDeviceChannel;
|
|
65
|
|
- public EventChannel.EventSink onReceiveSink;
|
|
66
|
65
|
|
|
67
|
|
- private BlueDeviceInfo itemPosition;
|
|
68
|
|
-
|
|
69
|
|
-// private MainActivity _mainActivity;
|
|
|
66
|
+ OnDeviceStateStreamHandler onDeviceStateStreamHandler;
|
|
|
67
|
+ private EventChannel onDeviceStateChannel;
|
|
70
|
68
|
|
|
|
69
|
+ private BlueDeviceInfo itemPosition;
|
|
71
|
70
|
|
|
72
|
71
|
Context _applicationContext;
|
|
73
|
72
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
@@ -104,8 +103,6 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
104
|
103
|
@Override
|
|
105
|
104
|
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
|
|
106
|
105
|
setupChannels(flutterPluginBinding.getBinaryMessenger(), flutterPluginBinding.getApplicationContext());
|
|
107
|
|
-
|
|
108
|
|
-
|
|
109
|
106
|
}
|
|
110
|
107
|
|
|
111
|
108
|
private void setupChannels(BinaryMessenger binaryMessenger, Context applicationContext) {
|
|
|
@@ -118,6 +115,10 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
118
|
115
|
onReceiveDeviceChannel = new EventChannel(binaryMessenger, methodChannelName + "/onDiscoveryDevice");
|
|
119
|
116
|
onDiscoveryDeviceStreamHandler = new OnDiscoveryDeviceStreamHandler();
|
|
120
|
117
|
onReceiveDeviceChannel.setStreamHandler(onDiscoveryDeviceStreamHandler);
|
|
|
118
|
+
|
|
|
119
|
+ onDeviceStateChannel = new EventChannel(binaryMessenger, methodChannelName + "/onDeviceState");
|
|
|
120
|
+ onDeviceStateStreamHandler = new OnDeviceStateStreamHandler();
|
|
|
121
|
+ onDeviceStateChannel.setStreamHandler(onDeviceStateStreamHandler);
|
|
121
|
122
|
}
|
|
122
|
123
|
|
|
123
|
124
|
@Override
|
|
|
@@ -149,6 +150,11 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
149
|
150
|
Log.d("ble", "startBluetoothConnect :"+methodCall.arguments);
|
|
150
|
151
|
startBluetoothConnect(methodCall, result);
|
|
151
|
152
|
break;
|
|
|
153
|
+ case "endBluetoothConnect":
|
|
|
154
|
+ Log.d("ble", "endBluetoothConnect :"+methodCall.arguments);
|
|
|
155
|
+ endBluetoothConnect(methodCall, result);
|
|
|
156
|
+ break;
|
|
|
157
|
+
|
|
152
|
158
|
|
|
153
|
159
|
|
|
154
|
160
|
case "autoLogin":
|
|
|
@@ -220,18 +226,17 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
220
|
226
|
if (mBluetoothAdapter.isDiscovering()) {
|
|
221
|
227
|
mBluetoothAdapter.cancelDiscovery();
|
|
222
|
228
|
}
|
|
223
|
|
- Log.d("ble", "startBluetoothPair");
|
|
224
|
|
- String deviceMac = methodCall.arguments.toString();
|
|
225
|
|
- String[] deviceInfoList = deviceMac.split(delimiterStr);
|
|
|
229
|
+
|
|
|
230
|
+ String deviceInfo = methodCall.arguments.toString();
|
|
|
231
|
+ String[] deviceInfoList = deviceInfo.split(delimiterStr);
|
|
|
232
|
+ for(int i =0; i < deviceInfoList.length ; i++) {
|
|
|
233
|
+ Log.d("ble", i+":"+deviceInfoList[i]);
|
|
|
234
|
+ }
|
|
|
235
|
+ Log.d("ble", "startBluetoothPair : ---"+deviceInfo + "----0:"+deviceInfoList[0]);
|
|
226
|
236
|
itemPosition = new BlueDeviceInfo(deviceInfoList[0], deviceInfoList[1], Integer.parseInt(deviceInfoList[2]));
|
|
227
|
237
|
BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(itemPosition.getDeviceHardwareAddress());
|
|
228
|
238
|
|
|
229
|
239
|
executorService.submit(() -> {
|
|
230
|
|
-// requireActivity().runOnUiThread(() -> {
|
|
231
|
|
-// bind.spinKit.setVisibility(View.GONE);
|
|
232
|
|
-// fragment = new MyDialogLoadingFragment("配对中");
|
|
233
|
|
-// fragment.show(requireActivity().getSupportFragmentManager(), "pairing");
|
|
234
|
|
-// });
|
|
235
|
240
|
Log.d("ble", "配对: 开始");
|
|
236
|
241
|
boolean returnValue = false;
|
|
237
|
242
|
try {
|
|
|
@@ -262,26 +267,21 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
262
|
267
|
BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(itemPosition.getDeviceHardwareAddress());
|
|
263
|
268
|
|
|
264
|
269
|
executorService.submit(() -> {
|
|
265
|
|
-// requireActivity().runOnUiThread(() -> {
|
|
266
|
|
-// bind.spinKit.setVisibility(View.GONE);
|
|
267
|
|
-// fragment = new MyDialogLoadingFragment("连接中");
|
|
268
|
|
-// fragment.show(requireActivity().getSupportFragmentManager(), "CONNECT");
|
|
269
|
|
-// });
|
|
270
|
|
-
|
|
271
|
270
|
BlueDeviceInfo blueDeviceInfo = new BlueDeviceInfo(bluetoothDevice.getName(), bluetoothDevice.getAddress(), itemPosition.getConnectState());
|
|
272
|
271
|
PrintUtil.setConnectedType(-1);
|
|
273
|
272
|
int connectResult = PrintUtil.connectBluetoothPrinter(blueDeviceInfo.getDeviceHardwareAddress());
|
|
274
|
273
|
Log.d("ble", "测试:连接结果 " + connectResult);
|
|
275
|
274
|
result.success(connectResult);
|
|
276
|
|
-
|
|
277
|
275
|
});
|
|
278
|
276
|
}
|
|
279
|
277
|
|
|
280
|
|
-
|
|
|
278
|
+ void endBluetoothConnect(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
|
|
|
279
|
+ PrintUtil.close();
|
|
|
280
|
+ result.success(0);
|
|
|
281
|
+ }
|
|
281
|
282
|
|
|
282
|
283
|
private final BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
283
|
284
|
|
|
284
|
|
-
|
|
285
|
285
|
@SuppressLint("MissingPermission")
|
|
286
|
286
|
@Override
|
|
287
|
287
|
public void onReceive(Context context, Intent intent) {
|
|
|
@@ -314,20 +314,25 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
314
|
314
|
if (supportBluetoothType && supportPrintName) {
|
|
315
|
315
|
|
|
316
|
316
|
Log.d("ble", "测试:打印机名称- " + deviceName + ",设备地址:" + deviceHardwareAddress + ",设备类型:" + device.getType() + ", 设备状态:"+deviceStatus);
|
|
317
|
|
- Log.d("ble", "sink:" + onReceiveSink + ":" + "onDiscoveryDeviceStreamHandler" + (onDiscoveryDeviceStreamHandler != null) + "onDiscoveryDeviceStreamHandler.sink != null:" + (onDiscoveryDeviceStreamHandler.sink));
|
|
|
317
|
+ Log.d("ble", ":" + "onDiscoveryDeviceStreamHandler" + (onDiscoveryDeviceStreamHandler != null) + "onDiscoveryDeviceStreamHandler.sink != null:" + (onDiscoveryDeviceStreamHandler.sink));
|
|
318
|
318
|
if (onDiscoveryDeviceStreamHandler != null && onDiscoveryDeviceStreamHandler.sink != null) {
|
|
319
|
319
|
onDiscoveryDeviceStreamHandler.sink.success(deviceName+delimiterStr+deviceHardwareAddress+delimiterStr+deviceStatus);
|
|
320
|
320
|
}
|
|
321
|
321
|
}
|
|
322
|
|
-
|
|
323
|
322
|
}
|
|
324
|
|
-
|
|
325
|
323
|
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
|
|
326
|
324
|
Log.v("ble", "测试:开始搜索");
|
|
327
|
|
-// bind.spinKit.setVisibility(View.VISIBLE);
|
|
|
325
|
+ Log.d("ble", ":" + "onDeviceStateStreamHandler" + (onDeviceStateStreamHandler != null) + "onDeviceStateStreamHandler.sink != null:" + (onDeviceStateStreamHandler.sink));
|
|
|
326
|
+ if (onDeviceStateStreamHandler != null && onDeviceStateStreamHandler.sink != null) {
|
|
|
327
|
+ onDeviceStateStreamHandler.sink.success("scanStart");
|
|
|
328
|
+ }
|
|
328
|
329
|
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
|
|
329
|
330
|
Log.v("ble", "测试:搜索结束");
|
|
330
|
331
|
// bind.spinKit.setVisibility(View.GONE);
|
|
|
332
|
+ Log.d("ble", ":" + "onDeviceStateStreamHandler" + (onDeviceStateStreamHandler != null) + "onDeviceStateStreamHandler.sink != null:" + (onDeviceStateStreamHandler.sink));
|
|
|
333
|
+ if (onDeviceStateStreamHandler != null && onDeviceStateStreamHandler.sink != null) {
|
|
|
334
|
+ onDeviceStateStreamHandler.sink.success("scanEnd");
|
|
|
335
|
+ }
|
|
331
|
336
|
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
|
|
332
|
337
|
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
|
|
333
|
338
|
Log.d("ble", "测试:配对状态改变:0 " + state);
|
|
|
@@ -338,9 +343,10 @@ public class BluetoothPlugin implements FlutterPlugin, MethodChannel.MethodCallH
|
|
338
|
343
|
}
|
|
339
|
344
|
}
|
|
340
|
345
|
} else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
|
|
341
|
|
-// if (fragment != null) {
|
|
342
|
|
-// fragment.dismiss();
|
|
343
|
|
-// }
|
|
|
346
|
+
|
|
|
347
|
+ if (onDeviceStateStreamHandler != null && onDeviceStateStreamHandler.sink != null) {
|
|
|
348
|
+ onDeviceStateStreamHandler.sink.success("pairEnd");
|
|
|
349
|
+ }
|
|
344
|
350
|
}
|
|
345
|
351
|
}
|
|
346
|
352
|
};
|
|
|
@@ -365,4 +371,22 @@ class OnDiscoveryDeviceStreamHandler implements EventChannel.StreamHandler {
|
|
365
|
371
|
Log.e("zhousuhua","OnReceiveDataStreamHandler=== onCancel");
|
|
366
|
372
|
sink = null;
|
|
367
|
373
|
}
|
|
|
374
|
+}
|
|
|
375
|
+
|
|
|
376
|
+class OnDeviceStateStreamHandler implements EventChannel.StreamHandler {
|
|
|
377
|
+
|
|
|
378
|
+ public EventChannel.EventSink sink;
|
|
|
379
|
+
|
|
|
380
|
+ @Override
|
|
|
381
|
+ public void onListen(Object o, EventChannel.EventSink eventSink) {
|
|
|
382
|
+
|
|
|
383
|
+ Log.d("ble", "OnDeviceStateStreamHandler: onListen");
|
|
|
384
|
+ sink = eventSink;
|
|
|
385
|
+ }
|
|
|
386
|
+ @Override
|
|
|
387
|
+ public void onCancel(Object o) {
|
|
|
388
|
+
|
|
|
389
|
+ Log.e("zhousuhua","OnReceiveDataStreamHandler=== onCancel");
|
|
|
390
|
+ sink = null;
|
|
|
391
|
+ }
|
|
368
|
392
|
}
|