Skip to content

Commit

Permalink
Merge pull request #53 from agap/fix/no-check-for-ble-adapter-on-stop
Browse files Browse the repository at this point in the history
Change the BluetoothAdapter state check to be run only on Android 5.0.
  • Loading branch information
agap authored Oct 28, 2020
2 parents c662066 + 952bf2a commit 048639a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
9 changes: 3 additions & 6 deletions luch/src/main/java/aga/android/luch/SystemBleDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.List;

Expand Down Expand Up @@ -43,11 +44,6 @@ public void startScans(@NonNull ScanCallback scanCallback) {
}

try {
// On Android 5.0 the behaviour of BluetoothAdapter/BluetoothLeScanner is different
// from subsequent releases - BluetoothAdapter.getBluetoothLeScanner() returns
// non-nullable reference even if Bluetooth is disabled, but an attempt to call
// startScan/stopScan on it raises an exception. On later versions,
// getBluetoothLeScanner() just returns null if Bluetooth is disabled.
if (!bluetoothAdapter.isEnabled()) {
BeaconLogger.e(
"BluetoothAdapter is not enabled, scans will not be started (check if "
Expand Down Expand Up @@ -84,7 +80,8 @@ public void startScans(@NonNull ScanCallback scanCallback) {
@Override
public void stopScans(@NonNull ScanCallback scanCallback) {
try {
if (!bluetoothAdapter.isEnabled()) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
&& !bluetoothAdapter.isEnabled()) {
BeaconLogger.e(
"Can't stop the BLE scans since BluetoothAdapter is not enabled, most likely "
+ "the scans weren't started either (check if Bluetooth is turned on)"
Expand Down
65 changes: 65 additions & 0 deletions luch/src/test/java/aga/android/luch/SystemBleDeviceSdk23Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aga.android.luch;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.Collections;
import java.util.List;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 23, manifest = Config.NONE)
public class SystemBleDeviceSdk23Test {
private final BluetoothAdapter adapter = mock(BluetoothAdapter.class);

private final BluetoothLeScanner scanner = mock(BluetoothLeScanner.class);

private final ScanSettings scanSettings = new ScanSettings.Builder().build();

private final List<ScanFilter> scanFilters = Collections.emptyList();

private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
}
};

private final SystemBleDevice device = new SystemBleDevice(
getApplicationContext(),
adapter,
scanSettings,
scanFilters
);

@Test
public void testAttemptToStopScansIsMadeEvenIfAdapterIsDisabled() {
// given
when(adapter.getBluetoothLeScanner()).thenReturn(scanner);
when(adapter.isEnabled()).thenReturn(false);

// when
device.stopScans(scanCallback);

// then
verify(adapter).getBluetoothLeScanner();
verifyNoMoreInteractions(adapter);

verify(scanner).stopScan(scanCallback);
verifyNoMoreInteractions(scanner);
}
}

0 comments on commit 048639a

Please sign in to comment.