GPS Programming Insights

Here is a quick insight into GPS programming for Android. It is by no means a complete reference but it may be able to give you the missing perception you are looking for.

GPS Primer

The location in GPS is calculated by measuring the distance from orbitting satellites and doing a process called triliteration which is similar to triangulation. GPS calculates the distances by measuring the time it took for the satellite signal to reach the receiver. So distance = travel time t x 300000 km/sec. The satellites broadcast their own position through data sets called Almanac(Imprecise data) and Ephemeris(Precise data)
So in theory the receiver can solve for position using the three satellites and the distances from them.
As is evident the travel time can only be calculated with any degree of accuracy if the receiver clock is perfectly in sync with the satellites. The satellites transmit a Pseudo Random Code which is also known to and repeated by the receivers. If the clocks are in sync then this code will be perfectly in step. However due to inaccuracies in receiver clock the code will be quiet off. The secret to GPS is that using this inaccurate clock the receiver solves for position using the three satellites and then uses a fourth satellite to eliminate the sync error. Thus 4 satellites are required to get a position fix.

Android LocationManager

The code to start listening to location updates in your Activity is:-

// Comment

public class GpsActivity extends Activity implements GpsStatus.Listener, LocationListener{
   //...
   @Override
   protected void onResume() {
      LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
      lm.addGpsStatusListener(this);
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, mintime, mindistance,this);
   }

   @Override
   public void onPause(){
      lm.removeGpsStatusListener(this);
      lm.removeUpdates(this);
   }
   @Override
   public void onGpsStatusChanged(int event) {
      switch(event){
         case GpsStatus.GPS_EVENT_FIRST_FIX:
         break;
      case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
         break;
      case GpsStatus.GPS_EVENT_STARTED:
         break;
      case GpsStatus.GPS_EVENT_STOPPED:
         break;
      }
   }

   @Override
   public void onLocationChanged(Location loc) {
   }

   @Override
   public void onProviderDisabled(String provider) {
   }

   @Override
   public void onProviderEnabled(String provider) {
   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {
   }
}

The GPS engine starts when the requestlocationupdates( ) is called. This engine is power hungry and guzzles power from EVENT START to EVENT STOP received in GPS status listener. The engine needs to download almanac and ephemeris and solve for multiple satellites to get location fixes so be very careful how long it runs. Commercial GPS receiver have dedicated multiple channels for simultaneous downloading and processing satellites almanac and ephemeris data. So solution emerges faster. How many channels do mobile GPS sets have ? If it is single channel it will be slow to converge location fix.
The validity of almanac data or orbital data of satellites is an imprecise long term data which typically remains valid for months. The ephemeris on the other hand is precise data and short term and the platform may invalid it in terms of a few hours. The ephemeris of each satellite is repeated in 30 sec intervals consisting of 3 packets with 6 sec blocks. So if 4 satellites are available and snr is > 28 dB and at least 30 sec for collecting ephemeris of one satellite. For four satellites 4*30=120 secs in single channel. For 4 channel rx it is only 30 sec. While collecting ephemeris if platform is moving then this critical process may need to be repeated until success.

So a lot of processing is to be done by GPS engine for giving location fix which has power implication.

There are some things to keep in mind while programming :-

If update rate is more than 3 secs for requestlocationupdates( ) then engine then will not work continuously. To conserve power at slower update rates a STOP_EVENT will start coming in between followed by a START_EVENT when the update interval comes.
When engine is ON wakelock is held. This also means that when you keep updaterate at <=3 secs no sleep for device.
The engine will call ongpsstatuschanged( ) listener when
1. FIRST FIX – First time location fix
2. EVENT STARTED – When Engine starts
3. EVENT STOPPED – When Engine stops
4. SAT STATUS – When the satellite data changes.
The SAT STATUS is a high throughput call and mutiple calls come so throttle your processing here if any. Here you can retreive an iterable satellite list through a gpsstat object and get loads of information like prn no , validity of ephemeris etc.
The engine will cal onlocationchanged( ) when solution is available at the rate passed to requestlocationupdates ( ). Here you can retreive location and other stuff like accuracy.
This is an ongoing article and I will update this as I get more insights.

Author: admin

Indie Android dev

Leave a Reply

Your email address will not be published. Required fields are marked *