简体   繁体   中英

Why am I not getting data from accelerometer sensor?

Problem Description:

My friend and I are teaching ourselves android development. We debugged the other classes, like game physics and manually updated values, which drew the marble going diagonal across our board. When we changed it back to read in accelerometer data, the marble quit moving. Hence, we are going to include the class where we believe the problem is occurring. Basically, our view class in android development.

We are teaching ourselves android development and we can't understand why data from the accelerometer, which we call mSensorX and mSensorY is not being updated. What we include below is our view class. Please note that we are developing for android 3.1. However, we are using all the latest goodies from android in eclipse.

package com.example.marblez;


import com.example.marblez.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

public class MarblezView extends View implements SensorEventListener{


//Sensor Stuff
private SensorManager mSensorManager;
private Display mDisplay;


//Accelerometer sensor stuff 
 private Sensor mAccelerometer;
 private float mSensorX;
 private float mSensorY;

 //Variables related to time 
 private long mCpuTimeStamp;
 private long mSensorTimeStamp;
 private WindowManager mWindowManager;



//Accelerometer buffer, such that slight movement will roll the marble 
private float sensor_buffer = 0;

//Create the canvas 
private Canvas mCanvas; 
private MarblezBackground background;

//Create the marble
private Marblez ball;

//Constructor for Marblez View 
@SuppressWarnings("deprecation")
public MarblezView(Context context, Activity activity){
    super(context);

    //Setup sensor stuff
    mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    mDisplay = mWindowManager.getDefaultDisplay();

    //Set up Maze 
    background = new MarblezBackground(activity);

    //Create our rolling little friend :-) 
    ball = new Marblez(activity);

}

 @Override
 public void onSensorChanged(SensorEvent event) {
     if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
         return;
     /*
      * record the accelerometer data, the event's timestamp as well as
      * the current time. The latter is needed so we can calculate the
      * "present" time during rendering. In this application, we need to
      * take into account how the screen is rotated with respect to the
      * sensors (which always return data in a coordinate space aligned
      * to with the screen in its native orientation).
      */

     switch (mDisplay.getRotation()) {
         case Surface.ROTATION_0:
             mSensorX = event.values[0];
             mSensorY = event.values[1];
             break;
         case Surface.ROTATION_90:
             mSensorX = -event.values[1];
             mSensorY = event.values[0];
             break;
         case Surface.ROTATION_180:
             mSensorX = -event.values[0];
             mSensorY = -event.values[1];
             break;
         case Surface.ROTATION_270:
             mSensorX = event.values[1];
             mSensorY = -event.values[0];
             break;
     }

     mSensorTimeStamp = event.timestamp;
     mCpuTimeStamp = System.nanoTime();
 }


//Automatic call
@Override
public void onDraw(Canvas canvas){
    mCanvas = canvas;

    //Draw the maze
    background.drawMaze(canvas);

    //Get the x and y sensor data and other goodies
    final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);

    //Draw the marble 
    ball.drawMarble(canvas, mSensorX, mSensorY, now);

    //Invalidate so it draws again 
    invalidate();

}

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

}

We got a lot of our ideas and methods from an open source google project called accelerometer play. However, those familiar with that code can see we went at lengths to try to simplify naming conventions, methods, and calls.

Basically, we would appreciate your help. It's like the sensor is "there", but for practical purposes it is never used. For example, it is very telling that view never calls the method onSensorChanged when we added a debug marker to it. Hence, another case in that our accelerometer really isn't working.

you need to register your instance at the sensor manager like this:

mSensorManager.registerListener(this, mAccelerometer,
                                SensorManager.SENSOR_DELAY_NORMAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM