Skip to content

Commit

Permalink
Catch NPE when starting/stopping scanning. Android issue #160503
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgyoung committed Jun 27, 2015
1 parent 6f0b4a2 commit 338c18b
Showing 1 changed file with 18 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ public CycledLeScannerForLollipop(Context context, long scanPeriod, long between
protected void stopScan() {
try {
if (getScanner() != null) {
getScanner().stopScan(getNewLeScanCallback());
try {
getScanner().stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
catch (NullPointerException npe) {
// Necessary because of https://code.google.com/p/android/issues/detail?id=160503
LogManager.e(TAG, "Cannot stop scan. Unexpected NPE.", npe);
}
}
}
catch (Exception e) {
LogManager.w(e, TAG, "Internal Android exception scanning for beacons");
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}
}

Expand Down Expand Up @@ -95,24 +101,7 @@ protected boolean deferScanIfNeeded() {
// On Android L, between scan cycles do a scan with a filter looking for any beacon
// 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();

try {
if (getScanner() != null) {
List scanFilters = new ScanFilterUtils().createScanFiltersForBeaconParsers(
mBeaconManager.getBeaconParsers());
ScanCallback callback = getNewLeScanCallback();
try {
getScanner().startScan(scanFilters, settings, callback);
}
catch (NullPointerException npe) {
LogManager.w(TAG, "Cannot start scan. Unexpected NPE.", npe);
}
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot start scan. Bluetooth may be turned off.");
}

startScan();
} else {
LogManager.d(TAG, "This is Android L, but we last saw a beacon only %s "
+ "ago, so we will not keep scanning in background.",
Expand All @@ -131,14 +120,7 @@ protected boolean deferScanIfNeeded() {
// a beacon in background L scanning mode. We need to stop scanning
// so we do not drain battery
LogManager.d(TAG, "We've been detecting for a bit. Stopping Android L background scanning");
try {
if (getScanner() != null) {
getScanner().stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}
stopScan();
mBackgroundLScanStartTime = 0l;
}
else {
Expand Down Expand Up @@ -168,15 +150,7 @@ public void run() {
}
else {
if (mBackgroundLScanStartTime > 0l) {
try {
if (getScanner() != null) {
getScanner().stopScan((android.bluetooth.le.ScanCallback) getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}

stopScan();
mBackgroundLScanStartTime = 0;
}
mScanDeferredBefore = false;
Expand All @@ -197,13 +171,17 @@ protected void startScan() {
settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build();

}

try {
if (getScanner() != null) {
List scanFilters = new ScanFilterUtils().createScanFiltersForBeaconParsers(
mBeaconManager.getBeaconParsers());
ScanCallback callback = getNewLeScanCallback();
try {
getScanner().startScan(filters, settings, callback);
getScanner().startScan(scanFilters, settings, callback);
}
catch (NullPointerException npe) {
// Necessary because of https://code.google.com/p/android/issues/detail?id=160503
LogManager.w(TAG, "Cannot start scan. Unexpected NPE.", npe);
}
}
Expand All @@ -215,15 +193,7 @@ protected void startScan() {

@Override
protected void finishScan() {
try {
if (getScanner() != null) {
getScanner().stopScan(getNewLeScanCallback());
}
}
catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
}

stopScan();
mScanningPaused = true;
}

Expand Down

0 comments on commit 338c18b

Please sign in to comment.