react-native-ble-manager 11.5.0 → 11.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -44,7 +44,7 @@ import java.util.Set;
|
|
|
44
44
|
|
|
45
45
|
class BleManager extends ReactContextBaseJavaModule {
|
|
46
46
|
|
|
47
|
-
public static final String LOG_TAG = "
|
|
47
|
+
public static final String LOG_TAG = "RNBleManager";
|
|
48
48
|
private static final int ENABLE_REQUEST = 539;
|
|
49
49
|
|
|
50
50
|
private static class BondRequest {
|
|
@@ -170,8 +170,8 @@ class BleManager extends ReactContextBaseJavaModule {
|
|
|
170
170
|
if (Build.VERSION.SDK_INT >= UPSIDE_DOWN_CAKE){
|
|
171
171
|
// Google in 2023 decides that flag RECEIVER_NOT_EXPORTED or RECEIVER_EXPORTED should be explicit set SDK 34(UPSIDE_DOWN_CAKE) on registering receivers.
|
|
172
172
|
// Also the export flags are available on Android 8 and higher, should be used with caution so that don't break compability with that devices.
|
|
173
|
-
context.registerReceiver(mReceiver, filter, Context.
|
|
174
|
-
context.registerReceiver(mReceiver, intentFilter, Context.
|
|
173
|
+
context.registerReceiver(mReceiver, filter, Context.RECEIVER_EXPORTED);
|
|
174
|
+
context.registerReceiver(mReceiver, intentFilter, Context.RECEIVER_EXPORTED);
|
|
175
175
|
} else {
|
|
176
176
|
context.registerReceiver(mReceiver, filter);
|
|
177
177
|
context.registerReceiver(mReceiver, intentFilter);
|
|
@@ -823,7 +823,7 @@ class BleManager extends ReactContextBaseJavaModule {
|
|
|
823
823
|
}
|
|
824
824
|
|
|
825
825
|
WritableArray peripherals = Arguments.createArray();
|
|
826
|
-
for (String address : getCompanionDeviceManager().getAssociations()) {
|
|
826
|
+
for (String address : ((CompanionDeviceManager) getCompanionDeviceManager()).getAssociations()) {
|
|
827
827
|
peripherals.pushMap(retrieveOrCreatePeripheral(address).asWritableMap());
|
|
828
828
|
}
|
|
829
829
|
|
|
@@ -838,7 +838,7 @@ class BleManager extends ReactContextBaseJavaModule {
|
|
|
838
838
|
return;
|
|
839
839
|
}
|
|
840
840
|
|
|
841
|
-
CompanionDeviceManager manager = getCompanionDeviceManager();
|
|
841
|
+
CompanionDeviceManager manager = (CompanionDeviceManager) getCompanionDeviceManager();
|
|
842
842
|
for (String association : manager.getAssociations()) {
|
|
843
843
|
if (association.equals(address)) {
|
|
844
844
|
manager.disassociate(address);
|
|
@@ -851,8 +851,8 @@ class BleManager extends ReactContextBaseJavaModule {
|
|
|
851
851
|
}
|
|
852
852
|
|
|
853
853
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
854
|
-
public
|
|
855
|
-
return
|
|
854
|
+
public Object getCompanionDeviceManager() {
|
|
855
|
+
return reactContext
|
|
856
856
|
.getCurrentActivity().getSystemService(Context.COMPANION_DEVICE_SERVICE);
|
|
857
857
|
}
|
|
858
858
|
|
|
@@ -5,6 +5,7 @@ import static android.app.Activity.RESULT_OK;
|
|
|
5
5
|
import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
|
|
6
6
|
|
|
7
7
|
import android.app.Activity;
|
|
8
|
+
import android.bluetooth.BluetoothDevice;
|
|
8
9
|
import android.bluetooth.le.ScanFilter;
|
|
9
10
|
import android.bluetooth.le.ScanResult;
|
|
10
11
|
import android.companion.AssociationRequest;
|
|
@@ -35,7 +36,7 @@ public class CompanionScanner {
|
|
|
35
36
|
|
|
36
37
|
private final BleManager bleManager;
|
|
37
38
|
private final ReactContext reactContext;
|
|
38
|
-
public static final String LOG_TAG = "
|
|
39
|
+
public static final String LOG_TAG = BleManager.LOG_TAG + "_Companion";
|
|
39
40
|
private static final int SELECT_DEVICE_REQUEST_CODE = 540;
|
|
40
41
|
|
|
41
42
|
private static Callback scanCallback = null;
|
|
@@ -52,17 +53,29 @@ public class CompanionScanner {
|
|
|
52
53
|
// Have device?
|
|
53
54
|
Log.d(LOG_TAG, "Ok activity result");
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
Object result = intent.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
|
|
57
|
+
if (result != null) {
|
|
58
|
+
Peripheral peripheral = null;
|
|
59
|
+
if (result instanceof BluetoothDevice) {
|
|
60
|
+
peripheral = bleManager.savePeripheral((BluetoothDevice) result);
|
|
61
|
+
} else if (result instanceof ScanResult) {
|
|
62
|
+
peripheral = bleManager.savePeripheral(((ScanResult) result).getDevice());
|
|
63
|
+
}
|
|
64
|
+
if (peripheral != null && scanCallback != null) {
|
|
65
|
+
scanCallback.invoke(null, peripheral.asWritableMap());
|
|
66
|
+
scanCallback = null;
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
scanCallback.invoke(null, null);
|
|
70
|
+
scanCallback = null;
|
|
60
71
|
}
|
|
72
|
+
|
|
61
73
|
} else {
|
|
62
74
|
// No device, user cancelled?
|
|
63
75
|
Log.d(LOG_TAG, "Non-ok activity result");
|
|
64
76
|
if (scanCallback != null) {
|
|
65
77
|
scanCallback.invoke(null, null);
|
|
78
|
+
scanCallback = null;
|
|
66
79
|
}
|
|
67
80
|
}
|
|
68
81
|
}
|
|
@@ -77,6 +90,11 @@ public class CompanionScanner {
|
|
|
77
90
|
public void scan(ReadableArray serviceUUIDs, ReadableMap options, Callback callback) {
|
|
78
91
|
Log.d(LOG_TAG, "companion scan start");
|
|
79
92
|
|
|
93
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
|
94
|
+
callback.invoke("Companion not supported");
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
80
98
|
AssociationRequest.Builder builder = new AssociationRequest.Builder()
|
|
81
99
|
.setSingleDevice(options.hasKey("single") && options.getBoolean("single")) ;
|
|
82
100
|
|
|
@@ -97,7 +115,8 @@ public class CompanionScanner {
|
|
|
97
115
|
}
|
|
98
116
|
scanCallback = callback;
|
|
99
117
|
|
|
100
|
-
|
|
118
|
+
CompanionDeviceManager companionDeviceManager = (CompanionDeviceManager) bleManager.getCompanionDeviceManager();
|
|
119
|
+
companionDeviceManager.associate(pairingRequest, new CompanionDeviceManager.Callback() {
|
|
101
120
|
@Override
|
|
102
121
|
public void onFailure(@Nullable CharSequence charSequence) {
|
|
103
122
|
Log.d(LOG_TAG, "companion failure: " + charSequence);
|