Don Doom
Don Doom

Reputation: 19

Android Sensor Data

I want to collecting some data from my accelerometer sensor in my android smartphone. I have try some ways, the best way is using service class and call it in activity class, but this method just give me three axes data of that time. My intention is to collect data from second to second, i mean everytime the data changed, it will be recorded in log file, so i can get some data appended second from second.

Anyone can help me?, im so glad for the help. Thank you very much for the advance.

i have already implement it here is my activity code

package trying.gapickerwithservice;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.renderscript.Type;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class GAPickerWithService extends Activity implements SensorEventListener
{
    Sensor mySens;
    String giro;
    EditText VALUE;
    SensorManager sensorManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gapickerwserv);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor mySens = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        sensorManager.registerListener(this, mySens, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.gapicker_with, menu);
        return true;    
    }   
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }   
    @Override
    public void onSensorChanged(SensorEvent event) {
        VALUE = (EditText) findViewById(R.id.value);
        float Roll = event.values[2];
        float Pitch = event.values[1];
        float Yaw = event.values[0];
        giro = Float.toString(Pitch)+Float.toString(Roll)+Float.toString(Yaw);
        VALUE.setText(giro);
        // TODO Auto-generated method stub  
    }       
    protected void onResume(){
        super.onResume();
        sensorManager.registerListener(this, mySens, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause(){
        super.onPause();
        sensorManager.unregisterListener(this);
    }
    }

and this is my service code

package trying.gapickerwithservice;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.EditText;

public class GAPickerGyroService extends Service implements SensorEventListener {
Sensor mySens;
static final String TAG = "GAPickerGyroService";
private SensorManager mySensMan;
String giro;
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
public void onCreate ()
{
    mySensMan = (SensorManager) getSystemService(SENSOR_SERVICE);
    mySens = mySensMan.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mySensMan.registerListener(this, mySens, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub      
}
@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub

    float Roll = event.values[2];
    float Pitch = event.values[1];
    float Yaw = event.values[0];
    giro = Float.toString(Pitch)+Float.toString(Roll)+Float.toString(Yaw);
    Log.d(TAG, giro);
}       
}

Upvotes: 1

Views: 1383

Answers (2)

Ridcully
Ridcully

Reputation: 23655

You can use the timestamp of the SensorEvent to keep only one data-set per second. However, it might be better to e.g. collect all data you get per second and calculate an average over those values and store the average result as the data-set for that second.

Where you actually store the data, would depend on what you want to do with it later. You can write it to a file or to a database.

EDIT

actually i want to record the sensor data (x,y, and z value, everytime the value changed it will appended become a row of data)

For that, just replace your Log.d(TAG, giro) with a line appending giro to a file (there are plenty of examples out there on how to do that).

with clicking a start button

Set up a start button with an onClickListener and do the mySensMan.registerListener(this, mySens, SensorManager.SENSOR_DELAY_NORMAL) there.

and stop the record with a stop button

Set up a stop button with an onClickListener and do a mySensMan.unregisterListener(...) there.

after that i want the data displayed in the textfield as a string.

Read the text from your file (again, plenty of examples out there) and use setText() of a TextView to show it.

Beware - The sensors might generate a lot of data.

Upvotes: 2

shanwu
shanwu

Reputation: 1675

I think you need to implement listener on your service. check out the SensorEventListener, you implement this interface like View.OnClickListener, then I think you are good to go!

Your code actually works ... check my code here...

package giveup.none.sensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity implements SensorEventListener {
    SensorManager sensorManager;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.tv);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> list = sensorManager.getSensorList(Sensor.TYPE_ALL);

        for (Sensor s : list) {
            Log.d("guang", s.getName());
        }

        List<Sensor> gyroList = sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);
        Sensor gyroSensor;
        if (!gyroList.isEmpty()) {
            gyroSensor = gyroList.get(0);
        } else {
            return;
        }

        sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        tv.setText( Arrays.toString(sensorEvent.values));
        Log.d("guang","changed: "+ Arrays.toString(sensorEvent.values));
    }

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

    }
}

=============================xml=============================

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

enter image description here if you try to get what those sensorevent value mean to you, please check this article.

Upvotes: 1

Related Questions