1

I am making an app that records the readings from the accelerometer. When I enter TYPE_ACCELEROMETER it can perfectly record the raw accelerometer data. However when I enter TYPE_LINEAR_ACCELERATION it fails to return any values. The app doesn't give me an error or quit. I think that I must be either using a device that doesn't support TYPE_LINEAR_ACCELERATION or I do not have all the necessary permissions in my manifest.

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

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

        currentX = (TextView) findViewById(R.id.currentX);
        currentY = (TextView) findViewById(R.id.currentY);
        currentZ = (TextView) findViewById(R.id.currentZ);


    }

public void onSensorChanged(SensorEvent event) {

            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];


            currentX.setText(Float.toString(x));
            currentY.setText(Float.toString(y));
            currentZ.setText(Float.toString(z));

            try {
                writer.write(x + "," + y + "," + z + "\n");
            } catch (IOException e) {
                Log.e(LOG_TAG2, "failed");
            }
        }
    };
public void Recordbutton(View view) {

        if (recordbuttonstatus) {
            playBtn.setEnabled(true);
            recordbuttonstatus = false;
            startBtn.setText(getString(R.string.stoprecording));
            sensorManager.registerListener(accelListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(accelListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);

            Thread accelerometerrecord = new Thread(new Runnable() {

                @Override
                        public void run(){
                File accoutputFile = new File(Environment.getExternalStorageDirectory(), "accelerometervals.txt");
                try {
                    accoutputFile.createNewFile();
                } catch (IOException e){
                    e.printStackTrace();
                }
                    try {
                        writer = new FileWriter(accoutputFile, false); /*When false, the file is overwritten, when true, the file is appended.*/
                    } catch (IOException e) {
                        Log.e(LOG_TAG2, "failed");
                    }
                }
            });

            accelerometerrecord.start();

} else {
            startBtn.setText(getString(R.string.newrecording));
            recordbuttonstatus = true;
            sensorManager.unregisterListener(accelListener);

        }
    }

 protected void onPause() {
        super.onPause();

        sensorManager.unregisterListener(accelListener);

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                Log.e(LOG_TAG2, "failed");
            }
        }
    }

public void onStop() {
        super.onStop();
        sensorManager.unregisterListener(accelListener);

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                Log.e(LOG_TAG2, "failed");
            }
        }
    }
  • should maybe confirm what is available by looking at the availability of the features using SensorManager apis – JoxTraex Nov 22 '16 at 03:11
  • You can check sensor's availability to your device by using this link: http://stackoverflow.com/a/26054086/3819836 – Hassan Jamil Nov 22 '16 at 06:27

1 Answers1

1

Thanks everyone. It turned out that my phone didn't have the linear accelerometer sensor.