Skip to content

Commit a3bf4af

Browse files
Elias Lecomteeliaslecomte
authored andcommitted
Replace throwing of IllegalStateException with using onError of the observable. On react-native, you can't try catch native exceptions when an IllegalStateException is used.
1 parent d535f70 commit a3bf4af

File tree

1 file changed

+12
-6
lines changed
  • android/library/src/main/java/com/polidea/multiplatformbleadapter

1 file changed

+12
-6
lines changed

android/library/src/main/java/com/polidea/multiplatformbleadapter/BleModule.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ public void getKnownDevices(String[] deviceIdentifiers,
360360
OnSuccessCallback<Device[]> onSuccessCallback,
361361
OnErrorCallback onErrorCallback) {
362362
if (rxBleClient == null) {
363-
throw new IllegalStateException("BleManager not created when tried to get known devices");
363+
onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to get known devices", null));
364+
return;
364365
}
365366

366367
List<Device> knownDevices = new ArrayList<>();
@@ -384,7 +385,8 @@ public void getConnectedDevices(String[] serviceUUIDs,
384385
OnSuccessCallback<Device[]> onSuccessCallback,
385386
OnErrorCallback onErrorCallback) {
386387
if (rxBleClient == null) {
387-
throw new IllegalStateException("BleManager not created when tried to get connected devices");
388+
onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to get connected devices", null));
389+
return;
388390
}
389391

390392
if (serviceUUIDs.length == 0) {
@@ -425,7 +427,8 @@ public void connectToDevice(String deviceIdentifier,
425427
OnEventCallback<ConnectionState> onConnectionStateChangedCallback,
426428
OnErrorCallback onErrorCallback) {
427429
if (rxBleClient == null) {
428-
throw new IllegalStateException("BleManager not created when tried to connect to device");
430+
onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to connect to device", null));
431+
return;
429432
}
430433

431434
final RxBleDevice device = rxBleClient.getBleDevice(deviceIdentifier);
@@ -449,7 +452,8 @@ public void cancelDeviceConnection(String deviceIdentifier,
449452
OnSuccessCallback<Device> onSuccessCallback,
450453
OnErrorCallback onErrorCallback) {
451454
if (rxBleClient == null) {
452-
throw new IllegalStateException("BleManager not created when tried to cancel device connection");
455+
onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to cancel device connection", null));
456+
return;
453457
}
454458

455459
final RxBleDevice device = rxBleClient.getBleDevice(deviceIdentifier);
@@ -470,7 +474,8 @@ public void isDeviceConnected(String deviceIdentifier,
470474
OnSuccessCallback<Boolean> onSuccessCallback,
471475
OnErrorCallback onErrorCallback) {
472476
if (rxBleClient == null) {
473-
throw new IllegalStateException("BleManager not created when tried to check if device is connected");
477+
onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to check if device is connected", null));
478+
return;
474479
}
475480

476481
final RxBleDevice device = rxBleClient.getBleDevice(deviceIdentifier);
@@ -1218,7 +1223,8 @@ private void safeStartDeviceScan(final UUID[] uuids,
12181223
final OnEventCallback<ScanResult> onEventCallback,
12191224
final OnErrorCallback onErrorCallback) {
12201225
if (rxBleClient == null) {
1221-
throw new IllegalStateException("BleManager not created when tried to start device scan");
1226+
onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to start device scan", null));
1227+
return;
12221228
}
12231229

12241230
ScanSettings scanSettings = new ScanSettings.Builder()

0 commit comments

Comments
 (0)