I have noticed that the AltBeacon library takes a while (10-15 seconds) to detect the beacon in case the app is paused and resumed multiple times.
I've recreated the error by writing a sample app in the simplest possible way. Here's the code.
Application class
class App : Application() {
override fun onCreate() {
super.onCreate()
appContext = applicationContext
// iBeacon layout
BeaconManager.getInstanceForApplication(applicationContext).beaconParsers.add(BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"))
// App lifecycle observer for debugging purpose
ProcessLifecycleOwner
.get()
.lifecycle
.addObserver(object : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
Log.d("BeaconScanner", "App.onStart")
}
override fun onStop(owner: LifecycleOwner) {
Log.d("BeaconScanner", "App.onStop")
}
})
}
companion object {
lateinit var appContext: Context
private set
}
}
Main Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Set the range notifier
val beaconManager: BeaconManager = BeaconManager.getInstanceForApplication(App.appContext)
beaconManager.addRangeNotifier { beacons, region ->
Log.d("BeaconScanner", "Beacons detected: ${beacons.size}")
}
// Start ranging my beacon
val id = "df4ec3a8-5380-4fc0-a217-ae2200c173dc"
val uuid = Identifier.parse("4f0183dd-7e3e-452e-bf91-b3492b0bbf1c")
val major = Identifier.parse("12345")
val minor = Identifier.parse("54321")
val region = Region(id, uuid, major, minor)
beaconManager.startRangingBeacons(region)
}
}
Logcat output while testing
Some additional info
- There's no permissions code, since it's a test project I simply enabled them manually in the Settings
- If the app is killed, everything is working as expected. Problem appears when app is put in background and then later resumed
- Often, the app will stop detecting the beacon after first pause/resume. It seems to be random. Often it will survive multiple pauses.
- I am testing on Google Pixel 4, Android 12. But I have gotten reports from multiple users that the beacon detection is inconsistent like this. Two specific reports came from users using Samsung 21 Ultra and Huawei P30 PRO.
Logcat output in debug mode
2022-01-19 17:49:27.814 8566-8566/com.mhcin.beacontest D/BeaconScanner: App.onStart
2022-01-19 17:49:27.827 8566-8566/com.mhcin.beacontest I/ScanJob: Using immediateScanJobId from manifest: 208352939
2022-01-19 17:49:27.828 8566-8566/com.mhcin.beacontest I/ScanJob: Using periodicScanJobId from manifest: 208352940
2022-01-19 17:49:27.828 8566-8566/com.mhcin.beacontest W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
2022-01-19 17:49:27.828 8566-8566/com.mhcin.beacontest W/JobInfo: Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
2022-01-19 17:49:27.851 8566-8668/com.mhcin.beacontest I/OpenGLRenderer: Davey! duration=11728ms; Flags=1, FrameTimelineVsyncId=1097496, IntendedVsync=19424053119697, Vsync=19424064230808, InputEventId=2106735827, HandleInputStart=19424067797913, AnimationStart=19424067801143, PerformTraversalsStart=19424067802497, DrawStart=19424079987394, FrameDeadline=19424091841919, FrameInterval=19424067788434, FrameStartTime=11111111, SyncQueued=19424080709842, SyncStart=19424080762602, IssueDrawCommandsStart=19424080971769, SwapBuffers=19424081612394, FrameCompleted=19435781611320, DequeueBufferDuration=9896, QueueBufferDuration=430260, GpuCompleted=19435781611320, SwapBuffersCompleted=19424082285259, DisplayPresentTime=0,
2022-01-19 17:49:27.885 8566-8566/com.mhcin.beacontest I/ScanJob: ScanJob Lifecycle START: org.altbeacon.beacon.service.ScanJob@5ff449f
2022-01-19 17:49:27.890 8566-8670/com.mhcin.beacontest I/CycledLeScanner: Using Android O scanner
2022-01-19 17:49:27.893 8566-8670/com.mhcin.beacontest I/ScanJob: Using immediateScanJobId from manifest: 208352939
2022-01-19 17:49:27.893 8566-8670/com.mhcin.beacontest I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@5ff449f
2022-01-19 17:49:27.893 8566-8670/com.mhcin.beacontest I/ScanJob: scanJob version 2.19.2 is starting up on the main process
2022-01-19 17:49:27.893 8566-8670/com.mhcin.beacontest W/ModelSpecificDistanceCalculator: App has no android.permission.INTERNET permission. Cannot check for distance model updates
2022-01-19 17:49:27.896 8566-8670/com.mhcin.beacontest W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
2022-01-19 17:49:27.896 8566-8670/com.mhcin.beacontest W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
2022-01-19 17:49:27.896 8566-8670/com.mhcin.beacontest D/BluetoothAdapter: isLeEnabled(): ON
2022-01-19 17:49:27.899 8566-8670/com.mhcin.beacontest I/ScanJob: Scan job running for 300000 millis
2022-01-19 17:49:27.899 8566-8671/com.mhcin.beacontest D/BluetoothAdapter: isLeEnabled(): ON
2022-01-19 17:49:27.901 8566-8668/com.mhcin.beacontest D/BluetoothLeScanner: onScannerRegistered() - status=6 scannerId=-1 mScannerId=0
2022-01-19 17:49:29.002 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:30.107 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:31.211 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:32.315 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:33.418 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:34.523 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:34.525 8566-8671/com.mhcin.beacontest D/BluetoothAdapter: isLeEnabled(): ON
2022-01-19 17:49:34.528 8566-8668/com.mhcin.beacontest D/BluetoothLeScanner: onScannerRegistered() - status=6 scannerId=-1 mScannerId=0
2022-01-19 17:49:35.628 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:36.731 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:37.835 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:38.938 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:40.043 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:41.147 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:41.149 8566-8671/com.mhcin.beacontest D/BluetoothAdapter: isLeEnabled(): ON
2022-01-19 17:49:41.152 8566-8668/com.mhcin.beacontest D/BluetoothLeScanner: onScannerRegistered() - status=6 scannerId=-1 mScannerId=0
2022-01-19 17:49:42.252 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:43.356 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:44.460 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:45.562 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:46.665 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:47.771 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 0
2022-01-19 17:49:47.772 8566-8671/com.mhcin.beacontest D/BluetoothAdapter: isLeEnabled(): ON
2022-01-19 17:49:47.772 8566-8671/com.mhcin.beacontest D/BluetoothLeScanner: could not find callback wrapper
2022-01-19 17:49:47.773 8566-8671/com.mhcin.beacontest D/BluetoothAdapter: isLeEnabled(): ON
2022-01-19 17:49:47.779 8566-8668/com.mhcin.beacontest D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=9 mScannerId=0
2022-01-19 17:49:47.821 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.839 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.843 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.843 8566-8677/com.mhcin.beacontest I/ScanHelper: Non-distinct packets detected in a single scan. Restarting scans unecessary.
2022-01-19 17:49:47.878 8566-8679/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.878 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.898 8566-8680/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.899 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.934 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.963 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.965 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.998 8566-8681/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:47.998 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.023 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.026 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.062 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.062 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.091 8566-8681/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.093 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.119 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.122 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.153 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.155 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.186 8566-8679/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.186 8566-8681/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.212 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.215 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.247 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.247 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.273 8566-8679/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.278 8566-8681/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.303 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.308 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.333 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.337 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.369 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.369 8566-8679/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.402 8566-8680/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.403 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.461 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.463 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.496 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.524 8566-8679/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.528 8566-8680/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.557 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.562 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.588 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.596 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.618 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.626 8566-8681/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.650 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.682 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.685 8566-8682/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.710 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.715 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.741 8566-8680/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.746 8566-8678/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.775 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.775 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.805 8566-8677/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.809 8566-8676/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.843 8566-8680/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.844 8566-8681/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.870 8566-8674/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.874 8566-8675/com.mhcin.beacontest D/BeaconParser: Cannot read device name without Manifest.permission.BLUETOOTH_CONNECT
2022-01-19 17:49:48.875 8566-8566/com.mhcin.beacontest D/BeaconScanner: Beacons detected: 1
More info
Here's some more Logcat dump that should contain logs of BackgroundPowerSaver as well: https://pastebin.com/69ksApq1
I should also say that this seems to be consistently reproducible for me. It always happens after exactly 3rd time the app is paused and restarted.

Initialize the variable
When required to scan
Call stop scan in onDestroy either from activity, service or in which context you have been used.