here is a short introduction to the JSR 179 API commonly known as the Java Location API.
Introduction on the API
The Java JSR 179 specification concerns the Location API (as mentioned above) provided by Sun for Java Micro Edition (JavaME). This API targets mobile phones or any other mobile devices such as PDAs which integrate the Java platform.This specification has been created and is being maintained by Nokia. For more information, please have a look to Forum Nokia (Of course I provide you with all the interesting links at the end of this article).
Introduction to location
Coordinates:
The main idea behind the location process is to get the coordinates of the mobile device and most of the time the result is to point out your location on a map. The coordinate system is not difficult to understand, it is working by degrees on a North/South and East/West way.For example, the coordinates of Baltimore are 39.28° North (latitude) and 76.60° West (longitude) which is represented for us as the couple of values (39.28, -76.60). You can notice the minus sign that appeared before the longitude value. It is only because west is the negative part of the coordinate system for the longitude, as well as south is for the latitude. If you want to try out some couple of values, just go on Google Maps and enter the values like I did for Baltimore.
Means for location:
There are currently 2 main means to get the location of a mobile device:- GPS
- GSM/UMTS
GPS
The GPS (Global Positioning System) will not be discussed here since I assume that everybody know how it is working. If not, it is easy to find some good introduction about it over the Internet, or just check out Wikipedia.What is useful to know about GPS for this article is that this is the easiest, cheapest and most commonly used mean to locate a mobile device. Nowadays, most of the high-end phones tend to integrate a GPS chip. If not, there is the possibility to use a Bluetooth one. Furthermore, this does not imply extra fees from the provider side, i.e. no need to provide a specific service for the users to use the system.
GSM/UMTS
For all the phones which do not have such a chip, there is another way to be located. To do so, it is needed to use the GSM network infrastructure which means the provider has to make such a service available for the users, i.e. it will certainly not be free of use.There are different ways to find out where a mobile phone is and only the most common are listed below:
- COO (Cell of Origin), also called Cell-ID, is the easiest and also the cheapest way. It is unfortunately not very accurate, especially in wide areas (up to 30km) but can still be helpful in an urban area (as good as a few hundred meters). This method consists in finding out the closest GSM base station to the phone. Once it is done, the phone location will be set as being the same as the found base station. An enhanced version is also available where the broadcasting area of the phone is part of the area around the base station, hence this is more accurate.
- TOA (Time of Arrival) is a more complicated way. This method consists in computing the time needed for an emitted signal to go from the phone to the base station. Of course, the more base stations, the more accurate.
- RSSI (Received Signal Strength Indicator) is working in a similar way as the previous method. In this case, the phone emits a signal that will be caught by the base station and the signal strength will help determining the distance between the phone and the base station. Once again, the more base stations, the more accurate
In the rest of this article, only the devices equipped with a GPS (built in or linked via Bluetooth) will be discussed. This means that only the location via GPS will be talked about since the API doesn’t provide any GSM/UMTS location techniques.
Using the API – A short example
The best way to understand how it works is with an example.import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.location.Coordinates; import javax.microedition.location.Criteria; import javax.microedition.location.Location; import javax.microedition.location.LocationException; import javax.microedition.location.LocationProvider; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class EasyLocation extends MIDlet { /** * ----------- * Attributes: * ----------- */ private static Display display = null; private LocationProvider lp = null; private Location location = null; private Criteria criteria = null; /** * -------------------- * Getters and Setters: * -------------------- */ public static Display getDisplay() { return display; } /** * ------------------------ * MIDlet specific methods: * ------------------------ */ protected void startApp() throws MIDletStateChangeException { // Getting the instance of the display display = Display.getDisplay(this); // Instantiation of the interesting attributes lp = null; location = null; criteria = new Criteria(); criteria.setHorizontalAccuracy(500); // Getting the instance of the provider i.e. the GPS. try { // If there is a built in chip on your phone, it will just ask for permission. // If not, it will look for a Bluetooth device. lp = LocationProvider.getInstance(criteria); } catch (LocationException e) { // ... } // Timeout for the connection try { location = lp.getLocation(20); } catch (LocationException e) { // ... } catch (InterruptedException e) { // ... } // Getting the coordinates String res = "Coordinates:\n"; try { Coordinates coordinates = location.getQualifiedCoordinates(); res += "Altitude: " + coordinates.getAltitude() + "\n"; res += "Latitude: " + coordinates.getLatitude() + "\n"; res += "Longitude: " + coordinates.getLongitude() + "\n"; } catch (Exception e) { // ... } // Displaying the result Form form = new Form("Results"); form.append(res); display.setCurrent(form); } protected void pauseApp() {} protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {} }The given code is the smallest code needed to get the location of the mobile device.
Now that we have an idea of how it looks like, it is not very difficult to understand how the thing is working. Indeed, the main idea is to create an instance of the GPS module. Once this is done, we just need to look for the coordinates and of course, we display them. Easy isn’t it?
Well, that should be enough for today. What you learned so far is that getting the coordinates using JavaME is really not difficult at all. You can all do that! I hope you also liked the small introductions I gave on both API and location idea. Have fun!
Thanks a lot,It is very useful
ReplyDelete