« Home | 5 Ways to SMS for free » | Sony Ericsson Virtual Lab » | Suggestions to make Twitter better » | MotoID launched in Brazil » | EA Mobile appoints new marketing manager » | IPhone unlock with TurboSIM » | Jaiku Mobile v.80 is OUT » | Samsung files game phone patents » | Digital Chocolate Buys Gaming Studios In India and... » | Symbian Slingplayer In Private Beta » 

Tuesday, August 28, 2007 

Using the Sensor API with the accelerometer for W910 in Java ME

New Java™ Platform 8 (JP-8) phones introduce, among other things, programmatic support for sensors (JSR 256). This example shows how to utilize the sensor API to monitor the built in accelerometer found in the W910 Walkman® phone. This API can, in the same manner, be used for other sensors if available.

Download example MIDlet>>

Sensors like an accelerometer can easily be incorporated into games, enriching the gaming experience on mobile phones. Information regarding available sensors can be retrieved through the use of the following API calls:

String sensorVersion = System.getProperty("microedition.sensor.version");
SensorInfo[] si = SensorManager.findSensors(null, null);

Information contained in the sensor information array specifies exactly what type of information can be retrieved divided into different channels (in this case acceleration in three dimensions: x, y and z). The following is an example of the type of information provided by the accelerometer:

getDescription: ThreeChannelAccelerometer
getUrl: sensor:acceleration;contextType=user;model=ST_LIS302DL;location=inside
getName: X
Data type: TYPE_INT
getUnit: G
getAccuracy: 0.0
Measurement range:
getLargestValue: 2300.0
getResolution: 0.015625
getName: Y

getName: Z

In order to utilize the sensor, a connection using the URL specified in the sensor information has to be opened. Once the connection is established, you can either register a data listener (a class overriding DataListener) for automatic callbacks or manually poll the sensor.

SensorConnection sensor = (SensorConnection)Connector.open(URL);
sensor.setDataListener(this, 10);

Sensors like the accelerometer above sample continuously at a fixed rate specified in the sensor information above. The accelerometer has specified a rate of 100Hz as we can see from the sensor information. Specifying the buffer size of a data listener or a "getData()" function call have the effect that the sensor will fill up as many "Data" objects as specified before calling the callback function or returning from the "getData()" function call. In our case this will result in 10 data samples for each call to our data listener, thus it is called 10 times per second (100Hz * 10 samples = 1/10 seconds).

source: Sony Ericsson Developer World

Labels: ,

Pedro "K2" Macêdo

About me

  • I'm Pedro Macedo
  • From
My profile

Sponsored Links



I recommend