Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed NPE when calling BackgroundScanner while BT is OFF #487

Merged
merged 2 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<android.bluetooth.le.ScanFilter> nativeScanFilters = scanObjectsConverter.toNativeFilters(scanFilters);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down