Skip to content

Commit

Permalink
Fix NPE when bluetooth is off per #107
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgyoung committed Feb 4, 2015
1 parent c2ad29e commit 1b19c2b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ protected BluetoothAdapter getBluetoothAdapter() {
final BluetoothManager bluetoothManager =
(BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
LogManager.w(TAG, "Failed to construct a BluetoothAdapter");
}
}
return mBluetoothAdapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public CycledLeScannerForLollipop(Context context, long scanPeriod, long between
@Override
protected void stopScan() {
try {
mScanner.stopScan(getNewLeScanCallback());
if (getScanner() != null) {
getScanner().stopScan(getNewLeScanCallback());
}
}
catch (Exception e) {
LogManager.w(e, TAG, "Internal Android exception scanning for beacons");
Expand Down Expand Up @@ -94,8 +96,10 @@ protected boolean deferScanIfNeeded() {
ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();

try {
mScanner.startScan(new ScanFilterUtils().createScanFiltersForBeaconParsers(
mBeaconManager.getBeaconParsers()), settings, getNewLeScanCallback());
if (getScanner() != null) {
getScanner().startScan(new ScanFilterUtils().createScanFiltersForBeaconParsers(
mBeaconManager.getBeaconParsers()), settings, getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot start scan. Bluetooth may be turned off.");
Expand All @@ -120,7 +124,9 @@ protected boolean deferScanIfNeeded() {
// so we do not drain battery
LogManager.d(TAG, "We've been detecting for a bit. Stopping Android L background scanning");
try {
mScanner.stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
if (getScanner() != null) {
getScanner().stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
Expand Down Expand Up @@ -155,28 +161,28 @@ public void run() {
else {
if (mBackgroundLScanStartTime > 0l) {
try {
mScanner.stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
if (getScanner() != null) {
getScanner().stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}

mScanner.stopScan(getNewLeScanCallback());
if (getScanner() != null) {
getScanner().stopScan(getNewLeScanCallback());
}

mBackgroundLScanStartTime = 0;
}
mScanDeferredBefore = false;
}
return false;
}


@Override
protected void startScan() {
List<ScanFilter> filters = new ArrayList<ScanFilter>();
if (mScanner == null) {
LogManager.d(TAG, "Making new Android L scanner");
mScanner = getBluetoothAdapter().getBluetoothLeScanner();
}
ScanSettings settings;

if (mBackgroundFlag) {
Expand All @@ -188,7 +194,9 @@ protected void startScan() {

}
try {
mScanner.startScan(filters, settings, getNewLeScanCallback());
if (getScanner() != null) {
getScanner().startScan(filters, settings, getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot start scan. Bluetooth may be turned off.");
Expand All @@ -198,7 +206,9 @@ protected void startScan() {
@Override
protected void finishScan() {
try {
mScanner.stopScan(getNewLeScanCallback());
if (getScanner() != null) {
getScanner().stopScan(getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
Expand All @@ -207,6 +217,17 @@ protected void finishScan() {
mScanningPaused = true;
}

private BluetoothLeScanner getScanner() {
if (mScanner == null) {
LogManager.d(TAG, "Making new Android L scanner");
mScanner = getBluetoothAdapter().getBluetoothLeScanner();
if (mScanner == null) {
LogManager.w(TAG, "Failed to make new Android L scanner");
}
}
return mScanner;
}

private ScanCallback getNewLeScanCallback() {
if (leScanCallback == null) {
leScanCallback = new ScanCallback() {
Expand Down

0 comments on commit 1b19c2b

Please sign in to comment.