Ady Wicaksono Daily Activities

Archive for the ‘j2me’ Category

When will your SBS Transit Bus arrive, check on your mobile phone?

with 3 comments

Previously I create an Iris next bus application using perl, now I create a J2ME application so you can now access Iris next bus using your mobile phone, everywhere, everytime, no matter what operator you use as long as you have access to GPRS network.

Why? Because I need it and I want to make it cheap service. How it works? Simply see the application on my Nokia N73.

1. Step 1, input your bus number and bus stop number
fescr000.jpg

2. Get the information

fescr001.jpg

fescr002.jpg

So, here is the J2ME source code, it’s very simple, dummy code, no such optimization, the I/O network is blocking, but it’s all that I need.


// Simple IRIS Next Bus Application
// version 0.1 2/26/2008
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class irisnextbus extends MIDlet  implements CommandListener{

     private Display display;
     private Command openCmd =
	   new Command("Check Sched.",Command.OK,1);
     private Command exitCmd =
	   new Command("Exit",Command.EXIT,2);
     private Command backCmd =
	   new Command("Back",Command.EXIT,2);

     private Form f, f2;
     private TextField t1, t2;
     private Alert a1;

     public  irisnextbus(){}

     public void startApp(){
        display    = Display.getDisplay(this);
		f          = new Form("IRIS next bus");
        t1         = new TextField("Bus Number","",4,TextField.NUMERIC);
        t2         = new TextField("Bus Stop Number","",5,TextField.NUMERIC);
		f.append(t1);
		f.append(t2);

		f.addCommand(openCmd);
		f.addCommand(exitCmd);
		f.setCommandListener(this);
		display.setCurrent(f);
     }

     public void checkSched(){
	     openSched();
     }

     public void openSched(){
     	 HttpConnection c = null;
         InputStream is   = null;
         String url2open;
		 int responsecode;
		 int ch;

		 if(t1.getString().trim().equals("") || t2.getString().trim().equals("")){

		 }else{
			 StringBuffer info = new StringBuffer("");
	     	try{
	           url2open = "http://202.147.193.229/iris/next.cgi?b1=" +
				         t1.getString().trim() + "&b2=" + t2.getString().trim();
	     	   c  = (HttpConnection)Connector.open(url2open.trim());
			   c.setRequestMethod(HttpConnection.GET);
			   is = c.openInputStream();
		       responsecode = c.getResponseCode();
			   String str   = null;
		   	   if(responsecode == 200){
					int length = (int) c.getLength();
					if (length != -1){
						byte incomingData[] = new byte[length];
						is.read(incomingData);
						str = new String(incomingData);
					}else{
						ByteArrayOutputStream bytestream =
							  new ByteArrayOutputStream();

						while ((ch = is.read()) != -1)
						{
						  bytestream.write(ch);
						}
						str = new String(bytestream.toByteArray());
						bytestream.close();
					}
					display.setCurrent(new Alert("Info",
					str,null,AlertType.INFO),f);
		       }
	     	}catch(IOException e){

	     	}finally{
				try{
				   if (is != null){
						 is.close();
				   }
				   if (c != null){
						 c.close();
				   }	

				}catch(Exception e){}
			}
	     }
     }  	 

	 public void exitApp(){
        destroyApp(true);
     }

     public void commandAction(Command c, Displayable d){
	    String lbl = c.getLabel();
		if(lbl.equals("Exit")){
		   exitApp();
		}else if(lbl.equals("Check Sched.")){
		   checkSched();
		}else if(lbl.equals("Back")){
			display.setCurrent(f);
		}
     }

	 public void pauseApp() {
	 }

	 public void destroyApp(boolean unconditional){
	    notifyDestroyed();
     }
}
Advertisements

Written by adywicaksono

February 26, 2008 at 6:13 pm

Posted in j2me

Sending SMS to J2ME application

with 14 comments

The Wireless Messaging API (WMA) is an optional package for J2ME that
provides platform-independent access to wireless communication resources
like Short Message Service (SMS). If URI for HTTP is defined by http://
then URI for SMS resource is defined by sms://. Example resource identifier
for SMS is sms://:50000, means the port of SMS is 50000.

For example you have a J2ME application that listen to specific port, then
how to send SMS to the receiver would be this J2ME application? Simple approach
is to create another J2ME application that act as sms sender. But what is the
real PDU (protocol data unit) involved for sending it?

If you see GSM 03.40 specification, you will find that we need to set some
special UDH (user data header) inside the 140 octet of TP-UD. This UDH is like this
060504C3500000, the details is:
0x06 : TP-UDHL, means the user data header length -> length of “0504C3500000” = 6 octet
0x05 : Application port addressing scheme, 16 bit address, means we have port range up
to 2^16 = 65536, so port 50000 is in the range
0x04 : Length of application port addressing scheme -> length of “C3500000” = 4 octet
0xC350: Destination port, convert 0xC350 to decimal, you will have 50000
0x0000: Source port

For working example, please run this WMA java example taken from Sun JME toolkit
On the phone side we see that application is waiting for incoming SMS at port 50000

choose-sim-receiver.jpg
sim-receiver-listening-for-incoming-sms.jpg

We’re now sending the SMS using AT+command, here’s the PDU:

55010A91560996658600F5A71F060504C3500000496E6920636F6E746F6820666C61736820534D53206C686F

To break down that PDU please refer to GSM 03.40 :), meanwhile I will focus on the 1st byte
that inform us that this PDU contain UDHI (user data header indicator) and the TP-UD of SMS
which is 1F060504C3500000496E6920636F6E746F6820666C61736820534D53206C686F

1F: TP-UDL (user data length)
060504C3500000 : this is the UDH (see above)
496E6920636F6E746F6820666C61736820534D53206C686F : this is the data

and here’s the screenshot at the mobile phone after receiving this SMS

sim-receiver-receive-sms.jpg

Written by adywicaksono

February 10, 2008 at 6:52 am

Posted in j2me, SMS