Sensor, data retrival

peter_budo 0 Tallied Votes 219 Views Share

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

*
 * @author Peter Miklosko
 * @homepage http://www.peterscorner.co.uk
 */

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.sensor.Data;
import javax.microedition.sensor.DataListener;
import javax.microedition.sensor.SensorConnection;
import javax.microedition.sensor.SensorInfo;
import javax.microedition.sensor.SensorManager;

public class SensorData implements DataListener
{
    private SensorConnection sensor;
    private String sensorURL;
    private int x, y, z;
    
    public SensorData()
    {
        sensorURL = getSensorURL();
        x = 0; y = 0; z = 0;
        try
        {
            sensor = (SensorConnection) Connector.open(sensorURL);
            sensor.setDataListener(this, 10);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }        
    }
    
    // Get coordinates
    public int getX(){ synchronized(this) { return x;}}
    public int getY(){ synchronized(this) { return y;}}
    public int getZ(){ synchronized(this) { return z;}}
    
    // Data listener for sensor
    public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost)
    {
        synchronized(this)
        {
            for(int i = 0; i < data.length; i++)
            {
                if(data[i].getChannelInfo().getName().compareTo("axis_x") == 0)
                {
                    x = getAverage(data[i].getIntValues());
                }
                else if(data[i].getChannelInfo().getName().compareTo("axis_y") == 0)
                {
                    y = getAverage(data[i].getIntValues());
                }
                else if(data[i].getChannelInfo().getName().compareTo("axis_z") == 0)
                {
                    z = getAverage(data[i].getIntValues());
                }                
            }           
        }
        
    }
    
    private int getAverage(int[] values)
    {
        int average = 0;
        
        for(int i = 0; i < values.length; i++)
        {
            average += values[i];
        }
        return average/values.length;
    }
    
    // Close connection when MIDlet will be close down
    public void closeConnection()
    {
        try
        {
            sensor.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("Sensor connection closed");
        }
    }

    // Get URL of sensor on the mobile phone
    private String getSensorURL()
    {
        SensorInfo[] si = 
        	SensorManager.findSensors("acceleration", 
        				SensorInfo.CONTEXT_TYPE_USER);
        							
        String URL = new String();
        for(int i=0; i < si.length; i++)
        {
            SensorInfo s = si[i];
            URL = s.getUrl();
        }
        return URL;
    }
}