diff --git a/src/main/java/org/altbeacon/beacon/service/BeaconService.java b/src/main/java/org/altbeacon/beacon/service/BeaconService.java index 3a50855a7..29f0d9007 100644 --- a/src/main/java/org/altbeacon/beacon/service/BeaconService.java +++ b/src/main/java/org/altbeacon/beacon/service/BeaconService.java @@ -37,8 +37,10 @@ import android.os.Build; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.Message; import android.os.Messenger; +import android.support.annotation.MainThread; import android.support.annotation.NonNull; import org.altbeacon.beacon.Beacon; @@ -145,9 +147,16 @@ static class IncomingHandler extends Handler { private final WeakReference mService; IncomingHandler(BeaconService service) { + /* + * Explicitly state this uses the main thread. Without this we defer to where the + * service instance is initialized/created; which is usually the main thread anyways. + * But by being explicit we document our code design expectations for where things run. + */ + super(Looper.getMainLooper()); mService = new WeakReference(service); } + @MainThread @Override public void handleMessage(Message msg) { BeaconService service = mService.get(); @@ -206,7 +215,7 @@ else if (msg.what == MSG_SYNC_SETTINGS) { */ final Messenger mMessenger = new Messenger(new IncomingHandler(this)); - + @MainThread @Override public void onCreate() { bluetoothCrashResolver = new BluetoothCrashResolver(this); @@ -294,6 +303,7 @@ public boolean onUnbind(Intent intent) { return false; } + @MainThread @Override public void onDestroy() { LogManager.e(TAG, "onDestroy()"); @@ -330,6 +340,7 @@ private PendingIntent getRestartIntent() { /** * methods for clients */ + @MainThread public void startRangingBeaconsInRegion(Region region, Callback callback) { synchronized (rangedRegionState) { if (rangedRegionState.containsKey(region)) { @@ -342,6 +353,7 @@ public void startRangingBeaconsInRegion(Region region, Callback callback) { mCycledScanner.start(); } + @MainThread public void stopRangingBeaconsInRegion(Region region) { int rangedRegionCount; synchronized (rangedRegionState) { @@ -355,6 +367,7 @@ public void stopRangingBeaconsInRegion(Region region) { } } + @MainThread public void startMonitoringBeaconsInRegion(Region region, Callback callback) { LogManager.d(TAG, "startMonitoring called"); monitoringStatus.addRegion(region, callback); @@ -362,6 +375,7 @@ public void startMonitoringBeaconsInRegion(Region region, Callback callback) { mCycledScanner.start(); } + @MainThread public void stopMonitoringBeaconsInRegion(Region region) { LogManager.d(TAG, "stopMonitoring called"); monitoringStatus.removeRegion(region); @@ -371,6 +385,7 @@ public void stopMonitoringBeaconsInRegion(Region region) { } } + @MainThread public void setScanPeriods(long scanPeriod, long betweenScanPeriod, boolean backgroundFlag) { mCycledScanner.setScanPeriods(scanPeriod, betweenScanPeriod, backgroundFlag); } diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java index 4c640cd6f..a176da53d 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java @@ -1,7 +1,6 @@ package org.altbeacon.beacon.service.scanner; import android.Manifest; -import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.PendingIntent; @@ -125,6 +124,7 @@ public static CycledLeScanner createScanner(Context context, long scanPeriod, lo * between LOW_POWER_MODE vs. LOW_LATENCY_MODE * @param backgroundFlag */ + @MainThread public void setScanPeriods(long scanPeriod, long betweenScanPeriod, boolean backgroundFlag) { LogManager.d(TAG, "Set scan periods called with %s, %s Background mode must have changed.", scanPeriod, betweenScanPeriod); @@ -165,6 +165,7 @@ public void setScanPeriods(long scanPeriod, long betweenScanPeriod, boolean back } } + @MainThread public void start() { LogManager.d(TAG, "start called"); mScanningEnabled = true; @@ -175,7 +176,7 @@ public void start() { } } - @SuppressLint("NewApi") + @MainThread public void stop() { LogManager.d(TAG, "stop called"); mScanningEnabled = false; @@ -194,6 +195,7 @@ public void setDistinctPacketsDetectedPerScan(boolean detected) { mDistinctPacketsDetectedPerScan = detected; } + @MainThread public void destroy() { LogManager.d(TAG, "Destroying"); // We cannot quit the thread used by the handler until queued Runnables have been processed, @@ -218,7 +220,7 @@ public void run() { protected abstract void startScan(); - @SuppressLint("NewApi") + @MainThread protected void scanLeDevice(final Boolean enable) { try { mScanCyclerStarted = true; @@ -285,6 +287,7 @@ protected void scanLeDevice(final Boolean enable) { } } + @MainThread protected void scheduleScanCycleStop() { // Stops scanning after a pre-defined scan period. long millisecondsUntilStop = mScanCycleStopTime - SystemClock.elapsedRealtime(); @@ -308,6 +311,7 @@ public void run() { protected abstract void finishScan(); + @MainThread private void finishScanCycle() { LogManager.d(TAG, "Done with scan cycle"); try {