为什么我的SensorManager.getRotationMatrix总是返回false?

时间:2015-02-18 22:57:31

标签: java android sensor

我通过使用" switch"尝试了很多次。或者"对于"更改代码的语法,但它总是看起来没什么,90%我认为这是因为SensorManager.getRotationMatrix返回false,但为什么呢?我该怎么解决?我试图在Stack中做其他解决方案,但没有一个工作..我正在使用Android Studio并运行我的三星,API为19,我的SDK是21。

BTW,我同时使用两种权限

<uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
    android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

我很困惑,希望有人可以帮助我!

public class MainActivity extends ActionBarActivity implements SensorEventListener {

TextView tv0;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
SensorManager mSensorManager;
Sensor mAccelerometerSensor;
Sensor mMagneticSensor;

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Layout
    LinearLayout lmain = (LinearLayout) findViewById(R.id.layout);
    lmain.setOrientation(LinearLayout.VERTICAL);

    //Title
    tv0 = (TextView) findViewById(R.id.label0);
    tv0.setText("\n COMPASS\n");
    tv0.setTextSize(30);


    //SensorManager
    SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    //Declaring the sensor (AccelerometerSensor & MagneticSensor)
    mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMagneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    //Initialize the Sensor Event Listener (AccelerometerSensor & MagneticSensor)
    mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, mMagneticSensor, SensorManager.SENSOR_DELAY_GAME);




    //setOrientation
    tv1 = (TextView) findViewById(R.id.label1);
    tv1.setText("\n This is Orientation \n");
    tv1.setTextSize(20);

    tv2 = (TextView) findViewById(R.id.label2);
    tv2.setTextSize(15);

    tv3 = (TextView) findViewById(R.id.label3);
    tv3.setTextSize(15);

    tv4 = (TextView) findViewById(R.id.label4);
    tv4.setTextSize(15);
}

@Override
protected void onResume() {
    super.onResume();

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onSensorChanged(SensorEvent event) {


    float[] mGravity = new float[3];
    float[] mGeomagnetic = new float[3];
    float azimuth;
    float pitch;
    float roll;

    if (event.sensor.getType() == event.sensor.TYPE_ACCELEROMETER) {
        for (int i=0;i<3;i++)
            mGravity[i]=event.values[i];
    }

    if (event.sensor.getType() == event.sensor.TYPE_MAGNETIC_FIELD){
        for (int i=0;i<3;i++)
            mGravity[i]=event.values[i];
    }

    if (mGravity != null && mGeomagnetic != null) {
        float[] R = new float[9];
        float[] I = new float[9];
        float[] orientation;
        boolean success = SensorManager.getRotationMatrix(R, null, mGravity, mGeomagnetic);
        if (success) {
            orientation = new float[3];
            SensorManager.getOrientation(R, orientation);
            azimuth = orientation[0]; 
            pitch = orientation[1];
            roll = orientation[2];
            tv2.setText("azimuth: " + Float.toString(azimuth));
            tv3.setText("pitch: " + Float.toString(pitch));
            tv4.setText("roll: " + Float.toString(roll));
        }

    }

}

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

}

}

0 个答案:

没有答案