Wednesday, February 28, 2007

Code : how to record data in j2me

private RecordStore recordStore=null;
byte [] data=.....
try{
int id = recordStore.addRecord(data,0,data.length);

}catch(RecordStoreException rse){

}
where data will be recorded?

Example:
In Nokia6600, E:\System\Midlets\[1012...]\rms.db

E : Disk (Memory Card)
[1012...] : midlet number

You can explore your device with FExplorer and see that when u add a new record size of rms.db will increase.

Tuesday, February 27, 2007

Code : how to reach camera in j2me

import javax.microedition.media.*;
import javax.microedition.media.control.*;

player=Manager.createPlayer("capture://video");
player.realize();
videoControl=(VideoControl)(player.getControl("VideoControl"));

videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,
canvas);

player.start();

byte[] image = videoControl.getSnapshot(null);

Monday, February 26, 2007

how to store data and retrieve

Here is an overview about javax.microedition.rms package which is used for storing data and later retrieving it.

Thursday, February 22, 2007

Installing and Executing j2me applications in real mobile phones

Every mobile phone dont support every API and Configuration!

Remembering this basic rule rescue you from errors like "File Manager Closed" "Wrong version".

You can see which device supports which API and configuration in J2mepolish website.

For example if you(like me) try to install a application which has CLDC 1.1 version it may not be installed in Nokia 6600 which supports CLDC 1.0.

How to install??

open file manager -->select jar file-->and installing...

Wednesday, February 21, 2007

next step

The codes below simply send data that user types to server and receive message from server which is echos of it.Next step doing a file browser, browsing a photo and sending it via HttpConnection.If you send byte you can send everything so i ll send image in this way.

Example Code:HttpConnection in J2ME #2

use SendDataViaHttp.java and HttpConnector.java together
/**
*
* @author sezera.blogspot.com
* @version 0010
*/
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HttpConnector extends Thread{

private String url="http://localhost:8084/YourWebApp/YourServlet";
private String msg;
private SendDataViaHttp midlet;
/** Creates a new instance of HttpConnector */
public HttpConnector(SendDataViaHttp midlet,String msg) {
this.midlet =midlet;
this.msg = msg;
}
public synchronized void run(){
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
try{
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
os = c.openOutputStream();
os.write(msg.getBytes());

is = c.openInputStream();

int contentLength = (int)c.getLength();
if (contentLength == -1) contentLength = 255;
byte[] raw = new byte[contentLength];
int length = is.read(raw);
String response = new String(raw,0,length);
midlet.get_response(response);
os.close();
is.close();
c.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}

Example Code:HttpConnection in J2ME #1

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

/**
*
* @author sezera.blogspot.com
* @version 0001
*/
public class SendDataViaHttp extends MIDlet implements CommandListener{

private Form sendForm;
public TextField sendTextField;
public TextField responseTextField;
private TextBox askUserTextBox;
private Command sendCommand;
private Command exitCommand;
private String msgString;
private String responseString;

public SendDataViaHttp(){
}

public void startApp() {
getDisplay().setCurrent(get_sendForm());
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
public Display getDisplay()
{
return Display.getDisplay(this);
}
public Form get_sendForm()
{
if(sendForm == null){
sendForm = new Form(null,new Item[]{get_sendTextField(),get_responseTextField()});
sendForm.addCommand(get_sendCommand());
sendForm.addCommand(get_exitCommand());
sendForm.setCommandListener(this);
}
return sendForm;
}
public TextField get_sendTextField(){
if(sendTextField == null){
sendTextField = new TextField("Your Message",null,20,TextField.ANY);
}
return sendTextField;
}
public TextField get_responseTextField(){
if(responseTextField == null){
responseTextField = new TextField("Response From Server",null,20,TextField.ANY);
}
return responseTextField;
}
public Command get_sendCommand()
{
if(sendCommand == null){
sendCommand = new Command("Send",Command.OK,1);
}
return sendCommand;
}
public Command get_exitCommand()
{
if(exitCommand == null){
exitCommand = new Command("Exit",Command.EXIT,2);
}
return exitCommand;
}
public void commandAction(Command command,Displayable displayable){
if(displayable == sendForm){
if(command == sendCommand){
//send written data to JavaServlet via HttpConnection
msgString = new String(sendTextField.getString());
HttpConnector conn = new HttpConnector(this,msgString);
conn.start();
}else if(command == exitCommand){
//exit Midlet
exitMiddlet();

}
}else
{
getDisplay().setCurrent(get_sendForm());
}
}
public void exitMiddlet()
{
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
public void get_response(String response)
{
responseTextField.setString(response);
}
}

Deadlock problem in J2ME networking

Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
The warning above says if you simply do a httpconnection in J2Me memory,you may cause a deadlock.So we should Java Threads to connect server and send/receive data.

How To Transfer Data Via HttpConnection

You can send data from your mobile phone via Http Connection.In our thesis we will use this method.Our j2me interface is:
javax.microedition.io

Interface HttpConnection

How to open a connection?

private HttpConnection c = null;
private InputStream is = null;
private OutputStream os = null;
private static String ="http://localhost:8084/YourWebApp/YourServlet/

8084:Default TomCat Port in NetBeans IDE

c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
os = c.openOutputStream();
os.write(msg.getBytes());
os.close();
c.close();

Add try catch for bold area.


Sunday, February 18, 2007

MIDLET

In MIDP, when you develop a program actually you develop a midlet.
Midlet : javax.microedition.midlet.MIDLET class, which contains main application management functions, is a interface between application management software(AMS) and MIDP application.
Every MIDP application must extend MIDLET class

As you see below our HelloPhone midlet extends from Midlet class and implements CommandListener.

startApp() : When program starts it call this method but it maybe recalled again and again during program.
pauseApp() : Simply pauses midlet.When user wants to continue executing program midlet invokes startApp()
destroyApp(boolean unconditional) : Ends program and release sources.If boolean unconditional set to true middlet have to release sources immediately.But if it is set to false middlet can throw "MIDletStateChangeException" to continue executing.


Example Code : Hello World!

As you guess, our first mobile program is HelloWorld which simply say HelloWorld lol

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

/**
*
* @author sezera.blogspot.com
* @version 0000
*/
public class HelloPhone extends MIDlet implements CommandListener{
private Form helloForm;
private Command okCommand;
private StringItem msgStringItem;

public void startApp() {
getDisplay().setCurrent(get_helloForm());
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command,Displayable displayable)
{
if(displayable == helloForm){
if(command == okCommand){
//exit Midlet
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
}
}
public Form get_helloForm()
{
if(helloForm == null){
helloForm = new Form(null,new Item[]{get_helloStringItem()});
helloForm.addCommand(get_okCommand());
helloForm.setCommandListener(this);
}
return helloForm;
}
public Display getDisplay()
{
return Display.getDisplay(this);
}
public StringItem get_helloStringItem() {
if (msgStringItem == null) {
msgStringItem = new StringItem(null,"Hello World!");
}
return msgStringItem;
}
public Command get_okCommand() {
if (okCommand == null) {
okCommand = new Command("Okey", Command.OK,1);
}
return okCommand;
}

}

MIDP

In the previous post, there is a word called MIDP in this figure.So what is MIDP?
MIDP(Mobile Information Device Profile) :Is a widely-common java me library for CLDC devices.
Java packages belong to MIDP 1.0
* java.io
* java.lang
* java.util
* javax.microedition.io
* javax.microedition.lcdui
* javax.microedition.midlet
* javax.microedition.rms
Java packages belong to MIDP 2.0
* MIDP 1.0 packages
* javax.microedition.lcdui.game
* javax.microedition.media
* javax.microedition.media.control
* javax.microedition.pki

J2ME

Think that you want to develop a java platform for mobile devices such as mobile phones,PDAs etc.How do you do it?You probably think simplifying J2SE and adding special attributes for mobile devices to it like Sun exactly did.Look at this platform.

JVM is java virtual machine as you know. KVM(first version is announced in javaOne conference,1999) is a JVM minimized and adopted for mobile phones and entry level PDAs as you see on image.CDC and CLDC are configurations and shows which devices will be used on this platform.

CDC (Connected Device Configuration) :High-end PDAs,TV set-top boxes,embedded devices
CLDC (Connected Limited Device Configuration) :Mobile phones and entry-level PDAs

Because we will use a mobile phone(old but useful nokia 6600) our platform configuration is CLDC so i ll examine this.

Java packages belong to CLDC:
  • java.io
  • java.lang
  • java.lang.ref
  • java.util
  • javax.microediton.io

Tuesday, February 06, 2007

NetBeans IDE 5.5

I have to say that i found Java Community NetBeans IDE 5.5 amazing and we ll use this tool for doing thesis.Jakarta Tom Cat,Java Servlet,J2ME...we begin to work.Nowadays i m working on this IDE to transfer a photo image from a mobile phone to a pc program. So i am going to write about this and leaving image processing for a while.

here again

After a long time i begin to study my thesis again.Forgive me for delay:)Although nobody read this blog except me i need having some responsibility lol