This is simple example how to get data reading from mobile phone's in-built sensor with JSR-256 and Java Microedition
SensorMidlet.java
/**
*
* @author Peter Miklosko
* @homepage http://www.peterscorner.co.uk
*/
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
public class SensorMidlet extends MIDlet implements CommandListener
{
private Canvas mCanvas;
private Command exitCommand;
private SensorData sd = new SensorData();
public void startApp()
{
Display display = Display.getDisplay(this);
if(mCanvas == null)
{
mCanvas = new SensorCanvas(sd);
exitCommand = new Command("Exit", Command.EXIT, 0);
mCanvas.addCommand(exitCommand);
mCanvas.setCommandListener(this);
}
display.setCurrent(mCanvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d)
{
if(c == exitCommand)
{
sd.closeConnection();
destroyApp(true);
notifyDestroyed();
}
}
}
SensorCanvas.java
/**
*
* @author Peter Miklosko
* @homepage http://www.peterscorner.co.uk
*/
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class SensorCanvas extends Canvas
{
private String mMessage = "Press the key!";
private int mKeyCode;
private SensorData sd;
private int xCor, yCor, zCor;
public SensorCanvas(SensorData sData)
{
sd = sData;
}
public void paint(Graphics g)
{
int w = getWidth();
int h = getHeight();
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
int fh = g.getFont().getHeight();
int y = h / 2 - fh * 2;
g.setColor(0x000000);
g.drawString(mMessage, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
y += fh;
if(mKeyCode != 0)
{
// Get sensor data
xCor = sd.getX();
yCor = sd.getY();
zCor = sd.getZ();
g.drawString("X coordinate: " + xCor, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
y += fh;
g.drawString("Y coordinate: " + yCor, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
y += fh;
g.drawString("Z coordiante: " + zCor, w / 2, y, Graphics.HCENTER | Graphics.BOTTOM);
}
}
protected void keyPressed(int keyCode)
{
mMessage = "KeyPressed()";
if(getGameAction(keyCode) == GAME_A)
{
mKeyCode = keyCode;
repaint();
}
}
protected void keyReleased(int keyCode)
{
mMessage = "keyReleased()";
if(getGameAction(keyCode) == GAME_A)
{
mKeyCode = keyCode;
repaint();
}
}
}
SensorData.java
More in detail information on the code can be found on my web site