2

I'm trying to hook up some functionality with the "shake" event. I used the tutorials here:

http://www.vogella.com/articles/AndroidSensor/article.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/os/Sensors.html

I'm testing with OpenIntents' SensorSimulator. I am able to connect the emulator to the SensorSimulator. When I move the device in the Simulator, the accelerometer readings on the emulator exactly mirror the simulator's.

In my code, I've added some log statements like:

public class ShakeEventListener implements SensorEventListener {

private void getAccelerometer(SensorEvent event) {

    float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];

float accelationSquareRoot = (x * x + y * y + z * z)
        / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
Log.i("onSensorChanged", "accelationSquareRoot: " + accelationSquareRoot);

if (accelationSquareRoot >= 2) //
{
    if (actualTime - lastUpdate < 200) {
    return;
    }
    lastUpdate = actualTime;

        // Shake Detected
    Log.i("onSensorChanged", "Shakie Shakie!");
    }
}

public void onSensorChanged(SensorEvent se) {

    Log.d("onSensorChanged", "X: " + se.values[SensorManager.DATA_X] + 
               " Y: " + se.values[SensorManager.DATA_Y] + 
               " Z: " + se.values[SensorManager.DATA_Z]);

    if (se.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
    {
    getAccelerometer(se);
    }
}
}

The output in log cat doesn't seem to change if I move the device in the simulator. It's always showing these readings (reading keeps updating a bunch of times every minute, but they're always the same):

05-22 15:09:07.717: I/onSensorChanged(686): accelationSquareRoot: 1.0006835
05-22 15:09:08.518: D/onSensorChanged(686): X: 0.0 Y: 9.77622 Z: 0.813417
05-22 15:09:08.518: I/onSensorChanged(686): accelationSquareRoot: 1.0006835

I tried the APIDemos sample sensors.java but that's also registering the same behavior. What could I be missing?

Code Poet
  • 10,615
  • 18
  • 60
  • 94

2 Answers2

3

This is accelerometer code that worked for my app:

// shake
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity


    //Shake
    private final SensorEventListener mSensorListener = new SensorEventListener() {
        int count = 0;
        public void onSensorChanged(SensorEvent se) {

          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter
          if (mAccel > 5) {
              //do something
          }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
      };

Here is the source to how I came to this snippet of code.

Community
  • 1
  • 1
Nick
  • 8,824
  • 32
  • 98
  • 143
  • I had started with the source you linked to, then I tried others (plenty of samples out there). Nothing seems to be working for me. – Code Poet May 25 '12 at 11:50
  • You may need to bite the bullet and buy a real phone for testing. It will save much head ache in the long run as well. – Nick Jun 01 '12 at 18:57
  • It doesn't work with the device either. Thanks for your time. – Code Poet Jun 03 '12 at 04:08
1

In my Game I have implemented this thing from here

The example code is really very simple and easy to understand.

MyActivity.java

public class MyActivity extends Activity
{
    private MyView m_myView;
    private SensorManager m_sensorManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {           
        super.onCreate(savedInstanceState);

        m_myView = (MyView)findViewById(R.id.my_view);

        m_sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        m_sensorManager.registerListener(m_myView,
                SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_GAME);
    }

        @Override
        protected void onPause()
        {
                super.onPause();

                m_sensorManager.unregisterListener(m_myView);
        }

        @Override
        protected void onResume()
        {               
                super.onResume();

                m_sensorManager.registerListener(m_myView,
                        SensorManager.SENSOR_ACCELEROMETER,
                        SensorManager.SENSOR_DELAY_GAME;
        }
}

MyView.java

public class MyView extends View implements SensorListener
{
    private float m_totalForcePrev; // stores the previous total force value

    // do your constructor and all other important stuff here
    // make sure you set totalForcePrev to 0
    // ...

        public void onAccuracyChanged(int arg0, int arg1)
        {
                // I have no desire to deal with the accuracy events
        }

        public void onSensorChanged(int sensor, float[] values)
        {       
                if(sensor == SensorManager.SENSOR_ACCELEROMETER)
                {
                        double forceThreshHold = 1.5f;

                        double totalForce = 0.0f;
                        totalForce += Math.pow(values[SensorManager.DATA_X]/SensorManager.GRAVITY_EARTH, 2.0);
                        totalForce += Math.pow(values[SensorManager.DATA_Y]/SensorManager.GRAVITY_EARTH, 2.0);
                        totalForce += Math.pow(values[SensorManager.DATA_Z]/SensorManager.GRAVITY_EARTH, 2.0);
                        totalForce = Math.sqrt(totalForce);

                        if((m_gameState == STATE_RUNNING) && (totalForce < forceThreshHold) && (m_totalForcePrev > forceThreshHold))
                        {
                                doWrenchWord();
                        }

                        m_totalForcePrev = totalForce;
                }
        }

}

The problem in your side can be,

The code for getAccelerometer(se); function Or you have done some mistake in connecting the SensorSimulator to your emulator, i think you will need to disconnect it and reconnect it check the ip and port also the setting tab and check the sensor is selected/available or not. and also some additional settings for the time delay for update and refresh..just follow them what they have written in the documentation.

MKJParekh
  • 32,883
  • 11
  • 84
  • 97