Sei sulla pagina 1di 3

package com.example.

tesappcompat7;
import org.json.JSONException;
import org.json.JSONObject;
import
import
import
import
import
import
import
import
import
import
import
import

android.os.Bundle;
android.app.Activity;
android.view.Menu;
android.widget.Toast;
android.app.Activity;
android.content.Context;
android.hardware.Sensor;
android.hardware.SensorEvent;
android.hardware.SensorEventListener;
android.hardware.SensorManager;
android.os.Bundle;
android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {


//private SensorManager mgr;
//private Sensor gyro;
private SensorManager mSensorManager;
private Sensor mSensor;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE
);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
//mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
//gyro = mgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
text = (TextView) findViewById(R.id.text);
//Kirim JSON
try {
JSONObject toSend = new JSONObject();
toSend.put("msg", "testing kirim sensor");
JSONTransmitter transmitter = new JSONTransmitter();
transmitter.execute(new JSONObject[] {toSend});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_
NORMAL);
}
@Override
protected void onPause() {
super.onPause();

mSensorManager.unregisterListener(this, mSensor);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private static final double NS2S = 1.0f / 1000000000.0f;
private final float[] deltaRotationVector = new float[4];
private double timestamp;
public void onSensorChanged(SensorEvent event) {
String pesan = "0: " + event.values[0] + "\n" +
"1: " + event.values[1] + "\n" +
"2: " + event.values[2] + "\n";
text.setText(pesan);
//text.invalidate();
if (timestamp != 0) {
final double dT = (event.timestamp - timestamp) * NS2S;
// Axis of the rotation sample, not normalized yet.
double axisX = event.values[0];
double axisY = event.values[1];
double axisZ = event.values[2];
// Calculate the angular speed of the sample
double omegaMagnitude = Math.sqrt(axisX*axisX + axisY*axisY
+ axisZ*axisZ);
// Normalize the rotation vector if it's big enough to get t
he axis
// (that is, EPSILON should represent your maximum allowable
margin of error)
if (omegaMagnitude > 0.000000001f) {
axisX /= omegaMagnitude;
axisY /= omegaMagnitude;
axisZ /= omegaMagnitude;
}
// Integrate around this axis with the angular speed by the
timestep
// in order to get a delta rotation from this sample over th
e timestep
// We will convert this axis-angle representation of the del
ta rotation
// into a quaternion before turning it into the rotation mat
rix.
double thetaOverTwo = omegaMagnitude * dT / 2.0f;
double sinThetaOverTwo = Math.sin(thetaOverTwo);
double cosThetaOverTwo = Math.cos(thetaOverTwo);
deltaRotationVector[0] = (float) (sinThetaOverTwo * axisX);
deltaRotationVector[1] = (float) (sinThetaOverTwo * axisY);
deltaRotationVector[2] = (float) (sinThetaOverTwo * axisZ);
deltaRotationVector[3] = (float) cosThetaOverTwo;
}
timestamp = event.timestamp;
float[] deltaRotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRo
tationVector);
}
@Override

public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Potrebbero piacerti anche