Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified
Accepted Solution

Emailing GPS Info from BlackBerry curve8310 using Java

Below is the code for sending GPS Info from blackberry to an email address. The code uses sleep to send info at intervals.

 

My problem is that when once GPS becomes out of range the application stops sending emails and no exception is thrown.

 

if (lp != null)

{ code }

sleep(65000);

 

But this has not worked for me.

////////

public void run() {
            while (!_stop) {
                Address a;
                Store store = Session.getDefaultInstance().getStore();
                Folder[] folders = store.list(Folder.SENT);
                Folder sentfolder = folders[0];
                Message m = new Message(sentfolder);
                //SimpleDateFormat sdf;
              
                try { 
                    LocationProvider lp = LocationProvider.getInstance(null);
                    if (lp != null) {
                        Location l = lp.getLocation(60);
                        Coordinates c = l.getQualifiedCoordinates(); // get GPS coordinates
                       
                        if (c != null) {
                            String lat = Double.toString(c.getLatitude()); // latitude
                            String lng = Double.toString(c.getLongitude()); // longitude
                            String alt = Float.toString(c.getAltitude()); // altitude
                            String tim = Long.toString(l.getTimestamp()); // timestamp
                            String spd = Float.toString(l.getSpeed()); // speed
                           
                            String orgstr = "0449953853,"+ tim + "," + lat + "," + lng + "," + spd + "," + alt;
                            String googmap = "http://maps.google.com/maps?q=" + lat + "," + lng;
                            String finalstr = orgstr + "\n" + googmap;
                            a = new Address("aditya.tuli@yahoo.com", "Aditya Tuli":smileywink:;
                            Address[] addresses = {a};
                            m.addRecipients(Message.RecipientType.TO, addresses);
                            m.setContent(finalstr);
                            m.setSubject("GPS Location":smileywink:;
                            Transport.send(m);
                       }
                    }sleep(65000); //moved for re-running the thread
                }catch (Exception e) {
                    System.err.println("An error has occurred " + e.toString());
                    System.exit(0);
                }
            }
        }

///////

 

Please suggest.

 

Thanks in advance.

 

Regards

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

I suggested in a previous thread (http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&thread.id=151&page=3), that 60 (seconds) was possibly too short a time for a getLocation, and I suggested 300, which you tried and found that it worked.  Has this change been lost or have you found that 60 worked anyway?

 

Looking at your code, it would seem that either you are not getting a LocationProvider, or you are not getting qualified coordinates.  Can you put debugging statements in to find out which of these it is?

New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

Yes getlocation(60) seconds worked with sleep timer of 65 seconds. I recon that in the code by canamgroup the (sleep time) < (getlocation) which probably made it go to sleep before it could send an email. I had sleep time. I even tried to work with getlocation (300) seconds and sleep (350000) 350 seconds but it had the same problem when GPS is out of range.

 

So reading your suggestions I realise that application is still running but probably unable to get either location provider / qualified coordinates even when there is GPS reception. Not sure why this could be happening. I will try some debugging.

 

Another question arise when I become impatient while waiting for application to send an email and then go and start the application again by clicking on the application icon. Does this run another seperate application instance or does it override the previous instance of the application.

 

Thanks

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

Remember that the time is the getLocation is the maximum time that it will wait till it gets a location.  If this was specified at 300, the actual time before you were given a location could be 1 second or 300 seconds, however if the GPS has still not got a location after waiting 300 seconds, then it will return to you without a location.

 

If you have a short sleep time, then perhaps the GPS unit does not switch off, which is why you can get away with 60 second getLocation and 65 seconds sleep.  However I would not recommend hoping that this is the case.  So I would leave the getLocation at 300.

 

I can't answer the question as to why the getLocation(300) with sleep time of 350 seconds still has a problem - I think you need to do some debugging.

 

Your other question, regarding the application, is more interesting.  It is possible that you end up with two copies of your code running - one as the background task, one with the screen.  That depends on how you have coded it and how you detect in your User Interface that there is a background application.  This I think is a topic for a different thread.  But check your main() processing, by working through it using a debugger on the simulator, to see if you get two instances of your email thread started.  I suspect you do.

New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

Hi Peter

 

What do you suggest I do for debugging the code. Since I dont get an exception i.e. my code does not go through catch at all.

 

Regards

Adi

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

I think we have agreed that the problem is either that there is no LocationProvider or the getLocation is not returning a location, so determining which of these seems like the best place to start.  Since you are testing on the device you will have to debug on the device, which you can do by USB connecting it, starting the JDE, the using Debug/Attach To/HandHeld, then select the USB attached device.

 

From there you should be able to use the JDE debugging, but depending on levels, this sometimes doesn't work so well for me.  So I would add three System.out.println(...) statements, one at the start of the loop and the others inside each of the 'if's.  Load this onto the device, then, once the debugging has started, you just have to wait for the missing output for each loop!

 

Does that help?

New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

I will give that a go and then let you know. Just a qiuck one on the catch{....}, if there was an error caught then it would have been displayed on the device right?

 

Many Thanks

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

System.err.println will not display on the device, in fact it will not even appear in the Device's log.  The only time you will see this is when the device is connected to a debugger. 

 

If you want it to appear on the device, you are going to have to use the EventLogger (I recommend this), or use some Dialog or Status pop-up screen to see it.  From memory, in your previous post, I think you mention that this is an Application without a User Interface, so I think you will have to use code like the following to display something for the User to see.

 

//Get an instance of the UiEngine and place a Dialog in the queue //to alert the user of the message in the String data. public static void notifyUser(String message) { UiEngine ui = Ui.getUiEngine(); Screen screen = new Dialog(Dialog.D_OK, message, Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.VERTICAL_SCROLL); ui.queueStatus(screen, 99, true); }

 

 

 

New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

Hi Peter

 

After some debugging I am now convinced that when there is no GPS reception [there is no location provider] then there is an exception caught (my bad for not attaching the debugger earlier). In my catch I have system.exit(0);. I have commented out that line and now things are working fine. :smileyhappy:

 


                }catch (Exception e) {
                    System.err.println("An error has occurred " + e.toString());
                    //System.exit(0);
                }

 

So I just dont let the system exit even if there is an exception is caught it just goes to sleep. Once the GPS is back in reception it wakes up instantly and sends an email.

 

I am now Developing the app further. I have a few questions about to be able to change sleep time and other fields through a user interface in the phone. Is that possible?? 

 

Many Thanks Peter

 

Regards

Adi

 

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Emailing GPS Info from BlackBerry curve8310 using Java

Glad you have that solved.

 

Updating via a User Interface is certainly possible.  I suggest that if you wish to leave this processing as an Application (so there is no user icon nor an icon on the Task Switcher screen as you would get with a UiApplication), then you could look at OptionsProvider interface. 

 

I also suggest you look at the EventLogger to log your Exceptions.