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

No beacons after locked screen #1174

Open
juliankotrba opened this issue Nov 28, 2023 · 22 comments
Open

No beacons after locked screen #1174

juliankotrba opened this issue Nov 28, 2023 · 22 comments

Comments

@juliankotrba
Copy link

Behavior + steps to reproduce this behavior

I am creating this ticket because we experience a weird problem with only one of our test devices (Pixel 7). The issue is one of the classic ones: as soon as we lock the phone and the display goes dark, the ranging stops.

I already tried to change all the different library settings in our app but the behavior stayed the same. To simplify the analysis of the problem for you, I checked out the android-beacon-library-reference-kotlin project (#2aa42a44) and tried to reproduce the problem there, which was actually reproducible. To prove that the problem occurs, I made some small modifications to the project.

  1. I updated the unique region id to match our beacon scheme.

  2. I uncommented the following lines

beaconManager.setBackgroundBetweenScanPeriod(0);
beaconManager.setBackgroundScanPeriod(1100);
  1. I added a beep tone when central ranging observer is called:
...
if (rangeAgeMillis < 10000) {
    val toneGen1 = ToneGenerator(AudioManager.STREAM_MUSIC, 100)
    toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150)
    Log.d(MainActivity.TAG, "Ranged: ${beacons.count()} beacons")
    ...
}
...
  1. I set the debug mode to true and added a custom logger, which is a replica of the VerboseAndroidLogger except that I prefixed the tag with "DEBUG" in order to make it easily filterable in the Logcat.

Then I ran the app on our Pixel 8 (Android 14) and on our Pixel 7. On the Pixel 8, the beeping continued after locking the phone while it stopped on the Pixel 7. I suspect that the problem is due to a device setting, but I haven't been able to identify it yet.

While reading through the GH issues I stumbled across your comment here where you mention three possible reasons why beacons are not detected when the screen is off: #944 (comment)

Ad 1. If the hardware code hasn't been set correctly, this would then lead to problems on different devices right? Since it works on almost all other phones I assume this one is correct.

Ad 2. In case the beacon parser layout is incorrect, I also assume it would also affect other devices.

Ad 3. I uninstalled all BLE scanning apps and tried again, no change in behavior

After the screen becomes active again, the beeping and therefore the ranging starts again.

Mobile device model and OS version

Pixel 7
Android 14
Build number: AP11.231020.016.A1
Security update: 1 November 2023
This devices is enrolled in the beta program

Library Logs:

altbeacon-logs.txt

We would really appreciate it if you could shed some light on this behavior.
Please let me know if any other important information is missing or if there is anything I can assist you with.

Thanks
Julian

@davidgyoung
Copy link
Member

Hi @juliankotrba thank you for this report.

I was able to reoproduce your results. At first glance, this appears to be a change to Android 14. It seems that with the screen off, scans with empty Bluetooth LE scan filters return no results. (This is how Samsung forks of Android behaved since 8.1, but Pixels did not appear to work this way as recently as Android 13).

I tried modifying the Android Beacon Library code to use non-empty scan filters (like the library already does for Samsung) and the ranging started working even with the screen off: #1176

Does your app target Android 14? Do you know if this matters?

@juliankotrba
Copy link
Author

Hi @davidgyoung.

I tried modifying the Android Beacon Library code to use non-empty scan filters (like the library already does for Samsung) and the ranging started working even with the screen off: #1176

Nice, I will test your fix and give you feedback later!

Does your app target Android 14? Do you know if this matters?
Yes, we target Android 14 but I am not sure if this matters. As far as I remember I did some tests were I went back in the git history of the project and it also did not work when we did not target Android 14.

When you were able to reproduce the issue, on which Pixel did you test? For us, the issue only appeared on our Pixel 7, but not Pixel 8 (tested on one Pixel 7 and two Pixel 8).

Thanks for your help!

@juliankotrba
Copy link
Author

@davidgyoung Pixel 7 is working again! 👏

@davidgyoung
Copy link
Member

davidgyoung commented Dec 6, 2023 via email

@davidgyoung
Copy link
Member

Test results on various devices:

Model      OS         Library Version   Ranges with Screen Off
--------  ----------  ---------------   ------------------------
Pixel 4a  Android 13  2.20.2            YES
Pixel 4a  Android 13  #1176             YES
Pixel 6a  Android 12  2.20.2            YES
Pixel 6a  Android 12  #1176             YES
Pixel 6a  Android 13  2.20.2            YES
Pixel 6a  Android 13  #1176             YES
Pixel 6a  Android 14  2.20.2            YES   
Pixel 6a  Android 14  #1176             YES
Pixel 7   Android 14  2.20.2            NO
Pixel 7   Android 14  #1176             YES

Note: the two different Android 14 build numbers above are:

Pixel 6a Android 14 build number UP1A.231105.003
Pixel 7 Android 14 build number U1B2.230922.006

Only the Pixel 7 Android 14 build shown above ever seems to block beacon detections with the screen off unless a non-empty bluetooth scan filter is active as added to library build #1176

@davidgyoung
Copy link
Member

davidgyoung commented Dec 8, 2023

I did a few more tests with Pixel 7 Android 14, varying the target SDK in the app:

Model      OS         Library Version Target SDK   Ranges with Screen Off  Notes
--------  ----------  --------------- ----------   ----------------------  -----
Pixel 7   Android 14  2.20.2          33           YES                     Clean install
Pixel 7   Android 14  2.20.2          34           YES                     Upgrade from above
Pixel 7   Android 14  2.20.2          34           NO                      Clean install

So the problem only happens on the Pixel 7 if set the targetSdk to 34. Further, if you set the targetSdk to 33, run the app, then change the targetSdk to 34 and run the app again (without uninstall), the behavior is not seen. So it seems the behavior only applies to apps targeting Android 14 and newly installed on Pixel 7s.

@kangjjang
Copy link

kangjjang commented Jan 5, 2024

I don't recognize the beacon after a few minutes after the screen is turned off.
After a few minutes (10 minutes to 15 minutes), sometimes it is recognized.

image

Your Kotlin reference code is used and Alt Beacon Library 2.20.2 is being used.
In the code below, which part should I activate or modify it to use the beacons in a short time (within 1 minute to 2 minutes) even in the LCD OFF state below?
Or is it impossible to scan a beacon within 1 to 2 minutes in Android LCD OFF?

In the code below, which part should I activate or modify it to use the beacons in a short time (within 1 minute to 2 minutes) even in the LCD OFF state below?
I added the code below.

https://github.com/davidgyoung/android-beacon-library-rational-kotlin/blob/master/app/src/main/java/org/OLTBEACON/BEACONREPERENCE Plcation.kt

Added code
BeaconManager.setBackgroundBetWeyenscanperiod (100);
BeaconManager.SetBackgroundScanperiod (1000);
BeaconManager.SetForegroundBet WeEenscanperiod (100)
BeaconManager.setScanperiod (1000)

@davidgyoung
Copy link
Member

davidgyoung commented Jan 9, 2024

@kangjjang, this thread is dedicated to detection problems with the screen off ONLY on Android 14 on Pixel phones. It sounds like you need general help configuring an app for fast background detections. Please see this page: Alternate Scan Strategies. You will need to configure the library to use either a Foreground Service or the Intent Scan Strategy described at the very bottom of this page.

If you need further help configuring your custom app, please open a new question on StackOverflow.com, as that is the proper forum for assistance with using the library with third party apps. This thread is ONLY for trouble detecting with the screen off on Android 14 on Pixel phones.

@akoskuczi-bw
Copy link

Hi @davidgyoung,
Same issue with Motorola g54 even with Android version 13.
Build: 5.10.168-android12-9-00003-g828a3b1f7462-ab10564101

From logcat: "BtGatt.ScanManager com.android.bluetooth W Cannot start unfiltered scan in screen-off. This scan will be resumed later: 14"

@davidgyoung
Copy link
Member

@akoskuczi-bw, the logcat line you mention has been around since Android 8.1, and as originally created, applied only to the case where there are zero scan filters attached to the scan:

https://android.googlesource.com/platform/packages/apps/Bluetooth/+/319aeae6f4ebd13678b4f77375d1804978c4a1e1%5E%21/#F1

When do you see this log line with Android 13? Does it happen every time you try to start a scan with the screen off or just sometimes? Is in an app using this library?

@akoskuczi-bw
Copy link

Yes, it is on Android 13. The scanning stops working consistently when the screen is off, and it restarts when the screen is on (not necessarily unlocked). I am using it with the following Flutter plugin: https://github.com/Lukangi/beacon_scanner/tree/main/beacon_scanner

@davidgyoung
Copy link
Member

Researching AOSP commits, I found this change from July 2023 that is now part of Android 14:

https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2654980

Interestingly the commit is from a Motorola engineer, so perhaps the change went in to an earlier proprietary change on Motorola that does not show up in AOSP.

OK, so this sounds believable. However, this does not explain the OP's report that the restriction did not apply to Pixel 8 with Android 14, and my observation above that it did not apply to Pixel 6a with Android 14. Could it be that this AOSP change was not in the Android 14 builds for the Pixel 6a and Pixel 8? What else could explain this?

@davidgyoung
Copy link
Member

I have merged the change to fix this for Android 14. This does not address the problem for Motorola Android 13 as reported by @akoskuczi-bw. Does anybody else on this thread have a Motorola device with Android 13 to confirm the problem there?

If we can get a confirmation of this on a second Motorola Android 13 device, I will make a new change that creates a scan filter for those devices, too. (I am reluctant to do this without confirmation since it could be a breaking change for apps that have been running on Motorola phones for the past year.) Until this is resolved I will leave this issue open.

@akoskuczi-bw
Copy link

@davidgyoung I absolutely understand your position regarding Motorola. I'm wondering if anyone can confirm the issue with Motorola. Thank you very much for your time and efforts.

@juliankotrba
Copy link
Author

juliankotrba commented Feb 28, 2024

@akoskuczi-bw @davidgyoung FYI I can't confirm the issue on the Motorola, however, I can reproduce it on our "new" Asus Zenfone 9 running with Android 13. Maybe also on a OnePlus but for that I need to do some more investigation.

Adding Build.MANUFACTURER.equalsIgnoreCase("asus") to the scan filter check if statement solves the issue on my Asus.

EDIT:

To make it more clear what I did locally to get it working:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
                if ((Build.MANUFACTURER.equalsIgnoreCase("samsung") || 
                          --> Build.MANUFACTURER.equalsIgnoreCase("asus")  || <--
                          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 14.0 or Samsung 8.1+");
                    filters = new ScanFilterUtils().createScanFiltersForBeaconParsers(
                            mBeaconManager.getBeaconParsers());
                }

@davidgyoung
Copy link
Member

thanks, @juliankotrba. it sounds like we do need to include Asus and Motorola on Android 13

@kerembalcan
Copy link

kerembalcan commented Mar 11, 2024

I have a similar issue with a Samsung Galaxy A34 (Android 14) in the latest version of the library(2.20.4). Since the issue is very similar, I'm posting it here instead of creating a new one, even though my device is not a Pixel device.

Attaching some log files here to avoid a distracting view in the comment.

As mentioned in the top comment, I’m testing the reference app with the foreground service configuration enabled, and I experience that both ranging and monitoring stop immediately when I lock the screen.

Once I unlock the screen, it starts working again and successfully sends the “A beacon is nearby” notification.

Additionally, if the app was in the foreground when the lock button was hit, ranging and monitoring do not start even after the unlock. I needed to go back to home screen that triggered an “onPause” event which started the beacon scanning again. If you search “onPause” in the logs of “locked_to_unlocked_logs” file, you will see how it triggered the scanning.

After that, you can check the “unlocked_to_locked_logs” file and search “Using a non-empty scan filter since this is 14.0 or Samsung 8.1+” logs to see how it stopped scanning.

I tested these with an OnePlus 7T (Android 12) and it just works as expected after locking the screen. Since it never stops working, I don’t need to trigger an onPause event when I unlocked.

  • iBeacon layout, manufacturer code, and region configuration are set successfully, as the other test device works with no issues proving that. Samsung also works as expected when the screen is unlocked.
  • The version of the library is set to 2.20.4
  • Applied all the settings suggested in https://dontkillmyapp.com/samsung

I hope this is clear, if it’s not or you need more logs not filtered application level, I can provide them too. The issue is easy to reproduce.


Edit: I found a solution for my case. I found this comment by @davidgyoung in the CycledLeScannerForLollipop.java - startScan() method:
// 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.
And then, I changed the manufacturerCode to Apple one(0x004c) instead of the actual manufacturer's one, and now it can scan iBeacons on my Samsung device even if the screen is off.
Maybe this is how it should be -- setting Apple manufacturer code for iBeacon scanning even though you have the iBeacon from a different manufacturer, I don't know, never seen it in the documentation. Maybe this helps someone

locked_to_unlocked_logs.txt
unlocked_to_locked_logs.txt
locked_logs.txt

@tomerpetel
Copy link

Seeing the same issue on
14, UP1A.231005.007, 22101316UG, Xiaomi
Running version 2.20.4

@RajatSinghBisht
Copy link

I have merged the change to fix this for Android 14. This does not address the problem for Motorola Android 13 as reported by @akoskuczi-bw. Does anybody else on this thread have a Motorola device with Android 13 to confirm the problem there?

If we can get a confirmation of this on a second Motorola Android 13 device, I will make a new change that creates a scan filter for those devices, too. (I am reluctant to do this without confirmation since it could be a breaking change for apps that have been running on Motorola phones for the past year.) Until this is resolved I will leave this issue open.

hi @davidgyoung @akoskuczi-bw , i face the same issue in Motorola edge40, Samsung F12. Please consider fixing this issue for these devices.
I am using the same flutter library to access iBeacon.

@thirumoorthithiru
Copy link

thirumoorthithiru commented Jun 20, 2024

Hi @davidgyoung,

Recently, I've been experiencing an issue with the Android version 14 on the OnePlus Nord CE4 device not receiving packets after the screen locks. After updating the below code fix, it started capturing packets.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
                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 14.0 or Samsung 8.1+");
                    filters = new ScanFilterUtils().createScanFiltersForBeaconParsers(
                            mBeaconManager.getBeaconParsers());
                }

However, I've noticed that the number of packets captured in the screen lock state is less compared to when the screen is on."

@devirtua
Copy link

I am using Realme GT2, Android 14.
Same problem appears.

@alexmicleaacc
Copy link

I have merged the change to fix this for Android 14. This does not address the problem for Motorola Android 13 as reported by @akoskuczi-bw. Does anybody else on this thread have a Motorola device with Android 13 to confirm the problem there?

If we can get a confirmation of this on a second Motorola Android 13 device, I will make a new change that creates a scan filter for those devices, too. (I am reluctant to do this without confirmation since it could be a breaking change for apps that have been running on Motorola phones for the past year.) Until this is resolved I will leave this issue open.

@davidgyoung I can confirm this for Motorola g42, Android 13.
The reference app does not detect any beacon immediately after putting the app in background mode, and resumes detection when the screen is on.
I've downloaded the library locally, added (Build.MANUFACTURER.equalsIgnoreCase("motorla") to the code and the reference app is now working as expected when in background mode.

I can create a pull request with the fix as well, just let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants