Skip to content

Commit

Permalink
Protect against crashes if ble is disabled per #105
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgyoung committed Feb 3, 2015
1 parent 554d285 commit b4411e4
Showing 1 changed file with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ protected boolean deferScanIfNeeded() {
// if we see one of those beacons, we need to deliver the results
ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();

mScanner.startScan(new ScanFilterUtils().createScanFiltersForBeaconParsers(mBeaconManager.getBeaconParsers()), settings,
(android.bluetooth.le.ScanCallback) getNewLeScanCallback());
try {
mScanner.startScan(new ScanFilterUtils().createScanFiltersForBeaconParsers(mBeaconManager.getBeaconParsers()), settings,
(android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
catch (IllegalStateException e) {
Log.w(TAG, "Cannot start scan. Bluetooth may be turned off.");
}
}
else {
BeaconManager.logDebug(TAG, "This is Android L, but we last saw a beacon only "+
Expand All @@ -115,7 +120,14 @@ protected boolean deferScanIfNeeded() {
// a beacon in background L scanning mode. We need to stop scanning
// so we do not drain battery
BeaconManager.logDebug(TAG, "We've been detecting for a bit. Stopping Android L background scanning");
mScanner.stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());

try {
mScanner.stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
catch (IllegalStateException e) {
Log.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}

mBackgroundLScanStartTime = 0l;
}
else {
Expand Down Expand Up @@ -145,7 +157,12 @@ public void run() {
else {
if (mBackgroundLScanStartTime > 0l) {
BeaconManager.logDebug(TAG, "Stopping Android L background scanning to start full scan");
mScanner.stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
try {
mScanner.stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
catch (IllegalStateException e) {
Log.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}
mBackgroundLScanStartTime = 0;
}
mScanDeferredBefore = false;
Expand All @@ -171,13 +188,23 @@ protected void startScan() {
settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build();

}
mScanner.startScan(filters, settings, getNewLeScanCallback());

try {
mScanner.startScan(filters, settings, getNewLeScanCallback());
}
catch (IllegalStateException e) {
Log.w(TAG, "Cannot start scan. Bluetooth may be turned off.");
}
}

@Override
protected void finishScan() {
mScanner.stopScan(getNewLeScanCallback());
try {
mScanner.stopScan(getNewLeScanCallback());
}
catch (IllegalStateException e) {
Log.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}

mScanningPaused = true;
}

Expand Down

0 comments on commit b4411e4

Please sign in to comment.