-
Notifications
You must be signed in to change notification settings - Fork 437
/
FlutterBluetoothSerialPlugin.java
1159 lines (1010 loc) · 52.5 KB
/
FlutterBluetoothSerialPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.github.edufolly.flutterbluetoothserial;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.util.SparseArray;
import android.os.AsyncTask;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.NetworkInterface;
import io.flutter.view.FlutterNativeView;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry.ViewDestroyListener;
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener;
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener;
public class FlutterBluetoothSerialPlugin implements MethodCallHandler, RequestPermissionsResultListener, ActivityResultListener, ViewDestroyListener {
// Plugin
private static final String TAG = "FlutterBluePlugin";
private static final String PLUGIN_NAMESPACE = "flutter_bluetooth_serial";
private final Registrar registrar;
private final MethodChannel methodChannel;
private Result pendingResultForActivityResult = null;
// Permissions and request constants
private static final int REQUEST_COARSE_LOCATION_PERMISSIONS = 1451;
private static final int REQUEST_ENABLE_BLUETOOTH = 1337;
private static final int REQUEST_DISCOVERABLE_BLUETOOTH = 2137;
// General Bluetooth
private BluetoothAdapter bluetoothAdapter;
private BluetoothManager bluetoothManager;
// State
private final BroadcastReceiver stateReceiver;
private EventSink stateSink;
// Pairing requests
private final BroadcastReceiver pairingRequestReceiver;
private boolean isPairingRequestHandlerSet = false;
private BroadcastReceiver bondStateBroadcastReceiver = null;
// Discovery
private EventChannel discoveryChannel;
private EventSink discoverySink;
private StreamHandler discoveryStreamHandler;
private BroadcastReceiver discoveryReceiver;
// Connections
/// Contains all active connections. Maps ID of the connection with plugin data channels.
private SparseArray<BluetoothConnectionWrapper> connections = new SparseArray<>(2);
/// Last ID given to any connection, used to avoid duplicate IDs
private int lastConnectionId = 0;
/// Registers plugin in Flutter plugin system
public static void registerWith(Registrar registrar) {
final FlutterBluetoothSerialPlugin instance = new FlutterBluetoothSerialPlugin(registrar);
registrar.addRequestPermissionsResultListener(instance);
registrar.addActivityResultListener(instance);
registrar.addViewDestroyListener(instance);
}
/// Constructs the plugin instance
FlutterBluetoothSerialPlugin(Registrar registrar) {
// Plugin
{
this.registrar = registrar;
methodChannel = new MethodChannel(registrar.messenger(), PLUGIN_NAMESPACE + "/methods");
methodChannel.setMethodCallHandler(this);
}
// General Bluetooth
{
this.bluetoothManager = (BluetoothManager) registrar.activity().getSystemService(Context.BLUETOOTH_SERVICE);
assert this.bluetoothManager != null;
this.bluetoothAdapter = bluetoothManager.getAdapter();
}
// State
{
stateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (stateSink == null) {
return;
}
final String action = intent.getAction();
switch (action) {
case BluetoothAdapter.ACTION_STATE_CHANGED:
// Disconnect all connections
int size = connections.size();
for (int i = 0; i < size; i++) {
BluetoothConnection connection = connections.valueAt(i);
connection.disconnect();
}
connections.clear();
stateSink.success(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR));
break;
}
}
};
EventChannel stateChannel = new EventChannel(registrar.messenger(), PLUGIN_NAMESPACE + "/state");
stateChannel.setStreamHandler(new StreamHandler() {
@Override
public void onListen(Object o, EventSink eventSink) {
stateSink = eventSink;
registrar.activeContext().registerReceiver(stateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
@Override
public void onCancel(Object o) {
stateSink = null;
try {
registrar.activeContext().unregisterReceiver(stateReceiver);
}
catch (IllegalArgumentException ex) {
// Ignore `Receiver not registered` exception
}
}
});
}
// Pairing requests
{
pairingRequestReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case BluetoothDevice.ACTION_PAIRING_REQUEST:
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
Log.d(TAG, "Pairing request (variant " + pairingVariant + ") incoming from " + device.getAddress());
switch (pairingVariant) {
case BluetoothDevice.PAIRING_VARIANT_PIN:
// Simplest method - 4 digit number
{
final BroadcastReceiver.PendingResult broadcastResult = this.goAsync();
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("address", device.getAddress());
arguments.put("variant", pairingVariant);
methodChannel.invokeMethod("handlePairingRequest", arguments, new MethodChannel.Result() {
@Override
public void success(Object handlerResult) {
Log.d(TAG, handlerResult.toString());
if (handlerResult instanceof String) {
try {
final String passkeyString = (String) handlerResult;
final byte[] passkey = passkeyString.getBytes();
Log.d(TAG, "Trying to set passkey for pairing to " + passkeyString);
device.setPin(passkey);
broadcastResult.abortBroadcast();
}
catch (Exception ex) {
Log.e(TAG, ex.getMessage());
ex.printStackTrace();
// @TODO , passing the error
//result.error("bond_error", "Setting passkey for pairing failed", exceptionToString(ex));
}
}
else {
Log.d(TAG, "Manual pin pairing in progress");
//Intent intent = new Intent(BluetoothAdapter.ACTION_PAIRING_REQUEST);
//intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
//intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, pairingVariant)
ActivityCompat.startActivity(registrar.activity(), intent, null);
}
broadcastResult.finish();
}
@Override
public void notImplemented() {
throw new UnsupportedOperationException();
}
@Override
public void error(String code, String message, Object details) {
throw new UnsupportedOperationException();
}
});
break;
}
// Note: `BluetoothDevice.PAIRING_VARIANT_PASSKEY` seems to be unsupported anyway... Probably is abandoned.
// See https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java#1528
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
// Displayed passkey on the other device should be the same as received here.
case 3: //case BluetoothDevice.PAIRING_VARIANT_CONSENT: // @TODO , Symbol not found?
// The simplest, but much less secure method - just yes or no, without any auth.
// Consent type can use same code as passkey confirmation since passed passkey,
// which is 0 or error at the moment, should not be used anyway by common code.
{
final int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("address", device.getAddress());
arguments.put("variant", pairingVariant);
arguments.put("pairingKey", pairingKey);
final BroadcastReceiver.PendingResult broadcastResult = this.goAsync();
methodChannel.invokeMethod("handlePairingRequest", arguments, new MethodChannel.Result() {
@Override
public void success(Object handlerResult) {
if (handlerResult instanceof Boolean) {
try {
final boolean confirm = (Boolean) handlerResult;
Log.d(TAG, "Trying to set pairing confirmation to " + confirm + " (key: " + pairingKey + ")");
// @WARN `BLUETOOTH_PRIVILEGED` permission required, but might be
// unavailable for thrid party apps on newer versions of Androids.
device.setPairingConfirmation(confirm);
broadcastResult.abortBroadcast();
}
catch (Exception ex) {
Log.e(TAG, ex.getMessage());
ex.printStackTrace();
// @TODO , passing the error
//result.error("bond_error", "Auto-confirming pass key failed", exceptionToString(ex));
}
}
else {
Log.d(TAG, "Manual passkey confirmation pairing in progress (key: " + pairingKey + ")");
ActivityCompat.startActivity(registrar.activity(), intent, null);
}
broadcastResult.finish();
}
@Override
public void notImplemented() {
throw new UnsupportedOperationException();
}
@Override
public void error(String code, String message, Object details) {
Log.e(TAG, code + " " + message);
throw new UnsupportedOperationException();
}
});
break;
}
case 4: //case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: // @TODO , Symbol not found?
// This pairing method requires to enter the generated and displayed pairing key
// on the remote device. It looks like basic asymmetric cryptography was used.
case 5: //case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: // @TODO , Symbol not found?
// Same as previous, but for 4 digit pin.
{
final int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("address", device.getAddress());
arguments.put("variant", pairingVariant);
arguments.put("pairingKey", pairingKey);
methodChannel.invokeMethod("handlePairingRequest", arguments);
break;
}
// Note: `BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT` seems to be unsupported for now, at least at master branch of Android.
// See https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java#1559
// Note: `BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS ` seems to be unsupported for now, at least at master branch of Android.
// See https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java#1559
default:
// Only log other pairing variants
Log.w(TAG, "Unknown pairing variant: " + pairingVariant);
break;
}
break;
default:
// Ignore other actions
break;
}
}
};
}
// Discovery
{
discoveryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch (action) {
case BluetoothDevice.ACTION_FOUND:
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//final BluetoothClass deviceClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS); // @TODO . !BluetoothClass!
//final String extraName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); // @TODO ? !EXTRA_NAME!
final int deviceRSSI = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
Map<String, Object> discoveryResult = new HashMap<>();
discoveryResult.put("address", device.getAddress());
discoveryResult.put("name", device.getName());
discoveryResult.put("type", device.getType());
//discoveryResult.put("class", deviceClass); // @TODO . it isn't my priority for now !BluetoothClass!
discoveryResult.put("isConnected", checkIsDeviceConnected(device));
discoveryResult.put("bondState", device.getBondState());
discoveryResult.put("rssi", deviceRSSI);
Log.d(TAG, "Discovered " + device.getAddress());
if (discoverySink != null) {
discoverySink.success(discoveryResult);
}
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Log.d(TAG, "Discovery finished");
try {
context.unregisterReceiver(discoveryReceiver);
}
catch (IllegalArgumentException ex) {
// Ignore `Receiver not registered` exception
}
bluetoothAdapter.cancelDiscovery();
if (discoverySink != null) {
discoverySink.endOfStream();
discoverySink = null;
}
break;
default:
// Ignore.
break;
}
}
};
discoveryChannel = new EventChannel(registrar.messenger(), PLUGIN_NAMESPACE + "/discovery");
discoveryStreamHandler = new StreamHandler() {
@Override
public void onListen(Object o, EventSink eventSink) {
discoverySink = eventSink;
}
@Override
public void onCancel(Object o) {
Log.d(TAG, "Canceling discovery (stream closed)");
try {
registrar.activeContext().unregisterReceiver(discoveryReceiver);
}
catch (IllegalArgumentException ex) {
// Ignore `Receiver not registered` exception
}
bluetoothAdapter.cancelDiscovery();
if (discoverySink != null) {
discoverySink.endOfStream();
discoverySink = null;
}
}
};
discoveryChannel.setStreamHandler(discoveryStreamHandler);
}
}
/// Provides access to the plugin methods
@Override
public void onMethodCall(MethodCall call, Result result) {
if (bluetoothAdapter == null) {
if ("isAvailable".equals(call.method)) {
result.success(false);
return;
}
else {
result.error("bluetooth_unavailable", "bluetooth is not available", null);
return;
}
}
methodCallDispatching: switch (call.method) {
////////////////////////////////////////
/* Adapter settings and general */
case "isAvailable":
result.success(true);
break;
case "isOn":
case "isEnabled":
result.success(bluetoothAdapter.isEnabled());
break;
case "openSettings":
ContextCompat.startActivity(registrar.activity(), new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS), null);
result.success(null);
break;
case "requestEnable":
if (!bluetoothAdapter.isEnabled()) {
pendingResultForActivityResult = result;
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
ActivityCompat.startActivityForResult(registrar.activity(), intent, REQUEST_ENABLE_BLUETOOTH, null);
}
else {
result.success(true);
}
break;
case "requestDisable":
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
result.success(true);
}
else {
result.success(false);
}
break;
case "ensurePermissions":
ensurePermissions(new EnsurePermissionsCallback() {
@Override
public void onResult(boolean granted) {
result.success(granted);
}
});
break;
case "getState":
result.success(bluetoothAdapter.getState());
break;
case "getAddress": {
String address = bluetoothAdapter.getAddress();
if (address.equals("02:00:00:00:00:00")) {
Log.w(TAG, "Local Bluetooth MAC address is hidden by system, trying other options...");
do {
Log.d(TAG, "Trying to obtain address using Settings Secure bank");
try {
// Requires `LOCAL_MAC_ADDRESS` which could be unavailible for third party applications...
String value = android.provider.Settings.Secure.getString(registrar.activeContext().getContentResolver(), "bluetooth_address");
if (value == null) {
throw new NullPointerException("null returned, might be no permissions problem");
}
address = value;
break;
}
catch (Exception ex) {
// Ignoring failure (since it isn't critical API for most applications)
Log.d(TAG, "Obtaining address using Settings Secure bank failed");
//result.error("hidden_address", "obtaining address using Settings Secure bank failed", exceptionToString(ex));
}
Log.d(TAG, "Trying to obtain address using reflection against internal Android code");
try {
// This will most likely work, but well, it is unsafe
java.lang.reflect.Field mServiceField;
mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
mServiceField.setAccessible(true);
Object bluetoothManagerService = mServiceField.get(bluetoothAdapter);
if (bluetoothManagerService == null) {
if (!bluetoothAdapter.isEnabled()) {
Log.d(TAG, "Probably failed just because adapter is disabled!");
}
throw new NullPointerException();
}
java.lang.reflect.Method getAddressMethod;
getAddressMethod = bluetoothManagerService.getClass().getMethod("getAddress");
String value = (String) getAddressMethod.invoke(bluetoothManagerService);
if (value == null) {
throw new NullPointerException();
}
address = value;
Log.d(TAG, "Probably succed: " + address + " ✨ :F");
break;
}
catch (Exception ex) {
// Ignoring failure (since it isn't critical API for most applications)
Log.d(TAG, "Obtaining address using reflection against internal Android code failed");
//result.error("hidden_address", "obtaining address using reflection agains internal Android code failed", exceptionToString(ex));
}
Log.d(TAG, "Trying to look up address by network interfaces - might be invalid on some devices");
try {
// This method might return invalid MAC address (since Bluetooth might use other address than WiFi).
// @TODO . further testing: 1) check is while open connection, 2) check other devices
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
String value = null;
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
String name = networkInterface.getName();
if (!name.equalsIgnoreCase("wlan0")) {
continue;
}
byte[] addressBytes = networkInterface.getHardwareAddress();
if (addressBytes != null) {
StringBuilder addressBuilder = new StringBuilder(18);
for (byte b : addressBytes) {
addressBuilder.append(String.format("%02X:", b));
}
addressBuilder.setLength(17);
value = addressBuilder.toString();
// Log.v(TAG, "-> '" + name + "' : " + value);
// }
// else {
// Log.v(TAG, "-> '" + name + "' : <no hardware address>");
}
}
if (value == null) {
throw new NullPointerException();
}
address = value;
}
catch (Exception ex) {
// Ignoring failure (since it isn't critical API for most applications)
Log.w(TAG, "Looking for address by network interfaces failed");
//result.error("hidden_address", "looking for address by network interfaces failed", exceptionToString(ex));
}
}
while (false);
}
result.success(address);
break;
}
case "getName":
result.success(bluetoothAdapter.getName());
break;
case "setName": {
if (!call.hasArgument("name")) {
result.error("invalid_argument", "argument 'name' not found", null);
break;
}
String name;
try {
name = call.argument("name");
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'name' argument is required to be string", null);
break;
}
result.success(bluetoothAdapter.setName(name));
break;
}
////////////////////////////////////////////////////////////////////////////////
/* Discovering and bonding devices */
case "getDeviceBondState": {
if (!call.hasArgument("address")) {
result.error("invalid_argument", "argument 'address' not found", null);
break;
}
String address;
try {
address = call.argument("address");
if (!BluetoothAdapter.checkBluetoothAddress(address)) {
throw new ClassCastException();
}
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'address' argument is required to be string containing remote MAC address", null);
break;
}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
result.success(device.getBondState());
break;
}
case "removeDeviceBond": {
if (!call.hasArgument("address")) {
result.error("invalid_argument", "argument 'address' not found", null);
break;
}
String address;
try {
address = call.argument("address");
if (!BluetoothAdapter.checkBluetoothAddress(address)) {
throw new ClassCastException();
}
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'address' argument is required to be string containing remote MAC address", null);
break;
}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
result.error("bond_error", "device already bonding", null);
break methodCallDispatching;
case BluetoothDevice.BOND_NONE:
result.error("bond_error", "device already unbonded", null);
break methodCallDispatching;
default:
// Proceed.
break;
}
try {
java.lang.reflect.Method method;
method = device.getClass().getMethod("removeBond");
boolean value = (Boolean) method.invoke(device);
result.success(value);
}
catch (Exception ex) {
result.error("bond_error", "error while unbonding", exceptionToString(ex));
}
break;
}
case "bondDevice": {
if (!call.hasArgument("address")) {
result.error("invalid_argument", "argument 'address' not found", null);
break;
}
String address;
try {
address = call.argument("address");
if (!BluetoothAdapter.checkBluetoothAddress(address)) {
throw new ClassCastException();
}
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'address' argument is required to be string containing remote MAC address", null);
break;
}
if (bondStateBroadcastReceiver != null) {
result.error("bond_error", "another bonding process is ongoing from local device", null);
break;
}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
result.error("bond_error", "device already bonding", null);
break methodCallDispatching;
case BluetoothDevice.BOND_BONDED:
result.error("bond_error", "device already bonded", null);
break methodCallDispatching;
default:
// Proceed.
break;
}
bondStateBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
// @TODO . BluetoothDevice.ACTION_PAIRING_CANCEL
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
final BluetoothDevice someDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (!someDevice.equals(device)) {
break;
}
final int newBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
switch (newBondState) {
case BluetoothDevice.BOND_BONDING:
// Wait for true bond result :F
return;
case BluetoothDevice.BOND_BONDED:
result.success(true);
break;
case BluetoothDevice.BOND_NONE:
result.success(false);
break;
default:
result.error("bond_error", "invalid bond state while bonding", null);
break;
}
registrar.activeContext().unregisterReceiver(this);
bondStateBroadcastReceiver = null;
break;
default:
// Ignore.
break;
}
}
};
final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
//filter.setPriority(pairingRequestReceiverPriority + 1);
registrar.activeContext().registerReceiver(bondStateBroadcastReceiver, filter);
if (!device.createBond()) {
result.error("bond_error", "error starting bonding process", null);
}
break;
}
case "pairingRequestHandlingEnable":
if (this.isPairingRequestHandlerSet) {
result.error("logic_error", "pairing request handling is already enabled", null);
break;
}
Log.d(TAG, "Starting listening for pairing requests to handle");
this.isPairingRequestHandlerSet = true;
final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
//filter.setPriority(pairingRequestReceiverPriority);
registrar.activeContext().registerReceiver(pairingRequestReceiver, filter);
break;
case "pairingRequestHandlingDisable":
this.isPairingRequestHandlerSet = false;
try {
registrar.activeContext().unregisterReceiver(pairingRequestReceiver);
Log.d(TAG, "Stopped listening for pairing requests to handle");
}
catch (IllegalArgumentException ex) {
// Ignore `Receiver not registered` exception
}
break;
case "getBondedDevices":
ensurePermissions(new EnsurePermissionsCallback() {
@Override
public void onResult(boolean granted) {
if (!granted) {
result.error("no_permissions", "discovering other devices requires location access permission", null);
return;
}
List<Map<String, Object>> list = new ArrayList<>();
for (BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
Map<String, Object> entry = new HashMap<>();
entry.put("address", device.getAddress());
entry.put("name", device.getName());
entry.put("type", device.getType());
entry.put("isConnected", checkIsDeviceConnected(device));
entry.put("bondState", BluetoothDevice.BOND_BONDED);
list.add(entry);
}
result.success(list);
}
});
break;
case "isDiscovering":
result.success(bluetoothAdapter.isDiscovering());
break;
case "startDiscovery":
ensurePermissions(new EnsurePermissionsCallback() {
@Override
public void onResult(boolean granted) {
if (!granted) {
result.error("no_permissions", "discovering other devices requires location access permission", null);
return;
}
Log.d(TAG, "Starting discovery");
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intent.addAction(BluetoothDevice.ACTION_FOUND);
registrar.activeContext().registerReceiver(discoveryReceiver, intent);
bluetoothAdapter.startDiscovery();
result.success(null);
}
});
break;
case "cancelDiscovery":
Log.d(TAG, "Canceling discovery");
try {
registrar.activeContext().unregisterReceiver(discoveryReceiver);
}
catch (IllegalArgumentException ex) {
// Ignore `Receiver not registered` exception
}
bluetoothAdapter.cancelDiscovery();
if (discoverySink != null) {
discoverySink.endOfStream();
discoverySink = null;
}
result.success(null);
break;
case "isDiscoverable":
result.success(bluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
break;
case "requestDiscoverable": {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
if (call.hasArgument("duration")) {
try {
int duration = (int) call.argument("duration");
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'duration' argument is required to be integer", null);
break;
}
}
pendingResultForActivityResult = result;
ActivityCompat.startActivityForResult(registrar.activity(), intent, REQUEST_DISCOVERABLE_BLUETOOTH, null);
break;
}
////////////////////////////////////////////////////////////////////////////////
/* Connecting and connection */
case "connect": {
if (!call.hasArgument("address")) {
result.error("invalid_argument", "argument 'address' not found", null);
break;
}
String address;
try {
address = call.argument("address");
if (!BluetoothAdapter.checkBluetoothAddress(address)) {
throw new ClassCastException();
}
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'address' argument is required to be string containing remote MAC address", null);
break;
}
int id = ++lastConnectionId;
BluetoothConnectionWrapper connection = new BluetoothConnectionWrapper(id, bluetoothAdapter);
connections.put(id, connection);
Log.d(TAG, "Connecting to " + address + " (id: " + id + ")");
AsyncTask.execute(() -> {
try {
connection.connect(address);
registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
result.success(id);
}
});
}
catch (Exception ex) {
registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
result.error("connect_error", ex.getMessage(), exceptionToString(ex));
}
});
connections.remove(id);
}
});
break;
}
case "write": {
if (!call.hasArgument("id")) {
result.error("invalid_argument", "argument 'id' not found", null);
break;
}
int id;
try {
id = call.argument("id");
}
catch (ClassCastException ex) {
result.error("invalid_argument", "'id' argument is required to be integer id of connection", null);
break;
}
BluetoothConnection connection = connections.get(id);
if (connection == null) {
result.error("invalid_argument", "there is no connection with provided id", null);
break;
}
if (call.hasArgument("string")) {
String string = call.argument("string");
AsyncTask.execute(() -> {
try {
connection.write(string.getBytes());
registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
result.success(null);
}
});
}
catch (Exception ex) {
registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
result.error("write_error", ex.getMessage(), exceptionToString(ex));
}
});
}
});
}
else if (call.hasArgument("bytes")) {
byte[] bytes = call.argument("bytes");
AsyncTask.execute(() -> {
try {
connection.write(bytes);
registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
result.success(null);
}
});
}
catch (Exception ex) {
registrar.activity().runOnUiThread(new Runnable() {
@Override
public void run() {
result.error("write_error", ex.getMessage(), exceptionToString(ex));
}
});
}
});
}
else {
result.error("invalid_argument", "there must be 'string' or 'bytes' argument", null);
}
break;
}
default:
result.notImplemented();
break;
}
}
private interface EnsurePermissionsCallback {
public void onResult(boolean granted);
}
EnsurePermissionsCallback pendingPermissionsEnsureCallbacks = null;
private void ensurePermissions(EnsurePermissionsCallback callbacks) {
if (
ContextCompat.checkSelfPermission(registrar.activity(),
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(registrar.activity(),
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_COARSE_LOCATION_PERMISSIONS);
pendingPermissionsEnsureCallbacks = callbacks;
}
else {
callbacks.onResult(true);
}