AltBeacon / android-beacon-library

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

ScanJob restarts scanning every time on navigation #1194

Open VolodaUa opened 1 month ago

VolodaUa commented 1 month ago

Expected behavior

The scan job should not restart every time once you have next/back navigation

Actual behavior

The scan job restarts scanning every time when another activity is opened. As a result, the app gets the error: "App 'com.example.myapplication' is scanning too frequently" in the log cat and scanning doesn't work for some time.

class MyApplication : Application(), MonitorNotifier { override fun onCreate() { super.onCreate() val beaconManager = BeaconManager.getInstanceForApplication(this) beaconManager.addMonitorNotifier(this) beaconManager.startMonitoring(region) } ... }

From documentation: There are subtle changes with the initial background state when using autobind. If you use autobind methods to start ranging or monitoring from Application.onCreate or any method in its call stack, then the library will **start scanning in background mode**. If you initiate scanning at any other time (from a custom service or from an activity or fragment) then the library will start out in foreground mode provided that the screen is on. Previously, the library always started out in foreground mode unless using RegionBootstrap, in which case it started out in background mode.

As I understood correctly, it should perform background scanning if I have starting of monitoring beacons in the Application.onCreate method. and should not restart scanning every time on navigation. Looks like it doen't work.

Steps to reproduce this behavior

1) Launch the sample app 2) Close the app, go to settings and grant all the permissions; 3) Open the app again; 3) See the Job started scanning 4) See logs Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@a1bb557 scanJob version 2.20.2 is starting up on the main process 5) Open next activity by button;

Mobile device model and OS version

Google Pixel 7 Pro, Android 14

Android Beacon Library version 2.20.6

Sample: BeaconsApplication.zip

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.

088 ScanJob com.example.myapplication I Using periodicScanJobId from manifest: 208352940 2024-05-28 09:59:02.297 24088-24088 ScanJob com.example.myapplication I ScanJob Lifecycle START: org.altbeacon.beacon.service.ScanJob@ecc2771 2024-05-28 09:59:02.365 24088-24112 ScanHelper com.example.myapplication D BeaconParsers set to count: 1 2024-05-28 09:59:02.365 24088-24112 ScanHelper com.example.myapplication D First parser layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25 2024-05-28 09:59:02.367 24088-24112 CycledLeScanner com.example.myapplication I Using Android O scanner 2024-05-28 09:59:02.376 24088-24112 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:02.378 24088-24112 ScanJob com.example.myapplication I Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@ecc2771 2024-05-28 09:59:02.381 24088-24112 ScanJob com.example.myapplication I scanJob version 2.20.6 is starting up on the main process 2024-05-28 09:59:02.506 24088-24112 ScanJob com.example.myapplication I Scan job running for 10000 millis 2024-05-28 09:59:02.511 24088-24104 BluetoothLeScanner com.example.myapplication D onScannerRegistered() - status=0 scannerId=8 mScannerId=0 2024-05-28 09:59:12.511 24088-24088 ScanJob com.example.myapplication I Scan job runtime expired: org.altbeacon.beacon.service.ScanJob@ecc2771 2024-05-28 09:59:18.555 24145-24145 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:18.557 24145-24145 ScanJob com.example.myapplication I Using periodicScanJobId from manifest: 208352940 2024-05-28 09:59:18.675 24145-24145 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:18.676 24145-24145 ScanJob com.example.myapplication I Using periodicScanJobId from manifest: 208352940 2024-05-28 09:59:18.757 24145-24145 ScanJob com.example.myapplication I ScanJob Lifecycle START: org.altbeacon.beacon.service.ScanJob@a1bb557 2024-05-28 09:59:18.770 24145-24192 ScanHelper com.example.myapplication D BeaconParsers set to count: 1 2024-05-28 09:59:18.771 24145-24192 ScanHelper com.example.myapplication D First parser layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25 2024-05-28 09:59:18.771 24145-24192 CycledLeScanner com.example.myapplication I Using Android O scanner 2024-05-28 09:59:18.774 24145-24192 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:18.774 24145-24192 ScanJob com.example.myapplication I Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@a1bb557 2024-05-28 09:59:18.774 24145-24192 ScanJob com.example.myapplication I scanJob version 2.20.2 is starting up on the main process 2024-05-28 09:59:18.805 24145-24192 ScanJob com.example.myapplication I Scan job running for 300000 millis 2024-05-28 09:59:18.813 24145-24160 BluetoothLeScanner com.example.myapplication D onScannerRegistered() - status=0 scannerId=8 mScannerId=0 2024-05-28 09:59:25.425 24145-24145 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:25.426 24145-24145 ScanJob com.example.myapplication I Using periodicScanJobId from manifest: 208352940 2024-05-28 09:59:25.434 24145-24145 ScanJob com.example.myapplication I Using periodicScanJobId from manifest: 208352940 2024-05-28 09:59:25.434 24145-24145 ScanJob com.example.myapplication I onStopJob called for immediate scan org.altbeacon.beacon.service.ScanJob@a1bb557 2024-05-28 09:59:25.434 24145-24145 ScanJob com.example.myapplication I ScanJob Lifecycle STOP: org.altbeacon.beacon.service.ScanJob@a1bb557 2024-05-28 09:59:25.442 24145-24193 BluetoothLeScanner com.example.myapplication D could not find callback wrapper 2024-05-28 09:59:25.482 24145-24145 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:25.483 24145-24145 ScanJob com.example.myapplication I Using periodicScanJobId from manifest: 208352940 2024-05-28 09:59:25.542 24145-24145 ScanJob com.example.myapplication I ScanJob Lifecycle START: org.altbeacon.beacon.service.ScanJob@3f38418 2024-05-28 09:59:25.578 24145-24224 ScanHelper com.example.myapplication D BeaconParsers set to count: 1 2024-05-28 09:59:25.578 24145-24224 ScanHelper com.example.myapplication D First parser layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25 2024-05-28 09:59:25.578 24145-24224 CycledLeScanner com.example.myapplication I Using Android O scanner 2024-05-28 09:59:25.580 24145-24224 ScanJob com.example.myapplication I Using immediateScanJobId from manifest: 208352939 2024-05-28 09:59:25.580 24145-24224 ScanJob com.example.myapplication I Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@3f38418 2024-05-28 09:59:25.580 24145-24224 ScanJob com.example.myapplication I scanJob version 2.20.2 is starting up on the main process 2024-05-28 09:59:25.596 24145-24224 ScanJob com.example.myapplication I Scan job running for 300000 millis

VolodaUa commented 1 month ago

HI @davidgyoung

Could you please take a look?

We used this library when we had one Activity only(Xamarin) and we didn't observe this issue. We also started using it in the project where we have a couple of activities(native) and we got that app scans too frequently messages in the log cat. I started an investigation and I found that it looks like the app restarts scanning every time when we have navigation. I attached the sample when it reproduces. The sample was built according to documentation when an application is used to create the instance of BeaconManager.

VolodaUa commented 1 month ago

Hi @davidgyoung

I found that BackgroundPowerSaverInternal works wrongly in the sample app.

It always considers navigation like the user puts the app in the background.