Showing posts with label J2ME. Show all posts
Showing posts with label J2ME. Show all posts

Finding your Location without GPS in J2ME

Off late, the Location Based Services are the talk of town and truly the potential for those services are enormous. But the limiting factor for the wide spread use of LBS is the means of delivering those services. The best means I would say is through their mobile phones, thanks to the ever increasing penetration of the mobiles and nowadays it is hard to find anyone who is not having a mobile phone. The issue here is the finding the location of the mobile phone to deliver the required services. Sure, there are phones with GPS but they are not widespread and the effective alternative would be to use the mobie phone network to find the location of the phone.

A GSM mobile phone keeps track of four parameters that can be used to find the approximate location of the mobile they are MCC (Mobile Country Code), MNC (Mobile Network Code), LAC (Location Area Code) and CellID. Combining these four paramenter you can zero in on mobile phone's location with an accuracy of 1KM (Urban areas) to 3KM (Sub-urban and rural areas), which is quiet sufficient for some Loaction Based Services.

There are some websites that collects the cellid's across the globe like

  1. http://www.cellspotting.com/
  2. http://www.opencellid.com/

or you can sure hop into your vehicle and roam around the city to make your own database.

Now the gotchas, there is no way to extract the cellid information from most of the phones in the J2ME environment. But the sony ericsson has thoughtfully made the cellid information accessible in the J2ME environment, refer Page 72 of SE Developer Guidelines Document. But it will work only on Sony Ericsson phones that has JAVA Platform 7.3 and above.

Now, using the below APIs' you can get the CellID information in your J2ME Application


For Home Network
System.getProperty("com.sonyericsson.net.mcc");
System.getProperty("com.sonyericsson.net.mnc");

For current Network
System.getProperty("com.sonyericsson.net.cmcc");
System.getProperty("com.sonyericsson.net.cmnc");

System.getProperty("com.sonyericsson.net.cellid");
System.getProperty("com.sonyericsson.net.lac");

Now, there is a small but usefull application called Location Alarm written based on the cellID information. Try it out.

Post a comment if you have any queries.

J2ME - HelloWorld Application

Recently I have been experimenting J2ME with my Sony Ericsson w710i. The simplicity of J2ME application is amazing.

Let’s create a J2ME project first. Hope you have already installed the Sun Java Wirless Toolkit for CLDC. Launch the CLDC KToolbar application. Select New Project. Give HelloWorld as Project Name and MIDlet Class name. It will show the Settings screen, go to the Required section and set the MicroEdition-Configuration and MicroEdition-Profile as per your phone. Select OK.

The KToolBar would have generated your application folder structure inside the apps folder of your installation, if it is not there check out in the Documents and Settings folder, under your <login> folder it would have created the j2mewtk folder and stored you application there. You have to put all you sources in the <app_name>\src directory.

Let’s create the main application class file. Create a text file in the name of HelloWorld.java in the src folder. It is really simple, you just need to have a class that extends the MIDlet class, now you have to implement the three basic function startApp, destroyApp, pauseApp. As the name suggests these are called when you Start, Destroy or Pause your application.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet
{
  private Display display;

  protected void startApp()
  {
    display = Display.getDisplay(this);
  }

  protected void destroyApp(boolean unconditional)
  {
    notifyDestroyed();
  }

  protected void pauseApp()
  {
  }
}
The function startApp() is called when you launch your application, the destroyApp() function is called when you close is application, notifyDestroyed() is called to notify the system that the application is closed. The pauseApp() function is called when the application is pause by System due to a interruption like incoming call or sms.

The Display class is the highest object in the application, using display class you can show a Form in the screen.

The basic screen component in J2ME is a Form. A Form can show UI Components, a Form can be shown / hide. Let’s add a Form to the application.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MyFirstApp extends MIDlet
{
  private Display display;
  private Form mForm;

  protected void startApp()
  {
    display = Display.getDisplay(this);

    mForm = new Form("Sample Application");
    mForm.append("Hello World");
    display.setCurrent(mForm);
  }

  protected void destroyApp(boolean unconditional)
  {
    notifyDestroyed();
  }

  protected void pauseApp()
  {
  }
}
That’s it. Now we have added a Form with a title “Sample Application” and text “Hello World”. The form will be displayed when the display.setCurrent(mForm); is called.

Now you can Build the application and Run the application in the simulator. To see your app in the phone select Project->Package->Create Package, it will create the jad and jar file in the <app_name>\bin folder. Download the jar file to your phone via cable or Bluetooth and select the file in the phone. Follow the steps given in your phone to install and run the application.

Getting started with J2ME Application Development

Of late I have started developing J2ME Applications in my spare time. I initially tried to learn Symbian / C++, but I am not quite comfortable with C++ and the Nokia emulators are a pile of crap. Since I own a Sony Ericsson W710i mobile phone, I can test the application on my phone itself :-). This is my first tryst with a interpreted programming language after BASIC in my good old high school days.

To get the ball rolling, you`ll have to download and install the Sun Java Wireless Toolkit 2.5.2 for CLDC. The required JDK/JRE is also listed in the download page itself. There are loads of sample applications to test the water before jumping into development.

The most basic radical differnce for me in J2ME compared against BREW is the memory management. There is no need to manage memory in J2ME, ofcourse there are best practices and guidelines to keep you memory utilization low but it wont crash the phone as BREW apps can do. You have the system garbage collector running whenever the phone is low on memory to reclaim the unused memory.

I follow up with more posts on my experience with J2ME Apps.