Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not getting GPS

I have been working on getting GPS location I have been through many examples and articles but did not have any success yet. May be I am missing something very minor I guess but I am new to BB development.

Below is the code

package mypackage;

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen {

/**
* Creates a new MyScreen object
*/
public MyScreen() {
    setTitle("GPS Demo");
    new GpsThread().start();
}

public boolean onClose() {
    UiApplication.getUiApplication().requestBackground();
    return false;
}

private class GpsThread extends Thread {

    Location currentLoc; // Stores component information of our position

    Criteria cr; // Settings for the GPS - we can read it at
    // different accuracy levels

    LocationProvider lp; // LocationProvider does the actual work of reading
    // coordinates from the GPS

        public GpsThread() {
            add(new LabelField(("GPS Constructor")));
            cr = new Criteria();
        }

        public void run() {

            add(new LabelField(("run")));
            showLocationToast();

        }

        private void setCriteria() {

        // I basically set no requirements on any of the horizontal,
        // vertical,
        // or
        // power consumption requirements below.
        // The distance components are set in meters if you do want to
        // establish
        // accuracy - the less the accuracy, the
        // quicker and more likely a successful read (I believe).
        // You can also set power consumption, between low, medium, high (or
        // no
        // requirement)
        // There are also a number of other settings you can tweak such as
        // minimum
        // response time, if altitude is required,
        // speed required, etc. It all depends on the exact application
        // you're
        // writing and how specific you need the info to
       cr.setCostAllowed(true);
       cr.setHorizontalAccuracy(Criteria.NO_REQUIREMENT);
       cr.setVerticalAccuracy(Criteria.NO_REQUIREMENT);
       cr.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
       add(new LabelField(("Criteria set ")));

    }

    private void getLocation() throws InterruptedException {

        setCriteria();
        try {
            add(new LabelField(("getting location")));
            // Get a new instance of the location provider using the
            // criteria we
            // established above.
           if (lp != null) {
                lp.setLocationListener(null, 0, -1, -1);
                lp.reset();
                lp = null;
            }

        add(new LabelField(("lp had something so resetting it")));
        lp = LocationProvider.getInstance(cr);
        add(new LabelField(("got locationprovider instance ... ")));

        // Now populate our location object with our current location
        // (with
       // a 60 second timeout)
       lp.setLocationListener(new MyLocationListener(), 120, -1, -1);

       currentLoc = lp.getLocation(60);

       String location = "Latitude : "
       + currentLoc.getQualifiedCoordinates().getLatitude()
       + "\n Longitude : "
       + currentLoc.getQualifiedCoordinates().getLongitude();
       add(new LabelField((location)));
       }
       // If we hit the timeout or encountered some other error, report it.
       catch (LocationException e) {
           // Dialog.alert("Error getting coordinates");
           add(new LabelField(e.getMessage()));
          return;
        }
   }

   private void showLocationToast() {

        add(new LabelField(("showing toast")));

        try {
            getLocation();
        } catch (InterruptedException e) {
            add(new LabelField(e.getMessage()));
        }

        add(new LabelField(("Got all info")));
        String location = "Latitude : "
        + currentLoc.getQualifiedCoordinates().getLatitude()
        + "\n Longitude : "
        + currentLoc.getQualifiedCoordinates().getLongitude();
        add(new LabelField("Showing toast..." + location));
    }
}
}

Additional Info: class my listener just has dialog that must be shown when location is updated. I even tried to get single location without using listener

like

lp.getLocation (60);

just before where I had set my listener. But both times I get Label only upto where I have set listener. I will be using Timer and TimerTask however I am able to get this demo right.

I am using 9900 for development and the app is supposed to be on 4.5.0

like image 505
Its not blank Avatar asked Jul 11 '26 20:07

Its not blank


1 Answers

I see two things that don't look right to me. I can't duplicate your exact test scenario, so I can't say for sure that they're the problem ... only that they look suspicious.

Threading

You are making calls to update your UI, adding LabelFields, from the background thread (your GpsThread). I don't think that's right. In general, I always do things like this when I have a background thread that needs to update the UI

private void addNewLabelField(final String text) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
           add(new LabelField(text));
        }
    });
}

Then, you can call addNewLabelField("something"); from wherever you want.

That said, I thought you would get an IllegalStateException if you tried to do this, and you say you're not getting any exceptions. Anyway, I think it's still good practice to keep UI-related calls on the main (UI) thread.

Location Criteria

You are using NO_REQUIREMENT, NO_REQUIREMENT, cost allowed, POWER_USAGE_HIGH for your location Criteria. If I look at this document:

http://supportforums.blackberry.com/t5/Java-Development/Location-APIs-Start-to-finish/ta-p/571949

... scrolling down to the table named Criteria mapping for JSR-179 API, it seems to imply that this precise location Criteria is only supported for CDMA phones. But, according to this, the 9900 is a GSM phone. Do you know (maybe I'm wrong, and it comes in CDMA models, too)?

But, if you happened to pick a Criteria that's not supported on your phone, you probably won't get location results. So, I would definitely try another combination of criterion, that are listed in that document I linked to above.

That's my best guess.

like image 153
Nate Avatar answered Jul 14 '26 00:07

Nate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!