Skip to content

Colota - Android GPS Location tracker

feat: send geofence anchor point as heartbeat instead of fresh GPS fix Closes #282

Max Dietricha73648ef02ba
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index 5fca800b..957cb128 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -141,7 +141,7 @@ Each geofence supports three independent GPS pause modes, configured per zone:
- **WiFi pause** - Stops GPS entirely when connected to an unmetered network (WiFi/Ethernet). Implemented via `ConnectivityManager.NetworkCallback`, which fires immediately on network availability changes. An active network counter handles devices with multiple simultaneous unmetered networks - GPS only resumes once all of them are gone, after a short debounce.
- **Motionless pause** - Stops GPS after no device motion is detected for a configurable timeout. Uses the hardware motion sensor (via `MotionDetector`) to resume GPS when movement is detected again.
-A per-zone **stationary heartbeat** can send periodic location updates while paused. It requests a fresh GPS fix, sends it immediately (bypassing sync conditions) and powers GPS back down. Configured via `heartbeatEnabled` and `heartbeatIntervalMinutes` per geofence.
+A per-zone **stationary heartbeat** can send periodic location updates while paused. It sends the geofence center as a synthetic anchor point - no GPS wake required - bypassing sync conditions. Configured via `heartbeatEnabled` and `heartbeatIntervalMinutes` per geofence.
When both WiFi and motionless pause are enabled, GPS only resumes when both conditions clear - WiFi disconnected **and** motion detected. Changes made in the editor take effect immediately even when already inside the zone, via `applyZoneSettingsIfChanged` on the next zone recheck.
diff --git a/apps/docs/docs/guides/geofencing.md b/apps/docs/docs/guides/geofencing.md
index 4790ce01..9ac6a3df 100644
--- a/apps/docs/docs/guides/geofencing.md
+++ b/apps/docs/docs/guides/geofencing.md
@@ -41,7 +41,7 @@ Stops GPS after no device motion is detected for a configurable time (default 10
### Stationary heartbeat
-Sends a periodic location update to your server while paused inside the zone. Useful as a proof-of-presence signal so your backend knows the device is still there. The heartbeat requests a fresh GPS fix, sends it immediately (bypassing normal sync conditions like Wi-Fi only) and then powers GPS back down. Configure the interval in minutes (default 15).
+Sends a periodic location update to your server while paused inside the zone. Useful as a proof-of-presence signal so your backend knows the device is still there. The heartbeat sends the geofence center as a synthetic anchor point (no GPS wake), bypassing normal sync conditions like Wi-Fi only. Configure the interval in minutes (default 15).
### Combined behavior
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
index be3b37e8..f092a61c 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
@@ -824,46 +824,28 @@ class LocationForegroundService : Service() {
heartbeatJob = null
}
- /**
- * Requests a single fresh GPS fix with a 30-second timeout.
- * Falls back to [lastKnownLocation] if no fix arrives in time.
- */
- private suspend fun requestFreshLocation(): android.location.Location? =
- withTimeoutOrNull(30_000L) {
- suspendCancellableCoroutine { cont ->
- val callback = object : LocationUpdateCallback {
- override fun onLocationUpdate(location: android.location.Location) {
- locationProvider.removeLocationUpdates(this)
- if (cont.isActive) cont.resume(location)
- }
- }
- cont.invokeOnCancellation { locationProvider.removeLocationUpdates(callback) }
- locationProvider.requestLocationUpdates(
- intervalMs = 1000L,
- minDistanceMeters = 0f,
- looper = android.os.Looper.getMainLooper(),
- callback = callback
- )
- }
- }
-
private suspend fun sendHeartbeatLocation() {
if (config.endpoint.isBlank() || !networkManager.isNetworkAvailable()) {
AppLogger.d(TAG, "Heartbeat skipped: no endpoint or no network")
return
}
- val location = withContext(Dispatchers.Main) { requestFreshLocation() } ?: lastKnownLocation
-
- if (location == null) {
- AppLogger.d(TAG, "Heartbeat skipped: no location available")
+ val zone = currentZoneGeofence
+ if (zone == null) {
+ AppLogger.d(TAG, "Heartbeat skipped: no current zone")
return
}
+ val location = android.location.Location("geofence").apply {
+ latitude = zone.lat
+ longitude = zone.lon
+ accuracy = zone.radius.toFloat()
+ time = System.currentTimeMillis()
+ }
lastKnownLocation = location
val (battery, batteryStatus) = deviceInfoHelper.getCachedBatteryStatus()
- val timestampSec = System.currentTimeMillis() / 1000
+ val timestampSec = location.time / 1000
val currentFieldMap = fieldMap ?: emptyMap()
val payload = payloadBuilder.buildPayload(