Development Guide

Local Navigation

Error handling

You can retrieve the last error that was received when a GPS fix is unsuccessful by invoking GPSInfo.getLastGPSError(), available in the JSR 179 Location API, or BlackBerryLocation.getError(), available in the BlackBerry extensions to JSR 179.

Handle errors (JSR 179)

If a request for a GPS fix is unsuccessful, you can retrieve the last returned error.
  1. Import the required class.
    import net.rim.device.api.gps.GPSInfo;
  2. Create a class and a constructor.
    public class handleGPS
    {
        public handleGPS()
        {
        }
    }
  3. In the constructor, invoke GPSInfo.getLastGPSError() to retrieve the error.
    switch (GPSInfo.getLastGPSError())
    {
        case GPSInfo.GPS_ERROR_NONE: break;
        case GPSInfo.GPS_ERROR_ALMANAC_OUTDATED: break;
        case GPSInfo.GPS_ERROR_AUTHENTICATION_FAILURE: break;
        case GPSInfo.GPS_ERROR_CHIPSET_DEAD: break;
        case GPSInfo.GPS_ERROR_DEGRADED_FIX_IN_ALLOTTED_TIME: break;
        case GPSInfo.GPS_ERROR_GPS_LOCKED: break;
        case GPSInfo.GPS_ERROR_INVALID_NETWORK_CREDENTIAL: break;
        case GPSInfo.GPS_ERROR_INVALID_REQUEST: break;
        case GPSInfo.GPS_ERROR_LOW_BATTERY: break;
        case GPSInfo.GPS_ERROR_NETWORK_CONNECTION_FAILURE: break;
        case GPSInfo.GPS_ERROR_NO_FIX_IN_ALLOTTED_TIME: break;
        case GPSInfo.GPS_ERROR_NO_SATELLITE_IN_VIEW: break;
        case GPSInfo.GPS_ERROR_PRIVACY_ACCESS_DENIED: break;
        case GPSInfo.GPS_ERROR_SERVICE_UNAVAILABLE: break;
        case GPSInfo.GPS_ERROR_TIMEOUT_DEGRADED_FIX_NO_ASSIST_DATA: break;
        case GPSInfo.GPS_ERROR_TIMEOUT_NO_FIX_NO_ASSIST_DATA: break;
    }
Back To Top

Handle errors (BlackBerry extensions to JSR 179)

You can check the status of a GPS fix request by invoking the getStatus() method that is provided in the BlackBerry extensions to JSR 179. If the return is BlackBerryLocation.GPS_ERROR, you can retrieve the error value by invoking BlackBerryLocation.getError().
  1. Import the required classes and interfaces.
    import net.rim.device.api.gps.*;
    import javax.microedition.location.*;
  2. Create a class and a constructor.
    public class handleGPS
    {
        public handleGPS()
        {
        }
    }
  3. In the constructor, create a try/catch block. In this block, create an instance of the BlackBerryCriteria class by passing the GPS mode to the constructor.
    try
    {
        BlackBerryCriteria myCriteria =
          new BlackBerryCriteria(GPSInfo.GPS_MODE_ASSIST);
    }
    catch ( UnsupportedOperationException ex )
    {
        return;
    }
  4. In the try/catch block, create another try/catch block. In this block, create an instance of the BlackBerryLocationProvider class by retrieving the BlackBerryCriteria object. Invoke setLocationListener() to specify the location listener.
    try
    {
        BlackBerryLocationProvider myProvider =
            (BlackBerryLocationProvider)LocationProvider.getInstance(myCriteria);
        myProvider.setLocationListener(new myLocationListener(), -1, -1, -1);
    }
    catch ( LocationException lex )
    {
        return;
    }
  5. In the class, create a static class that implements LocationListener. Implement locationUpdated() and providerStateChanged().
    private static class myLocationListener implements LocationListener
    {
        public void locationUpdated(LocationProvider provider, Location location)
        {
        }
    
        public void providerStateChanged(LocationProvider provider, int newState)
        {
        }
    }
  6. In locationUpdated(), verify if the location parameter is an instance of BlackBerryLocation. If so, then create a local BlackBerryLocation object by passing the location parameter. Invoke getStatus() to retrieve the status of GPS location request, and then process the returned status.
    if (location instanceof BlackBerryLocation)
    {
        BlackBerryLocation bLoc = (BlackBerryLocation)location;
    
        switch(bLoc.getStatus())
        {
            case BlackBerryLocation.GPS_ERROR:
                int gpsStatus = bLoc.getError();
                break;
    
            case BlackBerryLocation.FAILOVER_MODE_ON:
            case BlackBerryLocation.SUBSEQUENT_MODE_ON:
            case BlackBerryLocation.GPS_FIX_PARTIAL:
            case BlackBerryLocation.GPS_FIX_COMPLETE:
                break;
        }
    }

Code sample: Handling errors (BlackBerry extensions to JSR 179)

import net.rim.device.api.gps.*;
import javax.microedition.location.*;

public class handleGPS
{
    public handleGPS()
    {
        try
        {
            BlackBerryCriteria myCriteria =
              new BlackBerryCriteria(GPSInfo.GPS_MODE_ASSIST);

            try
            {
                BlackBerryLocationProvider myProvider =
                  (BlackBerryLocationProvider)
                    LocationProvider.getInstance(myCriteria);
                myProvider.setLocationListener
                  (new myLocationListener(), -1, -1, -1);
            }
            catch ( LocationException lex )
            {
                return;
            }
        }
        catch ( UnsupportedOperationException ex )
        {
            return;
        }
    }

    private static class myLocationListener implements LocationListener
    {
        public void locationUpdated
          (LocationProvider provider, Location location)
        {
            if (location instanceof BlackBerryLocation)
            {
                BlackBerryLocation bLoc = (BlackBerryLocation)location;

                switch(bLoc.getStatus())
                {
                    case BlackBerryLocation.GPS_ERROR:
                        int gpsStatus = bLoc.getError();
                        break;

                    case BlackBerryLocation.FAILOVER_MODE_ON:
                    case BlackBerryLocation.SUBSEQUENT_MODE_ON:
                    case BlackBerryLocation.GPS_FIX_PARTIAL:
                    case BlackBerryLocation.GPS_FIX_COMPLETE:
                        break;
                }
            }
        }

        public void providerStateChanged
          (LocationProvider provider, int newState)
        {
        }
    }
}
Back To Top

Was this information helpful? Send us your comments.