Fix runtime crashes reported from the field.
@@ -19,6 +19,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.os.Build;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
@@ -258,6 +259,7 @@ public class BluetoothMedic {
}
};
if(scanner != null) {
+ try {
scanner.startScan(callback);
while (this.mScanTestResult == null) {
LogManager.d(TAG, "Waiting for scan test to complete...");
@@ -271,17 +273,16 @@ public class BluetoothMedic {
break;
}
}
- try {
scanner.stopScan(callback);
- } catch (IllegalStateException e) { /* do nothing */ } // caught if bluetooth is off here
+ } catch (IllegalStateException e) {
+ LogManager.d(TAG, "Bluetooth is off. Cannot run scan test.");
+ }
}
else {
LogManager.d(TAG, "Cannot get scanner");
}
}
-
-
LogManager.d(TAG, "scan test complete");
return this.mScanTestResult == null || this.mScanTestResult;
}
@@ -302,8 +303,8 @@ public class BluetoothMedic {
initializeWithContext(context);
this.mTransmitterTestResult = null;
long testStartTime = System.currentTimeMillis();
- if (this.mAdapter != null) {
- final BluetoothLeAdvertiser advertiser = this.mAdapter.getBluetoothLeAdvertiser();
+ if (mAdapter != null) {
+ final BluetoothLeAdvertiser advertiser = getAdvertiserSafely(mAdapter);
if(advertiser != null) {
AdvertiseSettings settings = (new Builder()).setAdvertiseMode(0).build();
AdvertiseData data = (new android.bluetooth.le.AdvertiseData.Builder())
@@ -454,4 +455,17 @@ public class BluetoothMedic {
jobScheduler.schedule(builder.build());
}
}
+
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
+ private BluetoothLeAdvertiser getAdvertiserSafely(BluetoothAdapter adapter) {
+ try {
+ // This can sometimes throw a NullPointerException as reported here:
+ // https://github.com/AltBeacon/android-beacon-library/issues/672
+ return adapter.getBluetoothLeAdvertiser();
+ }
+ catch (Exception e) {
+ LogManager.w(TAG, "Cannot get bluetoothLeAdvertiser", e);
+ }
+ return null;
+ }
}
\ No newline at end of file
