You weren't wrong, just missing a lot of code. Here's the relevant parts of my code.
//Variables
private SensorManager mSensorManager;
private float[] mAccelerometerReading;
private float[] mMagneticFieldReading;
private float[] mRotationMatrix = new float[16];
private float[] mRemapedRotationMatrix = new float[16];
private float[] mOrientation = new float[3];
//Constructor
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
//Sensor reading callback
public void onSensorChanged(SensorEvent event)
{
switch (event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
{
mAccelerometerReading = event.values.clone();
break;
}
case Sensor.TYPE_MAGNETIC_FIELD:
{
mMagneticFieldReading = event.values.clone();
break;
}
}
if(mAccelerometerReading != null && mMagneticFieldReading != null &&
SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelerometerReading, mMagneticFieldReading))
{
SensorManager.remapCoordinateSystem(mRotationMatrix,
SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mRemapedRotationMatrix);
SensorManager.getOrientation(mRemapedRotationMatrix, mOrientation);
}
}
//Where you want to read the orientation
if(mOrientation != null)
{
//do stuff with mOrientation[0]
//do stuff with mOrientation[1]
//do stuff with mOrientation[2]
//to control a ball i used
//mOrientation[2] for horizontal movement and
//- mOrientation[1] for vertical movement
}