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

Do we have sample app to implement the library including the operation running in Background #1180

Open
rahul-kumawat-vts opened this issue Feb 19, 2024 · 4 comments

Comments

@rahul-kumawat-vts
Copy link

rahul-kumawat-vts commented Feb 19, 2024

@davidgyoung
I have checked in provided example everything is working in foreground state but when i moved the app either in background or removed from Background beacon detection is not working.

https://github.com/davidgyoung/android-beacon-library-reference-kotlin

@rahul-kumawat-vts rahul-kumawat-vts changed the title Do we have sample app to implement the library ? Do we have sample app to implement the library including the operation running in Background Feb 19, 2024
@rahul-kumawat-vts
Copy link
Author

My requirement is i have to detect the beacon in each state either App in background, App is removed from Background and App is running in foreground.

@davidgyoung
Copy link
Member

The library supports a number of different "Scan strategies" that can be used to optimize background detections depending on your use case. All of these scan strategies are shown in the Application class of the reference app you mention above (all but one are commented out, but the comments provide instructions on configuring the other scan strategies.)

See this table for the pros and cons of each scan strategy:

https://altbeacon.github.io/android-beacon-library/detection_times.html

For your purpose, you probably want to consider the Foreground Service scan strategy or the Intent scan strategy. They are designed to give you the best results for background detections within the limits allowed by the Android operating system. Neither are perfect, but one should get you as close as possible to your goal.

@rahul-kumawat-vts
Copy link
Author

rahul-kumawat-vts commented Feb 25, 2024

@davidgyoung Which function is used to stop library foreground service from our app?

In our app have a stop button with Notification for foreground service.

@davidgyoung
Copy link
Member

Thy this to stop a previously configured foreground service:

fun stopForegroundService(context: Context) {
        val beaconManager = BeaconManager.getInstanceForApplication(context)
        for (region in beaconManager.monitoredRegions) {
            beaconManager.stopMonitoring(region)
        }
        for (region in beaconManager.rangedRegions) {
            beaconManager.stopRangingBeacons(region)
        }
        BeaconManager.getInstanceForApplication(this).disableForegroundServiceScanning()
    }

The last line disables the foreground service configuration. If you want to restart the foreground service later, you can omit that line -- that way the next time you start ranging or monitoring it will start up again.

Be a advised that Android 13 and 14 only allow starting a foreground service when the app is in the foreground (or other specific situations like on BOOT_COMPLETED).

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

2 participants