3

I have upgrade the Android SDK project 30 to 31. Also i am using the the Android Beacon Library for scanning the the iBeacon.

Android 12 i am getting the frquently below crashes on 100% background application.

Caused by android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.beacon.android/org.altbeacon.beacon.service.BeaconService at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54) at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50) at android.os.Parcel.readParcelable(Parcel.java:3345) at android.os.Parcel.createExceptionOrNull(Parcel.java:2432) at android.os.Parcel.createException(Parcel.java:2421) at android.os.Parcel.readException(Parcel.java:2404) at android.os.Parcel.readException(Parcel.java:2346) at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6897) at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1926) at android.app.ContextImpl.startForegroundService(ContextImpl.java:1892) at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:796) at org.altbeacon.beacon.BeaconManager.bindInternal(BeaconManager.java:438) at org.altbeacon.beacon.BeaconManager.autoBind(BeaconManager.java:1810) at org.altbeacon.beacon.BeaconManager.startMonitoring(BeaconManager.java:1136) at com.beacon.BeaconApplication.onCreate(BeaconApplication.kt:170) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7429) at android.app.ActivityThread.access$1700(ActivityThread.java:309) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2268) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8582) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)

Currently using Altbeacon library below version

implementation 'org.altbeacon:android-beacon-library:2.19.3'

Also, below Android SDK version 30, its working absolutely fine.

Thanks in Advance !

Nitin Karande
  • 1,280
  • 14
  • 33
  • I cannot reproduce these results with the official Kotlin reference application. Please see a branch I made to target SDK 31 here: https://github.com/davidgyoung/android-beacon-library-reference-kotlin/tree/android-12 Please try out this branch and see if it works for you, and if so, check for differences in your setup code with the setup code in the reference application. If you cannot find the problem this way, then please post your setup code. – davidgyoung Feb 07 '22 at 17:26
  • 1
    Please do check that you have this permission in your manifest: ``. This is required for targeting SDK 28 and above – davidgyoung Feb 07 '22 at 17:28
  • @davidgyoung Thanks for quick response . I have already added permission and . Also, I will refer the android-beacon-library-reference-kotlin sample app and monitor for 2-3 days and let you know. – Nitin Karande Feb 08 '22 at 12:00
  • @davidgyoung We are facing issue on the https://github.com/davidgyoung/android-beacon-library-reference-kotlin/tree/android-12 this project , please can you suggest how we can fix this issue. – Nitin Karande Apr 21 '22 at 12:48
  • Please note that the Android Beacon Library has been updated to address this issue. See my answer for details. – davidgyoung Jun 25 '22 at 22:58

1 Answers1

1

This is caused by a security change to Android 12. For apps targeting SDK 31, Android 12 now blocks starting a foreground service except in very specific circumstances. Typically, your app must visible on the screen to start a foreground service.

A solution to the specific problem in the Android Beacon Library has been released in a subsequent library version. See the solution discussed here.

See here for a discussion of the bigger picture issue of using Foreground Services

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I'm certain this is a bug in Android. The "very specific circumstances" listed here https://developer.android.com/guide/components/foreground-services#background-start-restriction-exemptions provide exemptions which allow starting a foreground service from the background, however they're being ignored. For example, "Your app receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions." - is that not one of the functions of your library? Google appear to be looking into it: https://issuetracker.google.com/issues/229000935 – warbi Apr 22 '22 at 14:58
  • The problem is that the library starts the Bluetooth scan from inside the foreground service before it ever has a detection. This worked fine on Android 8-11 but not anymore (in the background anyway). You may wish to discuss how this might be changed in the library issue link above. – davidgyoung Apr 22 '22 at 21:52