JavaNullPointer
JavaNullPointer

Reputation: 1211

Android detect turn from phone

I want to detect, if my smartphone user turn left or right or doing a turn around.

Here an example to explain "turn right":

The User faces a crossroad. Now I want to detect if my user takes the right path or the left path or goes straight on.

therefore i use this to get azimuth:

    @Override
public void onSensorChanged(SensorEvent event) {

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        for (int i = 0; i < 3; i++) {
            valuesAccelerometer[i] = event.values[i];
        }
        break;
    case Sensor.TYPE_MAGNETIC_FIELD:
        for (int i = 0; i < 3; i++) {
            valuesMagneticField[i] = event.values[i];
        }
        break;
    }
    boolean success = SensorManager.getRotationMatrix(matrixR, matrixI,
            valuesAccelerometer, valuesMagneticField);

    if (success) {
        SensorManager.getOrientation(matrixR, matrixValues);
        Math.round(Math.toDegrees(Math.acos(matrixValues[0])));
        azimuth = (float) Math.round(Math.toDegrees(matrixValues[0]));
        if (azimuth < 0.0f) {
            azimuth += 360.0f;
        }
   }

This works fine. But now i dont know how to set the start and the end point of my rotation...

Upvotes: 0

Views: 2151

Answers (1)

harikrishnan
harikrishnan

Reputation: 1945

use this program. whenever you turn left or right and up and down, you will get horizontal and vertical notification like arrow image..and you will get that coordinates.

 package com.authorwjf;

 import android.annotation.SuppressLint;
 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.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;

 @SuppressLint("NewApi")
 public class Main extends Activity implements SensorEventListener {

private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
 private Sensor mAccelerometer;
 private final float NOISE = (float) 2.0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mInitialized = false;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this,
  mAccelerometer ,  SensorManager.SENSOR_DELAY_NORMAL);
 }

protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, 
 mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
 }

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

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // can be safely ignored for this demo
}

@Override
public void onSensorChanged(SensorEvent event) {
    TextView tvX= (TextView)findViewById(R.id.x_axis);
    TextView tvY= (TextView)findViewById(R.id.y_axis);
    TextView tvZ= (TextView)findViewById(R.id.z_axis);
    ImageView iv = (ImageView)findViewById(R.id.image);
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    if (!mInitialized) {
        mLastX = x;
        mLastY = y;
        mLastZ = z;
        tvX.setText("0.0");
        tvY.setText("0.0");
        tvZ.setText("0.0");
        mInitialized = true;
    } else {
        float deltaX = Math.abs(mLastX - x);
        float deltaY = Math.abs(mLastY - y);
        float deltaZ = Math.abs(mLastZ - z);
        if (deltaX < NOISE) deltaX = (float)0.0;
        if (deltaY < NOISE) deltaY = (float)0.0;
        if (deltaZ < NOISE) deltaZ = (float)0.0;
        mLastX = x;
        mLastY = y;
        mLastZ = z;
        tvX.setText(Float.toString(deltaX));
        tvY.setText(Float.toString(deltaY));
        tvZ.setText(Float.toString(deltaZ));
        iv.setVisibility(View.VISIBLE);
        if (deltaX > deltaY) {
            iv.setImageResource(R.drawable.horizontal);
        } else if (deltaY > deltaX) {
            iv.setImageResource(R.drawable.vertical);
        } else {
            iv.setVisibility(View.INVISIBLE);
        }
    }
}
 }

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView  
    android:paddingTop="20dip"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textSize="16sp" 
    android:textStyle="bold"
    android:gravity="center"
    android:text="Shaker Demo"/>
<TableLayout
    android:paddingTop="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*">
    <TableRow>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textSize="14sp"  
            android:text="X-Axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:textSize="14sp" 
            android:text="Y-Axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textSize="14sp"  
            android:text="Z-Axis"
            android:gravity="center"/>
    </TableRow>
    <TableRow>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/x_axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/y_axis"
            android:gravity="center"/>
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/z_axis"
            android:gravity="center"/>
    </TableRow>
</TableLayout>
<ImageView
    android:paddingTop="10dip"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:id="@+id/image"
    android:layout_gravity="center"
    android:visibility="invisible"/>
</LinearLayout>

Upvotes: 2

Related Questions