Skip to content

Commit 6b34beb

Browse files
NMEA Updates (#1490)
* MSL Altitude improvements * Fix compilation problems * Always listen to NMEA message to get satellite fix * Handle nullpointer edgec ases * Formatting fixes * fix use msl extra key * Add change log info --------- Co-authored-by: Maurits van Beusekom <maurits@baseflow.com>
1 parent 1bf9103 commit 6b34beb

File tree

7 files changed

+45
-15
lines changed

7 files changed

+45
-15
lines changed

geolocator_android/CHANGELOG.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 4.6.0
2+
3+
* Adds support to read NMEA messages from other satellites constellations.
4+
* Read the MSL altitude directly from the Android location starting with API 34.
5+
* Always listen to NMEA message to get GPS position fix data.
6+
* Updates `com.google.android.gms:play-services-location` to version `21.2.0`.
7+
* Updates `androidx.core:core` to version `1.13.0`.
8+
19
## 4.5.5
210

311
* Fixes a bug where location stream is not automatically started when enabling the location services.
@@ -34,22 +42,22 @@
3442

3543
## 4.4.0
3644

37-
- Adds `color` to `ForegroundNotificationConfig` to set the color of the notification icon.
45+
* Adds `color` to `ForegroundNotificationConfig` to set the color of the notification icon.
3846

3947
## 4.3.3
4048

41-
- Removes deprecated support for Android V1 embedding from the JAVA code base. Note that the geolocator's Flutter version restrictions already don't support V1 embedding anymore.
42-
- Registers location services state change broadcast receiver with required exported flags
49+
* Removes deprecated support for Android V1 embedding from the JAVA code base. Note that the geolocator's Flutter version restrictions already don't support V1 embedding anymore.
50+
* Registers location services state change broadcast receiver with required exported flags
4351

4452
## 4.3.2
4553

46-
- Updates the following dependencies:
47-
- uuid package from ^3.0.7 to ^4.1.0
48-
- flutter_lints from ^2.0.0 to ^3.0.0
54+
* Updates the following dependencies:
55+
* uuid package from ^3.0.7 to ^4.1.0
56+
* flutter_lints from ^2.0.0 to ^3.0.0
4957

5058
## 4.3.1
5159

52-
- Suppresses a deprecation warning for `LocationListenerCompat.onStatusChanged` when building for Android.
60+
* Suppresses a deprecation warning for `LocationListenerCompat.onStatusChanged` when building for Android.
5361

5462
## 4.3.0
5563

geolocator_android/android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ android {
4141
}
4242

4343
dependencies {
44-
implementation 'com.google.android.gms:play-services-location:21.0.1'
45-
implementation 'androidx.core:core:1.9.0'
44+
implementation 'com.google.android.gms:play-services-location:21.2.0'
45+
implementation 'androidx.core:core:1.13.0'
4646

4747
testImplementation 'junit:junit:4.13.2'
4848
testImplementation 'org.mockito:mockito-core:5.1.1'

geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.IntentSender;
77
import android.location.Location;
88
import android.os.Build;
9+
import android.os.Bundle;
910
import android.os.Looper;
1011
import android.util.Log;
1112

@@ -67,6 +68,16 @@ public synchronized void onLocationResult(@NonNull LocationResult locationResult
6768
}
6869

6970
Location location = locationResult.getLastLocation();
71+
if (location == null) {
72+
return;
73+
}
74+
if (location.getExtras() == null) {
75+
location.setExtras(Bundle.EMPTY);
76+
}
77+
if (locationOptions != null) {
78+
location.getExtras().putBoolean(LocationOptions.USE_MSL_ALTITUDE_EXTRA, locationOptions.isUseMSLAltitude());
79+
}
80+
7081
nmeaClient.enrichExtrasWithNmea(location);
7182
positionChangedCallback.onPositionChanged(location);
7283
}

geolocator_android/android/src/main/java/com/baseflow/geolocator/location/LocationMapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public static Map<String, Object> toHashMap(Location location) {
4545
location.getExtras().getDouble(NmeaClient.GNSS_SATELLITES_USED_IN_FIX_EXTRA);
4646
position.put("gnss_satellites_used_in_fix", mslSatellitesUsedInFix);
4747
}
48+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && location.hasMslAltitude())
49+
{
50+
double mslAltitude = location.getMslAltitudeMeters();
51+
position.put("altitude", mslAltitude);
52+
if (location.hasMslAltitudeAccuracy()) {
53+
float mslAccuracy = location.getMslAltitudeAccuracyMeters();
54+
position.put("altitude_accuracy", mslAccuracy);
55+
}
56+
}
4857
}
4958
return position;
5059
}

geolocator_android/android/src/main/java/com/baseflow/geolocator/location/LocationOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Map;
44

55
public class LocationOptions {
6+
public static final String USE_MSL_ALTITUDE_EXTRA = "geolocator_use_mslAltitude";
7+
68
private final LocationAccuracy accuracy;
79
private final long distanceFilter;
810
private final long timeInterval;

geolocator_android/android/src/main/java/com/baseflow/geolocator/location/NmeaClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public NmeaClient(@NonNull Context context, @Nullable LocationOptions locationOp
4444
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
4545
nmeaMessageListener =
4646
(message, timestamp) -> {
47-
if (message.startsWith("$GPGGA")) {
47+
if (message.trim().matches("^\\$..GGA.*$")) {
4848
lastNmeaMessage = message;
4949
lastNmeaMessageTime = Calendar.getInstance();
5050
}
@@ -71,7 +71,7 @@ public void start() {
7171
return;
7272
}
7373

74-
if (locationOptions != null && locationOptions.isUseMSLAltitude()) {
74+
if (locationOptions != null) {
7575
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locationManager != null) {
7676
locationManager.addNmeaListener(nmeaMessageListener, null);
7777
locationManager.registerGnssStatusCallback(gnssCallback, null);
@@ -81,7 +81,7 @@ public void start() {
8181
}
8282

8383
public void stop() {
84-
if (locationOptions != null && locationOptions.isUseMSLAltitude()) {
84+
if (locationOptions != null) {
8585
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && locationManager != null) {
8686
locationManager.removeNmeaListener(nmeaMessageListener);
8787
locationManager.unregisterGnssStatusCallback(gnssCallback);
@@ -117,7 +117,7 @@ public void enrichExtrasWithNmea(@Nullable Location location) {
117117

118118
// Parse altitude above sea level, Detailed description of NMEA string here
119119
// http://aprs.gids.nl/nmea/#gga
120-
if (type.startsWith("$GPGGA") && tokens.length > 9) {
120+
if (lastNmeaMessage.trim().matches("^\\$..GGA.*$") && tokens.length > 9) {
121121
if (!tokens[9].isEmpty()) {
122122
double mslAltitude = Double.parseDouble(tokens[9]);
123123
if (location.getExtras() == null) {

geolocator_android/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: geolocator_android
22
description: Geolocation plugin for Flutter. This plugin provides the Android implementation for the geolocator.
33
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_android
44
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
5-
version: 4.5.5
5+
version: 4.6.0
66

77
environment:
88
sdk: ">=2.15.0 <4.0.0"
@@ -22,7 +22,7 @@ dependencies:
2222
sdk: flutter
2323
geolocator_platform_interface: ^4.1.0
2424
meta: ^1.10.0
25-
uuid: ^4.1.0
25+
uuid: ">=4.0.0 <6.0.0"
2626

2727
dev_dependencies:
2828
async: ^2.8.2

0 commit comments

Comments
 (0)