From a3bf4afe03de044dfd7cfe3cfe80b253d4e56d73 Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Mon, 10 Aug 2020 15:23:15 +0200 Subject: [PATCH] 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. --- .../multiplatformbleadapter/BleModule.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/android/library/src/main/java/com/polidea/multiplatformbleadapter/BleModule.java b/android/library/src/main/java/com/polidea/multiplatformbleadapter/BleModule.java index 2fabd21a..89c53176 100755 --- a/android/library/src/main/java/com/polidea/multiplatformbleadapter/BleModule.java +++ b/android/library/src/main/java/com/polidea/multiplatformbleadapter/BleModule.java @@ -360,7 +360,8 @@ public void getKnownDevices(String[] deviceIdentifiers, OnSuccessCallback onSuccessCallback, OnErrorCallback onErrorCallback) { if (rxBleClient == null) { - throw new IllegalStateException("BleManager not created when tried to get known devices"); + onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to get known devices", null)); + return; } List knownDevices = new ArrayList<>(); @@ -384,7 +385,8 @@ public void getConnectedDevices(String[] serviceUUIDs, OnSuccessCallback onSuccessCallback, OnErrorCallback onErrorCallback) { if (rxBleClient == null) { - throw new IllegalStateException("BleManager not created when tried to get connected devices"); + onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to get connected devices", null)); + return; } if (serviceUUIDs.length == 0) { @@ -425,7 +427,8 @@ public void connectToDevice(String deviceIdentifier, OnEventCallback onConnectionStateChangedCallback, OnErrorCallback onErrorCallback) { if (rxBleClient == null) { - throw new IllegalStateException("BleManager not created when tried to connect to device"); + onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to connect to device", null)); + return; } final RxBleDevice device = rxBleClient.getBleDevice(deviceIdentifier); @@ -449,7 +452,8 @@ public void cancelDeviceConnection(String deviceIdentifier, OnSuccessCallback onSuccessCallback, OnErrorCallback onErrorCallback) { if (rxBleClient == null) { - throw new IllegalStateException("BleManager not created when tried to cancel device connection"); + onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to cancel device connection", null)); + return; } final RxBleDevice device = rxBleClient.getBleDevice(deviceIdentifier); @@ -470,7 +474,8 @@ public void isDeviceConnected(String deviceIdentifier, OnSuccessCallback onSuccessCallback, OnErrorCallback onErrorCallback) { if (rxBleClient == null) { - throw new IllegalStateException("BleManager not created when tried to check if device is connected"); + onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to check if device is connected", null)); + return; } final RxBleDevice device = rxBleClient.getBleDevice(deviceIdentifier); @@ -1218,7 +1223,8 @@ private void safeStartDeviceScan(final UUID[] uuids, final OnEventCallback onEventCallback, final OnErrorCallback onErrorCallback) { if (rxBleClient == null) { - throw new IllegalStateException("BleManager not created when tried to start device scan"); + onErrorCallback.onError(new BleError(BleErrorCode.BluetoothManagerDestroyed, "BleManager not created when tried to start device scan", null)); + return; } ScanSettings scanSettings = new ScanSettings.Builder()