Monday, April 6, 2009

GPS Co-ordinate viewer

This application must run with GPS enable mobile phones.
make sure to keep Gps1.png and Gps2.png (small images to show gps working or not)


import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;


import javax.microedition.location.LocationProvider;

/**
*
* @author Susith
*/
public class LocationListenerImpl implements LocationListener{

public static String lat_check = "", lon_check = "";

public void locationUpdated(LocationProvider provider, Location location) {
lat_check = "";
lon_check = "";
if (location.isValid()) {

double longitude = location.getQualifiedCoordinates().getLongitude();
double latitude = location.getQualifiedCoordinates().getLatitude();

MyMidlet.setLon(Double.toString(longitude));
MyMidlet.setLat(Double.toString(latitude));

}
}

public void providerStateChanged(LocationProvider arg0, int arg1) {
throw new UnsupportedOperationException("Not supported yet.");
}

}




import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;

/*
* To change this template, choose Tools Templates
* and open the template in the editor.
*/
import javax.microedition.location.LocationProvider;
import javax.microedition.midlet.MIDlet;

/**
*
* @author Susith
*/
public class MyMidlet extends MIDlet implements Runnable{
private static LocationProvider locationProvider;
private static String lat = "";
private static String lon = "";
private int count;

Form frm = null;
Thread th = null;

public void startApp() {
try {
frm = new Form("GPS Co-ordinates");
startLocationUpdate();
th = new Thread(this);
th.start();
} catch (Exception ex) {
Alert alert = new Alert("Error Message");
alert.setString("Exception "+ex.getMessage());
alert.setTimeout(3000);
alert.setType(AlertType.ALARM);
Display.getDisplay(this).setCurrent(alert);
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
public void run() {
while (true) {

count+=5;
frm = new Form("test");
StringItem strlat = new StringItem("LAT : ", lat);
StringItem strlon = new StringItem("LON : ", lon);
if (!LocationListenerImpl.lat_check.equals("") !LocationListenerImpl.lon_check.equals("")) {
try {
ImageItem img = new ImageItem("", Image.createImage("/gps1.png"), 0 0, "");
frm.append(img);
} catch (IOException ex) {
//todo handle error
}
} else {
try {
ImageItem img = new ImageItem("", Image.createImage("/gps2.png"), 0 0, "");
frm.append(img);
} catch (IOException ex) {
//todo handle error
}
}
frm.append(strlat);
frm.append(strlon);
frm.append(new StringItem("Count: ",String.valueOf(count) ));
Display.getDisplay(this).setCurrent(frm);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}

private static boolean startLocationUpdate() {
boolean retval = false;


try {
locationProvider = javax.microedition.location.LocationProvider.getInstance(null);

if (locationProvider != null) {
// Only a single listener can be associated with a provider, and unsetting it
// involves the same call but with null, therefore, no need to cache the listener
// instance request an update every second.
locationProvider.setLocationListener(new LocationListenerImpl() , 1, 1, 1);
retval = true;
}
} catch (javax.microedition.location.LocationException le) {
System.err.println("Failed to instantiate the LocationProvider object, exiting...");
System.err.println(le);
System.exit(0);
}


return retval;
}

public static void setLat(String lat) {
MyMidlet.lat = removeTail(lat);
}

public static void setLon(String lon) {
MyMidlet.lon = removeTail(lon);
}

private static String removeTail(String string){
return string.length()>5?string.substring(0, 5):string;
}

}

Thursday, March 12, 2009

Authenticate mobile phone application Garanteed MSISDN

Problem
It is not possible read SIM (MSISDN) using j2me
That means there is no guarantee of user(In secure application)
In is not possible to detect change of SIM card by j2me application

Reason
You can copy an install j2me application more than one mobile phone.
If you provide license key it is possible to activate both j2me applications
W@P or GPRS connection not provide any user specific information while access internet.

Solution
Messages can be received via SMS by user defined port. Those messages are not receive to Inbox. However we can read those messages by j2me. Since SMS receive (bounded) to MSISDN (SIM) we can guarantee the program activation is done for valid (authenticated mobile) user.

Drawback
send SMS in each time of authentication may costly.
To receiver SMS
1 Application must be in running state.
2 Or we can start application on SMS arrived.
for 2nd option application manager(mobile phone) ask to confirm run application and we must accept. Otherwise massage will discard.


NOTE: don't send any valuable data over plane text SMS. It should use private key public key encryption or some other method to make it secure.

/* Create Connection */
public void connectSMSServer() {
try {
messageConnection messageConnection = (MessageConnection)Connector.open("sms://:" + 7777);
//we specify port number 7777 t0 receiving port
messageConnection.setMessageListener(this);
} catch (Exception e) { }
}

/* Recieve SMSmessage */
public void receiveSMSMessage() {
try {
Message message = messageConnection.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage)message;
} else {
//process the message an do nessosory operations here...
}
} catch (Exception e) { }
}
more sample codes will come soon with next edit

Tuesday, February 17, 2009

J2ME Device Emulators


This shows Sun java device emulator.


We can add different skins to J2ME Wireless Toolkit emulator so that it looks and behaves (Act) as exact mobile device model.

Sun Java Wireless Toolkit (emulator) ; the one most commonly use emulator


For mobile application developers can not have all models and versions of mobile phone. So the best option is to have all types of mobile phone emulators to test.

New Skins and customize emulator: refer Basic Customization Guide

motorola

For Nokia there are 2 major versions

Nokia S60_5th_Edition (With touch screen support)

Nokia S60_3rd_Edition

SonyErrocson

Sprint (Samsung Nokia LG and meny other)

blackberry