-
Notifications
You must be signed in to change notification settings - Fork 837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start and stop BLE scans from a background thread to prevent blocking the UI (issue #136) #430
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.os.Handler; | ||
import android.os.HandlerThread; | ||
import android.os.Looper; | ||
import android.os.SystemClock; | ||
|
||
import org.altbeacon.beacon.BeaconManager; | ||
|
@@ -40,7 +42,10 @@ public abstract class CycledLeScanner { | |
private long mScanPeriod; | ||
|
||
protected long mBetweenScanPeriod; | ||
protected final Handler mHandler = new Handler(); | ||
|
||
protected final Handler mHandler = new Handler(Looper.getMainLooper()); | ||
protected final Handler mScanHandler; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidgyoung see my answer below (and this doc), this is the change that makes the whole PR work: by creating it with Instead, by creating a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, got it now... apologies, it's been awhile, it's been awhile since I looked at this code. I was incorrectly thinking that new |
||
private final HandlerThread mScanThread; | ||
|
||
protected final BluetoothCrashResolver mBluetoothCrashResolver; | ||
protected final CycledLeScanCallback mCycledLeScanCallback; | ||
|
@@ -57,6 +62,10 @@ protected CycledLeScanner(Context context, long scanPeriod, long betweenScanPeri | |
mCycledLeScanCallback = cycledLeScanCallback; | ||
mBluetoothCrashResolver = crashResolver; | ||
mBackgroundFlag = backgroundFlag; | ||
|
||
mScanThread = new HandlerThread("CycledLeScannerThread"); | ||
mScanThread.start(); | ||
mScanHandler = new Handler(mScanThread.getLooper()); | ||
} | ||
|
||
public static CycledLeScanner createScanner(Context context, long scanPeriod, long betweenScanPeriod, boolean backgroundFlag, CycledLeScanCallback cycledLeScanCallback, BluetoothCrashResolver crashResolver) { | ||
|
@@ -156,6 +165,10 @@ public void stop() { | |
} | ||
} | ||
|
||
public void destroy() { | ||
mScanThread.quit(); | ||
} | ||
|
||
protected abstract void stopScan(); | ||
|
||
protected abstract boolean deferScanIfNeeded(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of calling
Looper.getMainLooper()
here? This handler will never need to communicate with the main UI thread, so why not let it have its own looper?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default, parameterless
Handler
constructor uses theLooper
from the current thread (as documented here and here).In this particular case,
CycledLeScanner
is always instantiated from the main thread (fromBeaconService.onCreate()
) and consequently, thatHandler
will already use the main thread's looper.This addition doesn't modify the current logic, it's only a futureproof guarantee that the class behaviour won't change if in the future if
CycledLeScanner
is instantiated from another thread that doesn't have aLooper
(you would immediately notice because the Handler creation would crash).