消逝的光芒游戏:SensorSimulator - openintents - Sensor Simulator for simulating sensor data in real time. - Make Android applications work together. - Google Project Hosting

来源:百度文库 编辑:偶看新闻 时间:2024/04/20 12:01:22

Sensor Simulator

(scroll down for detailed description)

 

The SensorSimulator drives your Android application in real time.

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

 

  • 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 standard Android 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

"Sensorvalues are acceleration in the X, Y and Z axis, where the X axis haspositive direction toward the right side of the device, the Y axis haspositive direction toward the top of the device and the Z axis haspositive direction toward the front of the device. The direction of theforce of gravity is indicated by acceleration values in the X, Y and Zaxes. The typical case where the device is flat relative to the surfaceof the Earth appears as -STANDARD_GRAVITY in the Z axis and X and Zvalues 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

"Sensorvalues are the magnetic vector in the X, Y and Z axis, where the X axishas positive direction toward the right side of the device, the Y axishas positive direction toward the top of the device and the Z axis haspositive direction toward the front of the device. Magnetic values aregiven 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

"Sensorvalues are yaw, pitch and roll Yaw is the compass heading in degrees,range [0, 360[ 0 = North, 90 = East, 180 = South, 270 = West Pitchindicates 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 andnegative values indicate the top of the device is tilted up. Rollindicates 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 upand negative values indicate the right side of the device is tilted up."