diff --git a/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/scan/BackgroundScannerImpl.java b/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/scan/BackgroundScannerImpl.java index 213ceebfa..f4633f8e9 100644 --- a/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/scan/BackgroundScannerImpl.java +++ b/rxandroidble/src/main/java/com/polidea/rxandroidble2/internal/scan/BackgroundScannerImpl.java @@ -49,6 +49,10 @@ public void scanBleDeviceInBackground(@NonNull PendingIntent callbackIntent, Sca RxBleLog.w(TAG, "PendingIntent based scanning is available for Android O and higher only."); return; } + if (!rxBleAdapterWrapper.isBluetoothEnabled()) { + RxBleLog.w(TAG, "PendingIntent based scanning is available only when Bluetooth is ON."); + throw new BleScanException(BleScanException.BLUETOOTH_DISABLED); + } RxBleLog.i(TAG, "Requesting pending intent based scan."); final List nativeScanFilters = scanObjectsConverter.toNativeFilters(scanFilters); @@ -69,6 +73,10 @@ public void stopBackgroundBleScan(@NonNull PendingIntent callbackIntent) { RxBleLog.w(TAG, "PendingIntent based scanning is available for Android O and higher only."); return; } + if (!rxBleAdapterWrapper.isBluetoothEnabled()) { + RxBleLog.w(TAG, "PendingIntent based scanning is available only when Bluetooth is ON."); + return; + } RxBleLog.i(TAG, "Stopping pending intent based scan."); rxBleAdapterWrapper.stopLeScan(callbackIntent); diff --git a/rxandroidble/src/test/groovy/com/polidea/rxandroidble2/internal/scan/BackgroundScannerTest.groovy b/rxandroidble/src/test/groovy/com/polidea/rxandroidble2/internal/scan/BackgroundScannerTest.groovy index 53f1507e6..c38cb4670 100644 --- a/rxandroidble/src/test/groovy/com/polidea/rxandroidble2/internal/scan/BackgroundScannerTest.groovy +++ b/rxandroidble/src/test/groovy/com/polidea/rxandroidble2/internal/scan/BackgroundScannerTest.groovy @@ -41,6 +41,7 @@ class BackgroundScannerTest extends ElectricSpecification { def pendingIntent = Mock(PendingIntent) def settings = Mock(ScanSettings) def scanFilter = emptyFilters() + adapterWrapper.isBluetoothEnabled() >> true 1 * adapterWrapper.startLeScan(_, _, _) >> errorCode when: @@ -54,11 +55,53 @@ class BackgroundScannerTest extends ElectricSpecification { errorCode << [ScanCallback.SCAN_FAILED_ALREADY_STARTED, ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED] } + def "should throw BleScanException if bluetooth is OFF while calling `.scanBleDeviceInBackground()`"() { + given: + def pendingIntent = Mock(PendingIntent) + def settings = Mock(ScanSettings) + def scanFilter = emptyFilters() + adapterWrapper.isBluetoothEnabled() >> false + + when: + objectUnderTest.scanBleDeviceInBackground(pendingIntent, settings, scanFilter) + + then: + def scanException = thrown(BleScanException) + + and: + scanException.reason == BleScanException.BLUETOOTH_DISABLED + } + + def "should not throw if bluetooth is OFF while calling `.stopBackgroundBleScan()`"() { + given: + def pendingIntent = Mock(PendingIntent) + adapterWrapper.isBluetoothEnabled() >> false + + when: + objectUnderTest.stopBackgroundBleScan(pendingIntent) + + then: + noExceptionThrown() + } + + def "should not call RxBleAdapterWrapper.stopLeScan() if bluetooth is OFF while calling `.stopBackgroundBleScan()`"() { + given: + def pendingIntent = Mock(PendingIntent) + adapterWrapper.isBluetoothEnabled() >> false + + when: + objectUnderTest.stopBackgroundBleScan(pendingIntent) + + then: + 0 * adapterWrapper.stopLeScan(pendingIntent) + } + def "should pass callback intent to a wrapper"() { given: def pendingIntent = Mock(PendingIntent) def settings = Mock(ScanSettings) def scanFilter = emptyFilters() + adapterWrapper.isBluetoothEnabled() >> true when: objectUnderTest.scanBleDeviceInBackground(pendingIntent, settings, scanFilter) @@ -78,6 +121,7 @@ class BackgroundScannerTest extends ElectricSpecification { def scanFilter = emptyFilters() def nativeFilters = [Mock(android.bluetooth.le.ScanFilter)] def nativeSettings = Mock(android.bluetooth.le.ScanSettings) + adapterWrapper.isBluetoothEnabled() >> true when: objectUnderTest.scanBleDeviceInBackground(pendingIntent, settings, scanFilter) @@ -92,6 +136,7 @@ class BackgroundScannerTest extends ElectricSpecification { def "should pass callback intent when stopping scan"() { given: def pendingIntent = Mock(PendingIntent) + adapterWrapper.isBluetoothEnabled() >> true when: objectUnderTest.stopBackgroundBleScan(pendingIntent)