Automatically enable background mode for Android beacon monitoring #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Android beacon monitoring stopped working when the app moved to background. Android 8+ requires a foreground service for background Bluetooth scanning, but the existing
enableBackgroundMode()method required manual invocation.Changes
Merge with main branch (v8.1.11)
setBackgroundModeEnabled(),applyBackgroundMode(),enableForegroundServiceIfNeeded(), anddisableForegroundServiceIfNeeded()methodsFOREGROUND_CHANNEL_ID,FOREGROUND_NOTIFICATION_ID)Automatic lifecycle management
startMonitoringForRegion()andstartRangingBeaconsInRegion()now automatically enable background mode whenenableBackgroundModeparameter is not providedstopMonitoringForRegion()andstopRangingBeaconsInRegion()automatically disable background mode when no regions remainDocumentation
Backward compatibility
Existing code that explicitly passes
enableBackgroundMode: trueorenableBackgroundMode: falsecontinues to work unchanged. The automatic behavior only activates when the parameter is omitted, making this a non-breaking enhancement.Example
Original prompt
This section details on the original issue you should resolve
<issue_title>Android Background Beacon Monitoring - Two Critical Issues</issue_title>
<issue_description>
🐛 Problem Description
There are two critical issues with Android beacon monitoring that have been tested on both the latest version and version 8.0.2 (most stable). Both issues work perfectly on iOS.
🚨 Issue #1: No Background Beacon Detection
When the Android app goes to background, beacon region monitoring completely stops working. The app appears to be running in background, but the monitoring functionality is non-functional.
🚨 Issue #2: Permission Prompts Don't Work
When requesting permissions using capgo methods (
requestWhenInUseAuthorization(),requestAlwaysAuthorization()), Android Studio shows repeated"Location not available"errors. This appears to be a missing callback implementation in the Android native code.📱 Environment
@capgo/capacitor-ibeacon@8.0.2(also tested on latest version)^7.4.1🔧 Current Setup
Plugins Used for Android Background Support
@capacitor-community/background-geolocation@1.2.26(for maintaining background execution)@capacitor/geolocation@7.1.5(native Capacitor plugin)@capacitor-community/bluetooth-le@7.2.0(for nearby device permissions)@capgo/capacitor-ibeacon@8.0.2🚀 Reproduction Steps
For Issue #1 (Background Detection):
startMonitoringForRegion()for beacon regiondidEnterRegionevents fired (works perfectly on iOS)For Issue #2 (Permission Prompts):
CapacitorIbeacon.requestWhenInUseAuthorization()orrequestAlwaysAuthorization()"Location not available"errors appear (works perfectly on iOS)Expected (iOS behavior):
Actual (Android behavior):
🔍 Error Details
The error
"Location not available"appears to be triggered by a missing callback implementation in the Android native layer of the plugin. This suggests that the Android code is expecting a location callback that is not properly implemented or handled.❓ Questions
Are these known Android-specific issues that are being worked on?
Is there a workaround for the missing callback implementation that causes permission prompt failures?
Why does background monitoring stop working on Android when it works perfectly on iOS?
Should we avoid using capgo permission methods on Android and use alternative approaches?
🔍 Additional Context
💡 Impact
These issues make the plugin unusable for production Android apps that require:
The Android implementation appears to need significant work to match iOS functionality.</issue_description>
<agent_instructions>When I call startMonitoringForRegion() on Android, it should continue monitoring in background just like it does on iOS, triggering didEnterRegion/didExitRegion events even when the app is not in foreground.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@riderx Hello thanks for sending this issues, the number 2 is something we can fix, but the first one is not expected, i dont event know how we can do it as the background geoloc is on is own process. We could make something if you use the background-geoloc we made as we control both @riderx Hey @ifrolyak can you test latest and tell us if that fix your issue ?💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.