Question

alright so I'm new to programming for android, and I think I did something wrong, but I don't know what. I've looked at 3 different tutorials and my code seems to look like theirs. Can anyone tell me what I'm doing wrong? here is my src (altered from a android nehe tutorial).

package cypri.games;


import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;

/**
 * The initial Android Activity, setting and initiating
 * the OpenGL ES Renderer Class @see Lesson02.java
 * 
 * @author Savas Ziplies (nea/INsanityDesign)
 */
public class DGearActivity extends Activity {

    /** The OpenGL View */
    private GLSurfaceView glSurface;
    DGear dGear = new DGear();

    private static final String TAG = "DEBUG";

    SensorManager sensorManager;
    private final SensorEventListener sensorListener = new SensorEventListener(){
        public void onSensorChanged(SensorEvent se) {
             if (se.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                 dGear.playerX = se.values[0];
                 Log.v(TAG, "se.values[0] =" + se.values[0]);
             }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    /**
     * Initiate the OpenGL View and set our own
     * Renderer (@see Lesson02.java)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);

        //Create an Instance with this Activity
        glSurface = new GLSurfaceView(this);
        //Set our own Renderer
        glSurface.setRenderer(dGear);
        //Set the GLSurface as View to this Activity
        setContentView(glSurface);
    }

    /**
     * Remember to resume the glSurface
     */
    @Override
    protected void onResume() {
        Log.v(TAG, "or");
        super.onResume();
        glSurface.onResume();
        sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
    }

    /**
     * Also pause the glSurface
     */
    @Override
    protected void onPause() {
        super.onPause();
        glSurface.onPause();
        Log.v(TAG, "op");
        sensorManager.unregisterListener(sensorListener);
    }

}
Was it helpful?

Solution

  1. Are you running this in the emulator or on an actual device?
  2. If you're running it on an actual device are you sure it has a gyroscope? There are lots of different sensor types and the gyroscope is only one of them. It could very well be one of the others.

Instead of only writing to the log if it's a gyroscope type, try writing the name of the se.sensor when that event fires. That way you'll at least know the event is firing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top