Skip to content
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

Use scan filters on Android 14 when the screen is off #1176

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,25 @@ protected void startScan() {
// We only add these filters on 8.1+ devices, because adding scan filters has been reported
// to cause scan failures on some Samsung devices with Android 5.x
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
if (Build.MANUFACTURER.equalsIgnoreCase("samsung") && !mPowerManager.isInteractive()) {
// On the Samsung Galaxy Note 8.1, scans are blocked with screen off when the
// scan filter is empty (wildcard). We do a more detailed filter on Samsung only
// because it might block detections of AltBeacon packets with non-standard
if ((Build.MANUFACTURER.equalsIgnoreCase("samsung") ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) &&
!mPowerManager.isInteractive()) {
// On the Samsung 8.1 and Android 14.0, scans are blocked with screen off when the
// scan filter is empty (wildcard). We do a more detailed filter on such devices
// only because it might block detections of AltBeacon packets with non-standard
// manufacturer codes. See #769 for details.
LogManager.d(TAG, "Using a non-empty scan filter since this is Samsung 8.1+");
LogManager.d(TAG, "Using a non-empty scan filter since this is 14.0 or Samsung 8.1+");
filters = new ScanFilterUtils().createScanFiltersForBeaconParsers(
mBeaconManager.getBeaconParsers());
}
else {
if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
LogManager.d(TAG, "Using a wildcard scan filter on Samsung because the screen is on. We will switch to a non-empty filter if the screen goes off");
// if this is samsung, as soon as the screen goes off we will need to start a different scan
LogManager.d(TAG, "Using a wildcard scan filter because the screen is on. We will switch to a non-empty filter if the screen goes off");
// as soon as the screen goes off we will need to start a different scan
// that has scan filters
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
mContext.getApplicationContext().registerReceiver(mSamsungScreenOffReceiver, filter);
LogManager.d(TAG, "registering SamsungScreenOffReceiver "+mSamsungScreenOffReceiver);
} else {
LogManager.d(TAG, "Using an empty scan filter since this is 8.1+ on Non-Samsung");
mContext.getApplicationContext().registerReceiver(mScreenOffReceiver, filter);
LogManager.d(TAG, "registering ScreenOffReceiver "+mScreenOffReceiver);
}
// The wildcard filter matches everything.
filters = new ScanFilterUtils().createWildcardScanFilters();
Expand All @@ -228,9 +228,9 @@ protected void startScan() {
@MainThread
public void stop() {
super.stop();
LogManager.d(TAG, "unregistering SamsungScreenOffReceiver as we stop the cycled scanner");
LogManager.d(TAG, "unregistering ScreenOffReceiver as we stop the cycled scanner");
// Catch the exception in case it has not been registered
try { mContext.getApplicationContext().unregisterReceiver(mSamsungScreenOffReceiver); } catch (IllegalArgumentException e) {}
try { mContext.getApplicationContext().unregisterReceiver(mScreenOffReceiver); } catch (IllegalArgumentException e) {}
}

@Override
Expand Down Expand Up @@ -411,14 +411,14 @@ public void onScanFailed(int errorCode) {
return leScanCallback;
}

private BroadcastReceiver mSamsungScreenOffReceiver = new BroadcastReceiver() {
private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!mMainScanCycleActive) {
LogManager.d(TAG, "Screen has gone off while outside the main scan cycle on Samsung. We will do nothing.");
LogManager.d(TAG, "Screen has gone off while outside the main scan cycle. We will do nothing.");
}
else {
LogManager.d(TAG, "Screen has gone off while using a wildcard scan filter on Samsung. Restarting scanner with non-empty filters.");
LogManager.d(TAG, "Screen has gone off while using a wildcard scan filter. Restarting scanner with non-empty filters.");
stopScan();
startScan();
}
Expand Down