I gathered plenty of samples and tutorials on the matter but once I implement my own version or even just copy the code straight off, I cannot get any values to show up in my log.
I think I am missing one important part but I cannot seem to figure it out.
public class Accelerometer extends Activity {
SensorManager sensorManager;
Sensor accelerometer;
private SensorEventListener sensorEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
update(x, y, z);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
public void update(float x, float y, float z) {
Log.v("semajhan", "x: " + x + " y: " + y + " z: " + z);
}
protected void onResume() {
super.onResume();
sensorManager.registerListener(sensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(sensorEventListener);
}
}
My problem seems to be that I do not know how to really access the accelerometer.
Once I can get these values, I can move forward but I need to understand this first.
Your code seems ok to me. It is similar to working examples I also used to fetch the sensor data. One thing may be problematic. Are you sure you have sensor of that type on your device?
To check that use
function of
SensorManager. You can get full list of sensors available on your device if you usetype = Sensor.TYPE_ALLin function call, and go through all list elements.UPDATE:
Tried your code. It is fully functional. Issue is that display of traces is disabled on your Nexus. To enable traces go to Settings->Applications->Development and check option USB debugging. Without that you get no traces.