Retrieve satellite information by using the BlackBerryLocation class

You can request a GPS fix and then retrieve the current number of satellites in view, tracked satellites, average satellite signal quality, GPS data source (internal or external GPS), and the GPS mode.

  1. Import the required classes.
    import java.util.*;
    import java.lang.*;
    import net.rim.device.api.gps.*;
  2. Create a class and a constructor.
    public class handleGPS
    {
       public handleGPS()
       {
       }
    }
  3. In the class, declare static fields for a thread and for each item of location information that you retrieve.
    static GPSThread gpsThread;
    static int satCount;
    static int signalQuality;
    static int dataSource;
    static int gpsMode;
  4. In the constructor, create and start a thread.
    gpsThread = new GPSThread();
    gpsThread.start();
  5. In the class, create a private static class that extends Thread and a run() method.
    private static class GPSThread extends Thread
    {
        public void run()
        {
        }
    }
  6. In run(), create a try/catch block. In this block, create an instance of the BlackBerryCriteria class that specifies the GPS mode. Create a second try/catch block. In this block create an instance of the BlackBerryLocationProvider class by getting an instance of the BlackBerryCriteria object.
    try
    {
        BlackBerryCriteria myCriteria = new BlackBerryCriteria(GPSInfo.GPS_MODE_AUTONOMOUS);
    
        try
        {
            BlackBerryLocationProvider myProvider =
                (BlackBerryLocationProvider)LocationProvider.getInstance(myCriteria);
    
  7. Create a third try/catch block that is in the first try/catch block. Create a BlackBerryLocation object to retrieve the GPS fix including a 300 second timeout expiry. Populate the fields and extract the satellite information into a StringBuffer object.
    try
    {
        BlackBerryLocation myLocation =
            (BlackBerryLocation)myProvider.getLocation(300);
                        
        satCount= myLocation.getSatelliteCount();
        signalQuality = myLocation.getAverageSatelliteSignalQuality();
        dataSource = myLocation.getDataSource();
        gpsMode = myLocation.getGPSMode();
    
        SatelliteInfo si;
        StringBuffer sb = new StringBuffer("[Id:SQ:E:A]\n");
        String separator = ":";
    
        for (Enumeration e = myLocation.getSatelliteInfo();
            e!=null && e.hasMoreElements(); )
        {
            si = (SatelliteInfo)e.nextElement();
            sb.append(si.getId() + separator);
            sb.append(si.getSignalQuality() + separator);
            sb.append(si.getElevation() + separator);
            sb.append(si.getAzimuth());
            sb.append('\n');
        }
    }
    catch ( InterruptedException iex )
    {}
    catch ( LocationException lex )
    {}

Was this information helpful? Send us your comments.