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
Channels:
getName: X
Data type: TYPE_INT
getUnit: G
getAccuracy: 0.0
Measurement range:
getSmallestValue:-2300.0
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
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
Channels:
getName: X
Data type: TYPE_INT
getUnit: G
getAccuracy: 0.0
Measurement range:
getSmallestValue:-2300.0
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: JSR 256, sony ericsson