7

I'm using a GPS provider and LocationListener.onLocationChanged(Location location) to receive location fixes.
Documentation says, that Location.getExtras() contains next key/value pair:

satellites - the number of satellites used to derive the fix

but on practice I'm getting an empty extra object - there is no any data there.
Does it means that I'm getting the A-GPS fixes and not GPS?

GetUsername
  • 1,057
  • 5
  • 18
  • 29

4 Answers4

21

To get the number of satellites used by the GPS engine you need to implement android.location.GpsStatus.Listener and implement its method onGpsStatusChanged().

Example...

public void onGpsStatusChanged(int event) {
    int satellites = 0;
    int satellitesInFix = 0;
    int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();
    Log.i(TAG, "Time to first fix = " + timetofix);
    for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {
        if(sat.usedInFix()) {
            satellitesInFix++;              
        }
        satellites++;
    }
    Log.i(TAG, satellites + " Used In Last Fix ("+satellitesInFix+")"); 
}
Gama11
  • 31,714
  • 9
  • 78
  • 100
user1055212
  • 469
  • 4
  • 13
  • I don't understand why I need to implement a listener if I want to poll for the number of satellites... yet trying to call getSatellites() without setting up a listener always returns no satellites, even when a GPS has a fix and is locked. – Michael Jun 23 '17 at 22:59
  • It's very important to add the listener to the location manager like locationManager.addGpsStatusListener(); or else the listener won't be called. – The Berga Nov 27 '17 at 16:49
14

I use Location.getExtras().getInt("satellites"), and it give the number of satellites in use.

Jnherm
  • 149
  • 1
  • 5
6

Since Android API 24 GpsStatus is deprecated and one should use GnssStatus. Let us have an activity or a service processing Gps data and a LocationManager already created.

private GnssStatus.Callback gnssCallback;

public void initCallbacks() {
    ....
    gnssCallback = new GnssStatus.Callback() {
        @Override
        public void onSatelliteStatusChanged(@NonNull GnssStatus status) {
            final int satelliteCount = status.getSatelliteCount();
            int usedCount = 0;
            for (int i = 0; i < satelliteCount; ++i)
                if (status.usedInFix(i))
                    ++usedCount;
            Log.d("MyServiceTag", "satellites count = " + satelliteCount + ", used = " + usedCount);
        }
    };
    locationManager.registerGnssStatusCallback(gnssCallback, new Handler(Looper.myLooper()));
    ....
}

public void deinitCallbacks() {
    ....
    locationManager.unregisterGnssStatusCallback(gnssCallback);
    ....
}

initCallbacks() should be called after locationManager initialization. deinitCallbacks() should be called when information on the number of satellites is no longer needed, e.g. in onDestroy(). GnssStatus.getSatelliteCount() returns total number of known satellites, GnssStatus.usedInFix(int i) tells whether i-th satellite had been used in the most actual location capture.

artaxerx
  • 244
  • 1
  • 3
  • 11
1

Nope it means that your phone manufacturer decided not to implement this. (Or you could be using the NETWORK_PROVIDER which does not use satellites)

Use a NmeaListener and parse the sentences to know the number of satellites visible or used.

Reno
  • 33,594
  • 11
  • 89
  • 102
  • I'm definitely using GPS_PROVIDER. There is a chance that Samsung din't implemented this in Nexus S? – GetUsername Jul 05 '11 at 14:33
  • You should be able to get it using [bundle.getInt("satellites")](http://developer.android.com/reference/android/os/Bundle.html#getInt(java.lang.String). Are you sure you're getting fixes? Use the Nmea listener and look at the [GPGSV sentences](http://www.gpsinformation.org/dale/nmea.htm) – Reno Jul 06 '11 at 05:46
  • You right, Location.getExtras().getInt("satellites") suppose to return the number of satellites. But its not - I'm always getting a zero there despite the fact that location object itself contains a valid coordinates – GetUsername Jul 06 '11 at 07:57