κ°œλ°œπŸ’»/μ•ˆλ“œλ‘œμ΄λ“œ

[Android] κ°€μ†λ„μ„Όμ„œ(Acceleration Sensor) μ†ŒμŠ€

VIP 2017. 4. 26. 13:52
728x90
λ°˜μ‘ν˜•

- ν™˜κ²½

1. Android Studio 2.3.1

2. LG Watch Urbane 2

3. Android 6.0.1 / API 23


- MainActivity

package com.example.vip.hammer;

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.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView text_x, text_y, text_z, text_vector;

//Using the Accelometer & Gyroscoper
private SensorManager mSensorManager = null;

//Using the Accelometer
private SensorEventListener mAccLis;
private Sensor mAccelometerSensor = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

//Using the Gyroscope & Accelometer
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

//Using the Accelometer
mAccelometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccLis = new AccelometerListener();

// μ„Όμ„œ λ°›λŠ” 속도
mSensorManager.registerListener(mAccLis, mAccelometerSensor, SensorManager.SENSOR_DELAY_NORMAL);

stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
text_x = (TextView) stub.findViewById(R.id.textView);
text_y = (TextView) stub.findViewById(R.id.textView2);
text_z = (TextView) stub.findViewById(R.id.textView3);
text_vector = (TextView) stub.findViewById(R.id.textView4);
}
});
}

@Override
public void onPause(){
super.onPause();
Log.e("LOG", "onPause()");
mSensorManager.unregisterListener(mAccLis);
}

@Override
public void onDestroy(){
super.onDestroy();
Log.e("LOG", "onDestroy()");
mSensorManager.unregisterListener(mAccLis);
}

private class AccelometerListener implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {

double accX = event.values[0];
double accY = event.values[1];
double accZ = event.values[2];

double x = Double.parseDouble(String.format("%.3f", accX));
double y = Double.parseDouble(String.format("%.3f", accY));
double z = Double.parseDouble(String.format("%.3f", accZ));
double sum = (x*x) + (y*y) + (z*z);
double root = Math.sqrt(sum);

root = Double.parseDouble(String.format("%.3f", root));

text_x.setText("X : " + x);
text_y.setText("Y : " + y);
text_z.setText("Z : " + z);
text_vector.setText("Vector : " + root);

double angleXZ = Math.atan2(accX, accZ) * 180/Math.PI;
double angleYZ = Math.atan2(accY, accZ) * 180/Math.PI;

Log.e("LOG", "ACCELOMETER [X]:" + String.format("%.4f", event.values[0])
+ " [Y]:" + String.format("%.4f", event.values[1])
+ " [Z]:" + String.format("%.4f", event.values[2])
+ " [angleXZ]: " + String.format("%.4f", angleXZ)
+ " [angleYZ]: " + String.format("%.4f", angleYZ));

}

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

}
}
}


- round_activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.vip.hammer.MainActivity"
tools:deviceIds="wear_round">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_round"
android:layout_above="@+id/textView2"
android:layout_alignStart="@+id/textView2" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_round"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/textView3" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_round"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_round"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/textView3" />
</RelativeLayout>


728x90
λ°˜μ‘ν˜•