• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

indoor navigation using smartphone with inbulit sensors

reshma99

Lurker
Feb 2, 2020
1
0
Hi,
This is the code for saving readings of accelerometer and gyroscope sensor readings from smartphone .
I need to add the sampling period between these readings for position estimation.
Please help with this code.

package com.example.sensorsave2;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@RequiresApi(api = Build.VERSION_CODES.P)
public class MainActivity extends AppCompatActivity implements SensorEventListener2 {

SensorManager manager;
Button buttonStart;
Button buttonStop;
boolean isRunning;
final String TAG = "SensorLog";
FileWriter writer;


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

isRunning = false;

manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

buttonStart = (Button)findViewById(R.id.buttonStart);
buttonStop = (Button)findViewById(R.id.buttonStop);

buttonStart.setOnTouchListener(new View.OnTouchListener() {
@override
public boolean onTouch(View view, MotionEvent motionEvent) {
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);

Log.d(TAG, "Writing to " + getStorageDir());
try {
writer = new FileWriter(new File(getStorageDir(), "sensors_" + System.currentTimeMillis() + ".csv"));
} catch (IOException e) {
e.printStackTrace();
}

manager.registerListener(MainActivity.this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
manager.registerListener(MainActivity.this, manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE));
manager.registerListener(MainActivity.this, manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE_UNCALIBRATED));

isRunning = true;
return true;
}
});
buttonStop.setOnTouchListener(new View.OnTouchListener() {
@RequiresApi(api = Build.VERSION_CODES.P)
@override
public boolean onTouch(View view, MotionEvent motionEvent) {
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
isRunning = false;
manager.flush(MainActivity.this);
manager.unregisterListener(MainActivity.this);
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
});
}

private String getStorageDir() {
return this.getExternalFilesDir(null).getAbsolutePath();
// return "/storage/emulated/0/Android/data/com.iam360.sensorlog/";
}

@override
public void onFlushCompleted(Sensor sensor) {

}

@override
public void onSensorChanged(SensorEvent evt) {
if(isRunning) {
try {
switch(evt.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
writer.write(String.format("%d; ACC; %f; %f; %f; %f; %f; %f\n", evt.timestamp, evt.values[0], evt.values[1], evt.values[2], 0.f, 0.f, 0.f));
break;
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
writer.write(String.format("%d; GYRO_UN; %f; %f; %f; %f; %f; %f\n", evt.timestamp, evt.values[0], evt.values[1], evt.values[2], evt.values[3], evt.values[4], evt.values[5]));
break;
case Sensor.TYPE_GYROSCOPE:
writer.write(String.format("%d; GYRO; %f; %f; %f; %f; %f; %f\n", evt.timestamp, evt.values[0], evt.values[1], evt.values[2], 0.f, 0.f, 0.f));
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@override
public void onAccuracyChanged(Sensor sensor, int i) {

}
}
 
You are much more likely to get "help with the code" if you say specifically what help you need. What have you tried? What is working and what is not working? Secondly, please read the sticky topic "***PLEASE READ ME BEFORE POSTING***" for advice on how to post code formatted so people can read it easily.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones