无法获得加速度计来感应震动事件

时间:2012-05-22 09:52:21

标签: android android-sensors

我正在尝试使用“shake”事件来连接一些功能。我在这里使用了教程:

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

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

我正在使用OpenIntents'SensorSimulator进行测试。我能够将仿真器连接到SensorSimulator。当我在模拟器中移动设备时,模拟器上的加速度计读数与模拟器完全相同。

在我的代码中,我添加了一些日志语句,如:

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);
    }
}
}

如果我在模拟器中移动设备,log cat中的输出似乎没有改变。它总是显示这些读数(读数每分钟都会更新一次,但它们总是一样):

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

我尝试了APIDemos示例sensors.java,但也注册了相同的行为。我能错过什么?

2 个答案:

答案 0 :(得分:3)

这是适用于我的应用的加速度计代码:

// 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) {

        }
      };

以下是source我是如何看到这段代码的。{/ p>

答案 1 :(得分:1)

在我的游戏中,我实现了这个from here

示例代码非常简单易懂。

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;
                }
        }

}

你身边的问题可能是,

getAccelerometer(se);函数的代码或者您在将SensorSimulator连接到模拟器时遇到了一些错误,我认为您需要断开它并重新连接它检查ip和端口以及设置选项卡并检查传感器是否已选择/可用。以及更新和刷新时间延迟的一些其他设置。只需按照他们在文档中编写的内容进行操作。