Creating GPS Tracker Application on J2ME Phones
This article describes how to create the J2ME tracking application to be installed on J2ME capable phones with an internal GPS receiver. The application will utilize the Location API available on GPS enabled phones, read the GPS location data at a specified interval, send the data via HTTP connection to a specified server.
Here is the application looks like.
To simulate the GPS events on the device, let’s create an XML file to be loaded to the emulator:
<waypoints>
<waypoint time="0" latitude="-6.10" longitude="107.36" altitude="100" />
<waypoint time="10000" latitude="-6.54" longitude="108.38" altitude="0" />
</waypoints>
Save it into a file. Then load into the emulator from menu View- External Events Generator. Browse for the script file and press the Start button.
Here is the source code for the middlet. The filename is TrackMe.java.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package gpstracking;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
import java.util.Date;
import java.util.Calendar;public class TrackMe extends MIDlet implements CommandListener,
LocationListener {private LocationProvider locationProvider = null;
private Display display;
private Form form;
private Command exit = new Command("Exit", Command.EXIT, 1);
private Command start = new Command("Start", Command.SCREEN, 1);
private Command stop = new Command("Stop", Command.SCREEN, 1);
private TextField unitId =
new TextField("Unit ID/IMEI", "067442957992", 50,
TextField.ANY);
private String unitIdstr = "067442957992";
private TextField interval =
new TextField("Update Interval(min)", "1", 5,
TextField.NUMERIC);
private int sec = 60;
private StringItem info = new StringItem("Location:", "unknown");public TrackMe() {
display = Display.getDisplay(this);
form = new Form("GPS Tracking Indonesia");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
form.append(unitId);
form.append(interval);
form.append(info);try {
locationProvider = LocationProvider.getInstance(null);
} catch (Exception e) {
e.printStackTrace();
exit();
}
}public void locationUpdated(LocationProvider provider, Location location) {
if (location != null && location.isValid()) {
QualifiedCoordinates qc =
location.getQualifiedCoordinates();HttpConnection connection = null;
try {String url = "http://server-address/up.php?"
+ "unit=" + unitIdstr
+ "&lat=" + qc.getLatitude()
+ "&lon=" + qc.getLongitude();System.out.println(url);
connection = (HttpConnection) Connector.open(url);info.setText(
"Lat: " + qc.getLatitude() + "\n"
+ "Lon: " + qc.getLongitude() + "\n"
+ "Alt: " + qc.getAltitude() + "\n"
+ "Server Response: "
+ connection.getResponseMessage());
connection.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Exception io) {
io.printStackTrace();
}
}
}
}static public String urlEncode(String sUrl) {
StringBuffer urlOK = new StringBuffer();
for (int i = 0; i < sUrl.length(); i++) {
char ch = sUrl.charAt(i);
switch (ch) {
case ‘<’:
urlOK.append("%3C");
break;
case ‘>’:
urlOK.append("%3E");
break;
case ‘/’:
urlOK.append("%2F");
break;
case ‘ ‘:
urlOK.append("%20");
break;
case ‘:’:
urlOK.append("%3A");
break;
case ‘-’:
urlOK.append("%2D");
break;
default:
urlOK.append(ch);
break;
}
}
return urlOK.toString();
}public void providerStateChanged(LocationProvider provider,
int newState) {
}public void commandAction(Command c, Displayable s) {
if (c == exit) {
exit();
}
if (c == start) {
form.removeCommand(start);
unitIdstr = (unitId.getString() != null)
? unitId.getString() : "123";
sec = (interval.getString() != null)
? Integer.parseInt(interval.getString()) : 5;// Start querying GPS data :
locationProvider.setLocationListener(TrackMe.this, sec, -1, -1);
form.addCommand(stop);
}
if (c == stop) {
form.removeCommand(stop);// Stop querying GPS data :
locationProvider.setLocationListener(null, -1, -1, -1);
form.addCommand(start);
}
}public void startApp() {
display.setCurrent(form);
}public void pauseApp() {
}public void destroyApp(boolean forced) {
}public void exit() {
destroyApp(false);
notifyDestroyed();
}
}
Some things that’s important on the above source code:
locationProvider = LocationProvider.getInstance(null)
This is where we instantiate the locationProvider object to be able to query the GPS location from the device.
locationProvider.setLocationListener(TrackMe.this, sec, -1, -1)
This will set the listener that will process the location update information sent by the LocationProvider. The provider will update the listener for the current GPS location at every sec seconds.
public void locationUpdated(LocationProvider provider, Location location)
This is the implemetation of the locationUpdated method of the LocationListener. This will be called on the interval set at the setLocationListener.
QualifiedCoordinates qc = location.getQualifiedCoordinates()
This is called every time the locationUpdated is invoked. This will store the location information into the QualifiedCoordinates qc variable where later we can set the latitude and longitude to be sent to out server.
locationProvider.setLocationListener(null, -1, -1, -1)
This will stop the LocationProvider to send the location information to the listener.
connection = (HttpConnection) Connector.open(url)
This is how we send data to the server using HTTP GET. The data is already encoded on the url, which including the latitude and longitude along with the unit that are sending the data. Later we can see the connection result by setting the info’s text using info.setText() method.
static public String urlEncode(String sUrl)
This is our own function to encode a string to a HTML encoded so that we can send it as a parameter of HTTP GET.
Real live example GPS Tracking System complete application can be viewed at www.GpsTrackingIndonesia.com.
If You need any further information or consultation, contact me directly:
Akhmad Daniel Sembiring
vitraining.com – CEO



This is work perfectly…
mas minta tolong di kirim source codenya dunk,, berbentuk zip apa rar..
biar enak cara ngerjainya. mksih ya mas..
i am newbi in j2me
can u tell me?
to run dis java program can i use netbeans if yes den how to configure netbeans to use internet?
plz tell me………..
yes, you can use netbeans
please ask for source code. the form of zip or rar.
that is unambiguous.
thanks.
please help me,.
I am getting the error phone not compatible…i am using an E71…any suggestion?
make sure you select MIDP2.0 on your project profile
Hello,
Very nice post. I really appreciate your program. Is it possible to get a compiled file so that I can directly install it onto my phone.
Thanks.
Hi Varun, thanks. I think you need to compile it yourself , it;s a very basic program so should work on any devices
sir i want to know the exact location of a person using gps and try to give dtails what he needs
how to get the gps co-ordinates of a preson’s mobile using j2me please help me
please mail to athotasandeep@gmail.com
Hi Admin,
I am trying to make this file run in Blackberry Phone and a Samsung Wave Phone , in both the phones this app is giving location as unknown. Can you please help.
@admin
Also it is not reporting to the server that I specified. Any help ?
i am new to j2me i had build the same code and run on my 6212Nokia which is not GPS enabled . its giving me error of class not found exception for location listener Interface ..pls help me i am not get complete understanding of application what should be the url i have to use to get HTTP connection. my device is Gprs Enabled with vodaphone Pls Help me ?
hi admin, it’s a very nice post. mmm could you tell me how this program can get the data from the gps? and coul you tell me what’s the “data.xml” for? is it injected to the phone or whe just hace to load it?
i’m sorry, i’m just a newbie.
thanks
(if you don’t mind you may send the answer to my email)
Guys I really appreciate you.
Thanks for the post, its really
important
Creating project “position”
Place Java source files in “C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src”
Place application resource files in “C:\Users\pardeep\j2mewtk\2.5.2\apps\position\res”
Place application library files in “C:\Users\pardeep\j2mewtk\2.5.2\apps\position\lib”
Settings updated
Project settings saved
Building “position”
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:87: illegal character: \8216
case ‘<’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:87: generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
case ‘<’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:87: illegal character: \8217
case ‘’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:90: illegal character: \8217
case ‘>’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:93: illegal character: \8216
case ‘/’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:93: illegal character: \8217
case ‘/’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:96: illegal character: \8216
case ‘ ‘:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:96: illegal character: \8216
case ‘ ‘:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:99: illegal character: \8216
case ‘:’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:99: illegal character: \8217
case ‘:’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:102: illegal character: \8216
case ‘-’:
^
C:\Users\pardeep\j2mewtk\2.5.2\apps\position\src\TrackMe.java:102: illegal character: \8217
case ‘-’:
^
13 errors
com.sun.kvem.ktools.ExecutionException
Build failed
im getting these errors,can u please tell me
i am an embedded designer. i want to send data from an embedded device via a gsm module to any java enabled mobile phone. any guide/help to write a j2me app that will maintain a gprs connection and received ascii characters/data from the embedded device.
can’t use sms as mission critical information cannot be guarenteed in realtime via sms and therefore want to use gprs.
thank you,