AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.84k stars 836 forks source link

getting IllegalStateException: schedule()/enqueue() called more than 250 times in the past 60000ms on android 12+ #1111

Closed octoberv29 closed 1 year ago

octoberv29 commented 1 year ago

Expected behavior

not getting IllegalStateException when using the library

Actual behavior

after calling startMonitoring(region) I'm getting IllegalStateException: schedule()/enqueue() called more than 250 times in the past 60000ms

this started after updating to 2.1.9 version and updating app's targetSdkVersion to 31

crash log:

Caused by: java.lang.IllegalStateException: schedule()/enqueue() called more than 250 times in the past 60000ms. See the documentation for more information.
at
android.os.Parcel.createExceptionOrNull(Parcel.java:2434)
at
android.os.Parcel.createException(Parcel.java:2410)
at
android.os.Parcel.readException(Parcel.java:2393)
at
android.os.Parcel.readException(Parcel.java:2335)
at
android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:312)
at
android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:47)
at
org.altbeacon.beacon.service.ScanJobScheduler.schedule(ScanJobScheduler.java:205)
at
org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(ScanJobScheduler.java:96)
at
org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(ScanJobScheduler.java:103)
at
org.altbeacon.beacon.BeaconManager.applyChangesToServices(BeaconManager.java:1251)
at
org.altbeacon.beacon.BeaconManager.startMonitoringBeaconsInRegion(BeaconManager.java:1097)
at
org.altbeacon.beacon.BeaconManager$1.onBeaconServiceConnect(BeaconManager.java:1783)
at
org.altbeacon.beacon.BeaconManager.bindInternal(BeaconManager.java:425)
at
org.altbeacon.beacon.BeaconManager.autoBind(BeaconManager.java:1808)
at
org.altbeacon.beacon.BeaconManager.startMonitoring(BeaconManager.java:1136)

another variant of crash log:

java.lang.RuntimeException: Unable to pause activity {com.endclothing.endroid/com.endclothing.endroid.activities.cms.CmsActivity}: java.lang.IllegalStateException: schedule()/enqueue() called more than 250 times in the past 60000ms. See the documentation for more information.
at
android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:5109)
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:5060)
at
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:5012)
at
android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:47)
at
android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at
android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at
android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
at
android.os.Handler.dispatchMessage(Handler.java:106)
at
android.os.Looper.loopOnce(Looper.java:201)
at
android.os.Looper.loop(Looper.java:288)
at
android.app.ActivityThread.main(ActivityThread.java:7870)
at
java.lang.reflect.Method.invoke(Native Method:-2)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Caused by: java.lang.IllegalStateException: schedule()/enqueue() called more than 250 times in the past 60000ms. See the documentation for more information.
at
android.os.Parcel.createExceptionOrNull(Parcel.java:2434)
at
android.os.Parcel.createException(Parcel.java:2410)
at
android.os.Parcel.readException(Parcel.java:2393)
at
android.os.Parcel.readException(Parcel.java:2335)
at
android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:312)
at
android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:47)
at
org.altbeacon.beacon.service.ScanJobScheduler.schedule(SourceFile:17)
at
org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(SourceFile:5)
at
org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(SourceFile:9)
at
org.altbeacon.beacon.BeaconManager.applyChangesToServices(SourceFile:7)
at
org.altbeacon.beacon.BeaconManager.updateScanPeriods(SourceFile:7)
at
org.altbeacon.beacon.BeaconManager.setBackgroundModeInternal(SourceFile:8)
at
org.altbeacon.beacon.BeaconManager.setBackgroundMode(SourceFile:1)
at
org.altbeacon.beacon.powersave.BackgroundPowerSaverInternal.onActivityPaused(SourceFile:6)
at
android.app.Application.dispatchActivityPaused(Application.java:453)
at
android.app.Activity.dispatchActivityPaused(Activity.java:1430)
at
android.app.Activity.onPause(Activity.java:2387)
at
androidx.core.app.ComponentActivity.onPause(Unknown Source:3)
at
androidx.activity.ComponentActivity.onPause(Unknown Source:3)
at
androidx.fragment.app.FragmentActivity.onPause(SourceFile:1)
at
androidx.appcompat.app.AppCompatActivity.onPause(Unknown Source:3)

Steps to reproduce this behavior

I start scanning for beacons on Android 12 device (Google Pixel 6), then I background an app, then come back to it, do a couple of clicks in the UI and get this IllegalStateException.

Mobile device model and OS version

Android 12, Google Pixel 6

Android Beacon Library version

2.1.9

IMPORTANT: This forum is reserved for feature requests or reproducible bugs with the library itself. If you need help with using the library with your project, please open a new question on StackOverflow.com.

davidgyoung commented 1 year ago

The stack trace starts with this API method: BeaconManager.startMonitoring(...). Can you please show the code that calls this method in your app? How is this code triggered? It seems something may be calling it over 250 times in 60 seconds... this method is not designed to be called that frequently.

I suspect this is a problem with the way the method is called within your app, as I do not believe this happens with the library's reference application. If you can reproduce this problem with the library reference app please say so.

octoberv29 commented 1 year ago

Hi, sorry, we have finally found an issue. Yeah, it was on our side.

After update to Target API = 31, we only added a check for bluetooth permissions and we thought that the error came from beacon library, because we couldn't find an error for almost a week. But yesterday we found that we forgot to add distinctUntilChanged() operator to behaviourSubject that was responsible for posting changes in bluetooth permissions.

Thus, previously we would make a call every time the subject is updated, even if it's the same value, but we wanted to only make a call when we receive a value (true) different to a previous one. Hope this helps to anyone else facing the same issue.

btw, thanks for quick response!

davidgyoung commented 1 year ago

Great! Good to hear you found the issue