Skip to content

Commit

Permalink
Android internal GPS : force "Full GNSS Measurements" if available
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotl committed Apr 8, 2024
1 parent a066a2f commit 60c06b1
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions android/src/org/LK8000/InternalGPS.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ of the License, or (at your option) any later version.
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.GnssMeasurementRequest;
import android.location.GnssMeasurementsEvent;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
Expand All @@ -37,6 +39,8 @@ of the License, or (at your option) any later version.
import android.provider.Settings;
import android.util.Log;

import java.util.concurrent.Executor;

/**
* Code to support the internal GPS receiver via #LocationManager.
*/
Expand Down Expand Up @@ -97,25 +101,18 @@ public static void Initialize() {

private GpsStatus.NmeaListener listener = null;
private OnNmeaMessageListener listener_N = null;
private GnssMeasurementsEvent.Callback measurementsCallback = null;


/**
* Called by the #Handler, indirectly by update(). Updates the
* LocationManager subscription inside the main thread.
*/
@SuppressLint("MissingPermission")
@Override public void run() {
Log.d(TAG, "Updating GPS subscription...");
locationManager.removeUpdates(this);

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
if(listener != null) {
locationManager.removeNmeaListener(listener);
}
} else {
if(listener_N != null) {
locationManager.removeNmeaListener(listener_N);
}
}

unregisterCallback();

if (locationProvider != null) {
Log.d(TAG, "Subscribing to GPS updates.");
Expand All @@ -130,6 +127,23 @@ public static void Initialize() {
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Request "force full GNSS measurements" explicitly (on <= Android R this is a manual developer setting)
measurementsCallback = new GnssMeasurementsEvent.Callback() {
@Override
public void onGnssMeasurementsReceived(GnssMeasurementsEvent eventArgs) {
super.onGnssMeasurementsReceived(eventArgs);
}

@Override
public void onStatusChanged(int status) {
super.onStatusChanged(status);
}
};
GnssMeasurementRequest request = new GnssMeasurementRequest.Builder().setFullTracking(true).build();
locationManager.registerGnssMeasurementsCallback(request, command -> { }, measurementsCallback);
}

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
if(listener == null) {
listener = (timestamp, nmea) -> InternalGPS.this.parseNMEA(nmea);
Expand Down Expand Up @@ -172,7 +186,26 @@ private void setLocationProvider(String _locationProvider) {
update();
}

private void unregisterCallback() {
locationManager.removeUpdates(this);

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
if(listener != null) {
locationManager.removeNmeaListener(listener);
}
} else {
if(listener_N != null) {
locationManager.removeNmeaListener(listener_N);
}
if (measurementsCallback != null) {
locationManager.unregisterGnssMeasurementsCallback(measurementsCallback);
}
}
}

public void close() {
unregisterCallback();

safeDestruct.beginShutdown();
setLocationProvider(null);
safeDestruct.finishShutdown();
Expand Down

0 comments on commit 60c06b1

Please sign in to comment.