08-11-2008 05:55 AM
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"
;
Address[] addresses = {a};
m.addRecipients(Message.RecipientType.TO, addresses);
m.setContent(finalstr);
m.setSubject("GPS Location"
;
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
Solved! Go to Solution.
08-11-2008 06:35 AM
I suggested in a previous thread (http://supportforums.blackberry.com/rim/board/mess
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?
08-11-2008 07:14 AM
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
08-11-2008 07:42 AM
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.
08-11-2008 11:39 PM
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
08-12-2008 03:50 AM
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?
08-12-2008 05:36 AM
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
08-12-2008 11:43 AM
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); }
08-13-2008 02:38 AM
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. ![]()
}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
08-13-2008 12:17 PM
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.