Home > Android > Sensor Simulator

Sensor Simulator

The SensorSimulator drives your Android application in real time.

About the SensorSimulator

The OpenIntents SensorSimulator lets you simulate sensor data with the mouse in real time. It currently supports accelerometer, compass, orientation, and temperature sensors, where the behavior can be customized through various settings. TRY IT OUT NOW

Connecting the SensorSimulator with the Android emulator

  • Download the latest sensorsimulator-x.x.x.zip from the download tab and unzip.
  • Start bin/sensorsimulator-x.x.x.jar (Java standalone application).
  • Install bin/SensorSimulatorSettings-x.x.x.apk on your Android emulator.
  • Launch SensorSimulator on the emulator.
  • Enter IP address and socket (port) number into the emulator as shown by the SensorSimulator (see below).
  • Press “Connect” on the emulator.
    • Note: The first time you connect to the SensorSimulator, all sensors are DISABLED automatically. This is, because it is the Android application’s responsibility to enable the sensors before reading values.
  • Press “Enable sensors”.
    • Now you should see the sensor data (see below). With a small delay, they are in sync with the SensorSimulator numbers.
    • Try moving the SensorSimulator phone around with the mouse: The numbers will change in the SensorSimulator and on the Android phone.
    • Note: Only these numbers are transfered between the SensorSimulator and the Android phone. These numbers are the same that you would read out in your application.

Additional settings

Scroll down the settings pane to access the following settings:

  • Gravity: You can set the standard gravity to a different value in units of g=9.8 m/s^2.
  • Magnetic field: Set the magnetic field in units of nano Tesla. You can obtain the values for your current location from the National Geophysical Data Center.
  • Temperature: Set the temperature in centigrade Celsius.

How to use the SensorSimulator in your application

    • Add the external JAR lib/sensorsimulator-lib-x.x.x.jar into your project.
    • Import the sensorsimulator classes
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
    • Replace the following code in onCreate():
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    • by this code
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
    • Connect to the sensor simulator, using the settings that have been set previously with SensorSimulatorSettings:
mSensorManager.connectSimulator();
    • All other code stays untouched. You can find reference code for how to implement sensors in the API demos / OS / Sensors.java.
    • Usually one would (un)register the sensors in onResume() and onStop():
@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_FASTEST);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE), SensorManager.SENSOR_DELAY_FASTEST);
}

@Override
protected void onStop() {
    mSensorManager.unregisterListener(this);
    super.onStop();
}
    • Finally implement the SensorEventListener.
class MySensorActivity extends Activity implements SensorEventListener{

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        int sensor = event.type;
        float[] values = event.values;
        // do something with the sensor data
    }
}
  • Note 1: The OpenIntents class SensorManagerSimulator is derived from the Android class SensorManager and implements exactly the same functions (see Android SensorManager). For the callback, the new SensorEventListener has been implemented that resembles the standardAndroid SensorEventListener.
  • Note 2: Whenever you are not connected to the simulator, you will get real device sensor data: the org.openintents.hardware.SensorManagerSimulator class transparently calls the SensorManager returned by the system service in this case.

Sensor data definition

The coordinate system with XYZ directions is defined as described in the Android SensorManager reference

Accelerometer

The accelerometer values are defined in the accelerometer reference

“Sensor values are acceleration in the X, Y and Z axis, where the X axis has positive direction toward the right side of the device, the Y axis has positive direction toward the top of the device and the Z axis has positive direction toward the front of the device. The direction of the force of gravity is indicated by acceleration values in the X, Y and Z axes. The typical case where the device is flat relative to the surface of the Earth appears as -STANDARD_GRAVITY in the Z axis and X and Z values close to zero. Acceleration values are given in SI units (m/s^2).”

Magnetic field (compass)

The magnetic field sensor values are defined in the magnetic field sensor reference

“Sensor values are the magnetic vector in the X, Y and Z axis, where the X axis has positive direction toward the right side of the device, the Y axis has positive direction toward the top of the device and the Z axis has positive direction toward the front of the device. Magnetic values are given in micro-Tesla (uT)”.

  • NOTE: You can obtain the magnetic field values for your current location from the National Geophysical Data Center in nano Tesla (1000 nano Tesla = 1 micro Tesla).

Orientation

The magnetic field sensor values are defined in the orientation sensor reference

“Sensor values are yaw, pitch and roll Yaw is the compass heading in degrees, range [0, 360[ 0 = North, 90 = East, 180 = South, 270 = West Pitch indicates the tilt of the top of the device, with range -90 to 90. Positive values indicate that the bottom of the device is tilted up and negative values indicate the top of the device is tilted up. Roll indicates the side to side tilt of the device, with range -180 to 180. Positive values indicate that the left side of the device is tilted up and negative values indicate the right side of the device is tilted up.”

 

http://code.google.com

Advertisement
Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.