I am developing a compass app from this tutorial but with some things different to the tutorial, for example i don´t want to draw the compass, just put the values in a EditText, the thing is that is not working or really I don´t know what is happening... I try to debug the application but the sensors do not respond and the log don´t show any errors... this is what i try:
public class MainActivity extends Activity implements SensorEventListener {
float azimut;
private SensorManager mSensorManager;
Sensor accelerometer;
Sensor magnetometer;
float[] mGravity;
float[] mGeomagnetic;
private EditText edTLatitud;
private EditText edTLongitud;
private EditText edTCompass;
private EditText edTDirecc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edTCompass = (EditText)this.findViewById(R.id.edTxtBrujula);
edTDirecc = (EditText)this.findViewById(R.id.edTxtBrujdireccion);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if(mGravity != null && mGeomagnetic != null){
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R,I, mGravity, mGeomagnetic);
if(success){
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimut = orientation[0];
UpdateCompassOnScreen(azimut);
edTCompass.setText((int)azimut);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
// Show the value on the application of the compass with the direction
public void UpdateCompassOnScreen(float value)
{
if (value >= 338 && value <= 22.9999)
{
edTDirecc.setText("° N");
}
else if(value >= 23 && value <= 67.9999)
{
edTDirecc.setText("° NE");
}
else if (value >= 68 && value <= 112.9999)
{
edTDirecc.setText("° E");
}
else if(value >= 113 && value <= 157.9999)
{
edTDirecc.setText("° SE");
}
else if(value >= 158 && value <= 202.9999)
{
edTDirecc.setText("° S");
}
else if(value >= 203 && value <= 247.9999)
{
edTDirecc.setText("° SW");
}
else if(value >= 248 && value <= 292.9999)
{
edTDirecc.setText("° W");
}
else if (value >= 293 && value <= 337.9999)
{
edTDirecc.setText("° NW");
}
edTCompass.setText(String.valueOf(value));
}
thanks in advice.
I think getRotationMatrix returns a Matrix.
Here. There's stuff in API demos. Pro Android 2 has stuff also.
public void onSensorChanged(SensorEvent event) {