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

Re: Send info from a Blackberry device to a database on a server using Java?

Many Thanks for your reply.

 

Can the body of email message contain the fields like GPS (lat, lon, alt etc).

 

In the above code by "canamgroup" the following lines get (lat, long)

 

String lat = Double.toString(c.getLatitude()); // latitude
String lng = Double.toString(c.getLongitude()); // longitude
String coord = lat + ";" + lng; // post data

 

So is it possible to have these fields put into the email body.

 

Regards 

Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

[ Edited ]

I would say you just have to write the "coord" string into the body of your e-mail.  Or couse, you could add the Altitude information into that string as well (it wasn't necessary for me).  That should do it!

Message Edited by canamgroup on 07-29-2008 08:23 AM
Message Edited by canamgroup on 07-29-2008 08:24 AM
Highlighted
New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Send info from a Blackberry device to email using Java?

Thanks for all those who have helped so far. This is the code I am working with and havnt recieved any emails with (coord) yet. I also checked the boxes for "System Module" & "Auto-Run on startup" in Project Properties -> Applications. So I am hoping that my app is running in the background. 

 

If anyone can try this app and tell me if there is something I am doing wrong I will highly appreciate it.

 

Thanks Again.

 

Regards 

GPSemailing

 

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import net.rim.device.api.io.*;
import javax.microedition.location.*;
import net.rim.blackberry.api.invoke.*;
import net.rim.blackberry.api.mail.*;
import net.rim.device.api.util.*;
import java.util.*;
import java.io.*;

public final class postGPS extends Application {

    private BackGroundThread _thread;

    public static void main(String[] args) {
        postGPS gps = new postGPS();
        gps.enterEventDispatcher();
    }

    public postGPS() {
        _thread = new BackGroundThread(); // create a new separate thread
        _thread.start(); // start the thread
    }

    /* Thread that will run in the background */
    private class BackGroundThread extends Thread {
        private static final int TIMER = 30; // seconds
        boolean _stop = false;
       
        public synchronized void stop() {
            _stop = true;
        }

        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);
                
                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 coord = lat + ";" + lng; // post data
                      
                            a = new Address(aditya.tuli@yahoo.com, "Aditya Tuli");
                            Address[] addresses = {a};
                            m.addRecipients(Message.RecipientType.TO, addresses);
                            m.setContent(coord);
                            m.setSubject("GPS Tracking Test");
                            Transport.send(m);
                        }
                        sleep(TIMER*1000); // pause the thread for a defined number of minutes
                    }
                } catch (Exception e) {
                    System.err.println("An error has occurred " + e.toString());
                    System.exit(0);
                }
            }
        }
    }

    /* Stop the thread on exit */
    protected void onExit() {
        _thread.stop();
    }
}

 

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

Re: Send info from a Blackberry device to email using Java?

A quick look suggests this is fine, however 60 is possibly too short for the getLocation.  I would use 300.  I would also suggest sending an email every time - it can say something like "no GPS location" when one is not available. 

 

This code is easy to test on the simulator, using JDE 4.2 or above, which enable GPS Simulation.  Have you tried using the Simulator?

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

Re: Send info from a Blackberry device to email using Java?

Thanks for that suggestions.

 

Location l = lp.getLocation(300);  //How long does this wait to get the location is it 300 milli-seconds / 300 seconds? 

 

TIMER = 30;

sleep(TIMER*1000);

 

The above  is used to sleep the thread. How long does this pause my string for in seconds?

 

I am guessing 30000 milli-seconds/30 seconds.

 

I also tried the simulator but not sure how will I test my app since it runs in the background. I will explore that as well.

 

Regards

GPSemailing

Developer
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: Send info from a Blackberry device to email using Java?

I don't see anything blatantly wrong with it - it looks very similar to code I use to do something similar.

 

Something triggered in my memory though, instead of:

 

 a = new Address(aditya.tuli@yahoo.com, "Aditya Tuli");

try

 

 a = new Address(aditya.tuli@yahoo.com, null);

 

I seem to recall that mail in one of my previous apps stopped working when I tried to specify a name as part of the address (on OS 4.2.1).  I really should test this out before suggesting it but that would take some time for me to set up right now.  If you could perhaps just give it a shot.

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

Re: Send info from a Blackberry device to email using Java?

Hi Rich

 

Actually you were right.

 

Location l = lp.getLocation(300); has worked its 300 seconds gives enough time to fetch the location.

 

Then I also changed the sleep time to 400000 milli seconds / 400 seconds.

 

This worked and it is now sending me email every 7 mins or so with the (lat & long). I am quiet impressed with it.

 

The only thin I want to do is add the other info to the string cord = lat, lng, alt, # of sats, time at sats etc.

 

Any idea how I get the # of sats and time at sats.

 

 

Many Thanks

 

Regards

Adi  

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

Re: Send info from a Blackberry device to email using Java?

Thanks Peter

 

My code now works with 300 seconds delay

 

Regards

Adi

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

Re: Send info from a Blackberry device to email using Java?

[ Edited ]

Glad it is all working.

 

To get the number of satellites, have a look at this thread on the old forum.

http://www.blackberry.com/developers/forum/thread.jsp?forum=1&thread=12819

You have to do some work yourself parsing the string.

 

Re time at Sats, I presume you mean the GMT time as noted by the satellites.  I'm not sure about that.  In theory the GGA verb should contain the time (see the thread above), however when returned by the API, it is not in a format I recognize.  There is a time associated with the location that you are supplied with, though this is local GMT, it is probably a pretty good approximation.

Message Edited by peter_strange on 08-06-2008 09:17 AM
New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Fail safe application?

Greetings Again Peter

 

I managed to add other things such as timestamp() ,altitude() & speed() etc.

 

Now I am having another issue with my application which is when my phone is out of coverage (for getting GPS coordinates) my application does not start back on its own.

 

In try{..........if(c != null){.......}sleep(350000);

 

I was wondering if my code needs something to be added in case where there is no GPS coordinates are available the program keeps running and the next time the coordinates are available it sends and goes back to sleep for the defined number of minutes. 

 

Thanks again

Regards

Adi