Getting GPS Data from Android

Because Android’s source code is open, there are a lot of goodies that anyone with an Android device and Google’s development kit can explore. I was digging through the API section regarding location services when I found the method getSatellites(). How can you resist a method that sounds that cool?

Before you can get the GPS data, you need to set a couple of permissions in the manifest:

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

Next you can start building the Activity. You don’t really need to do anything in the UI since we’re only using the Android device for its GPS antenna.

Here’s the code I used to get my data:

package com.letstalkdata.gps_fun;

import java.util.Random;

import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity implements LocationListener {
	
	private LocationManager locationManager;
	private LocationListener locationListener = new DummyLocationListener();
	private GpsListener gpsListener = new GpsListener();
	private Location location;
	private GpsStatus gpsStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		gpsStatus = locationManager.getGpsStatus(null);
		locationManager.addGpsStatusListener(gpsListener);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2*1000, 0, locationListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void getSatData(){
		Iterable<GpsSatellite> sats = gpsStatus.getSatellites();
		
		for(GpsSatellite sat : sats){
			StringBuilder sb = new StringBuilder();
			sb.append(sat.getPrn());
			sb.append("\t");
			sb.append(sat.getElevation());
			sb.append("\t");
			sb.append(sat.getAzimuth());
			sb.append("\t");
			sb.append(sat.getSnr());
			
			try {
				new HttpLog().execute(sb.toString());
			} catch (Exception e) {
				Log.w("SatData Error",e.getMessage());
			}
		}
		
		gpsStatus = locationManager.getGpsStatus(gpsStatus);
	}

	protected void onResume() {
	   super.onResume();
	}
  
	@Override
	public void onLocationChanged(Location location){ }
	@Override
	public void onProviderDisabled(String provider) { }
	@Override
	public void onProviderEnabled(String provider) { }
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) { }
	
	class GpsListener implements GpsStatus.Listener{
	    @Override
	    public void onGpsStatusChanged(int event) {
	    	getSatData();
	    }
	}
	
	class DummyLocationListener implements LocationListener {
		//Empty class just to ease instatiation
	    @Override
	    public void onLocationChanged(Location location) { }
	    @Override
	    public void onProviderDisabled(String provider) { }
	    @Override
	    public void onProviderEnabled(String provider) { }
	    @Override
	    public void onStatusChanged(String provider, int status, Bundle extras) { }
	}
	
}

These are the real “goodie” methods in the code above:

  • getAzimuth() : Returns the azimuth of the satellite in degrees.
  • getElevation() : Returns the elevation of the satellite in degrees.
  • getPrn() : Returns the PRN (pseudo-random number) for the satellite.
  • getSnr() : Returns the signal to noise ratio for the satellite.

One quirk I noticed is that the internal GPS service does not actually refresh until it detects you are at a new location. So while it will continue to spit out numbers, you won’t see anything change until you move the device.

The azimuth and elevation tell you where, approximately, the satellite is in the sky from your point of view. To make sense of this, stand facing north and turn towards east the number of degrees of the azimuth. Then tilt your head upwards the number of degrees of the elevation. For example, when I grabbed this data, the satellite with PRN 18 had an Azimuth of 324°, which is almost a complete circle East, or about a tenth of a turn West. The elevation was 67° up from the horizon, which is about three quarters of the way from looking straight ahead to looking straight up.Example of using azimuth and elevation(Source: NOAA, Public Domain)

And if you want to know which specific satellite you’re looking at, Wikipedia has a list of the GPS Satellites with their PRN assignments.

Tagged on: , ,

3 thoughts on “Getting GPS Data from Android

    1. Phillip

      Sure thing, it’s just a simple AsyncTask that calls another class to actually send the data over HTTP.

      import android.os.AsyncTask;
      
      public class HttpLog extends AsyncTask<String, Void, Void> {
      
      	@Override
      	protected Void doInBackground(String... data) {
      		LogToFile.Log(data[0]);
      		return null;
      	}
      
      }
      

      I used this code for the LogToFile class.

Leave a Reply to Phillip Cancel reply

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