陀螺仪传感器apk

时间:2014-12-11 10:29:49

标签: android

我试图使用下面的apk从HAL读取陀螺仪值。 apk成功启用陀螺仪传感器并从内核读取和处理事件,adb-log中没有崩溃,但即使在日志或屏幕上也无法读取任何数据。 对于加速度计和磁场,我得到了适当的数据。(在kitkat板上) 我想知道为什么数据丢失。为什么我不能读取任何x值,如果它能够成功读取事件?

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MySensorActivity extends Activity implements SensorEventListener {

    private TextView tv;
    private SensorManager mSensorManager;
    private Sensor mLightSensor,mGyroSensor;

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

        tv= (TextView)findViewById(R.id.txt2);
        // Get an instance of the sensor service
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        mGyroSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

        PackageManager PM= this.getPackageManager();
        boolean gyro = PM.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE);
        boolean light = PM.hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT);

        if(gyro){

            if(light){
                Toast.makeText(getApplicationContext(),"Both light and gyroscope sensors are present", Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(getApplicationContext(),"Only gyroscope sensor is present", Toast.LENGTH_LONG).show();

        }
    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {

        //float lux = event.values[0];
        float angularXSpeed = event.values[0];
        tv.setText("Angular X speed level is: " + "" +angularXSpeed); 
    }

    @Override
    protected void onResume() {
        // Register a listener for the sensor.
        super.onResume();
        mSensorManager.registerListener(this, mGyroSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        // important to unregister the sensor when the activity pauses.
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

}

1 个答案:

答案 0 :(得分:1)

您必须为要使用的所有传感器注册侦听器:

// Declare as static at start of class (Global Variables)

    static SensorManager sensorManager;
    static Sensor mAccelerometer;
    private Sensor mMagnetometer;
    private Sensor mLinearAccelertion;
    static Context mContext;

然后OnCreate服务功能

public void onCreate(){ 

    super.onCreate(); 
    mContext = getApplicationContext();

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMagnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mGyro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); 
    mLinearAccelertion = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

    sensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(this, mGyro, SensorManager.SENSOR_DELAY_NORMAL); 
    sensorManager.registerListener(this, mLinearAccelertion, SensorManager.SENSOR_DELAY_NORMAL);

} 

您必须检查哪个传感器正在创建事件:

    public void onSensorChanged(SensorEvent event) {

        float[] values = event.values;
        Sensor mSensor = event.sensor;

        if(mSensor.getType() == Sensor.TYPE_ACCELEROMETER){
            float[] accelerometerValue = values;
        } 

        if(mSensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION){           

        } 

        if(mSensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
            float[] magnetometerValue = values;
        } 

if(mSensor.getType() == Sensor.TYPE_GYROSCOPE){
           float[] gyroValue = values;
        } 


    } 

修改

我在这里有传感器代码:

My Algorithm to Calculate Position of Smartphone - GPS and Sensors