From fa5e91f96645f34bedf28979f77d20f9d5a68826 Mon Sep 17 00:00:00 2001 From: illis Date: Wed, 30 Aug 2023 21:31:21 +1200 Subject: [PATCH] Fix GPS getSpeed issue for devices that report hasSpeed as true, yet only return a 0 value for speed. --- .../gadgetbridge/externalevents/gps/GBLocationListener.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/gps/GBLocationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/gps/GBLocationListener.java index 76581d94d..c77672057 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/gps/GBLocationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/gps/GBLocationListener.java @@ -36,6 +36,8 @@ public class GBLocationListener implements LocationListener { private final EventHandler eventHandler; private Location previousLocation; + // divide by 3.6 to get km/h to m/s + private static final double SPEED_THRESHOLD = 1.0 / 3.6; public GBLocationListener(final EventHandler eventHandler) { this.eventHandler = eventHandler; @@ -49,7 +51,9 @@ public class GBLocationListener implements LocationListener { location.setTime(getLocationTimestamp(location)); // The location usually doesn't contain speed, compute it from the previous location - if (previousLocation != null && !location.hasSpeed()) { + // Some devices report hasSpeed() as true, and yet only return a 0 value, so we have to check against a speed threshold + boolean hasValidSpeed = location.hasSpeed() && (location.getSpeed() > SPEED_THRESHOLD); + if (previousLocation != null && !hasValidSpeed) { long timeInterval = (location.getTime() - previousLocation.getTime()) / 1000L; float distanceInMeters = previousLocation.distanceTo(location); location.setSpeed(distanceInMeters / timeInterval);