Rishabh Jindal
Rishabh Jindal

Reputation: 75

Type_linear_acceleration sensor not working but TYPE_ACCELEROMETER is working

When i use the Sensor.TYPE_ACCELEROMETER its working fine and giving me acceleration values but when i use type_linear_acceleration it is not even found in the list (See the code).

Here is my MainActivity.java

public class MainActivity extends AppCompatActivity {

SensorManager sm = null;
TextView textView1 = null;
List list;

SensorEventListener sel = new SensorEventListener(){
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    public void onSensorChanged(SensorEvent event) {

        Log.i("tag","inside sensor listener");
        textView1.setText("x:"+event.values[0]+"\ny:"+event.values[1]+"\nz:"+event.values[2]);

    }

};

@Override
protected void onResume() {
    super.onResume();
    sm = (SensorManager)getSystemService(SENSOR_SERVICE);

    list = sm.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION);


    //Log.i("tag",Sensor.TYPE_ACCELEROMETER);
    if(list.size()>0){

    sm.registerListener(sel,sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION) , SensorManager.SENSOR_DELAY_NORMAL);

        Toast.makeText(getBaseContext(), "Accelerometer.", Toast.LENGTH_LONG).show();
    }else{
    Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG).show();
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView1 = (TextView)findViewById(R.id.textView2);

}

@Override
protected void onStop() {
    if(list.size()>0){
        sm.unregisterListener(sel);
    }
    super.onStop();
}

}

The problem is its showing "Error: No Accelerometer". What could be the possible reasons for that? P.s- i don't have gyroscope on the device.

Upvotes: 0

Views: 1133

Answers (1)

Ivan
Ivan

Reputation: 21

Gravity sensor, a linear acceleration sensor, and a rotation vector sensor were updated in Android 4.0 and now use a device's gyroscope. https://developer.android.com/guide/topics/sensors/sensors_motion.html

Upvotes: 2

Related Questions