Created
November 14, 2014 00:45
-
-
Save akira-sasaki/a45c3ad2cdd0ec18438a to your computer and use it in GitHub Desktop.
MySensor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gclue.com.mysensor; | |
import android.app.Activity; | |
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.view.Menu; | |
import android.view.MenuItem; | |
import java.util.List; | |
import static android.hardware.SensorManager.SENSOR_DELAY_FASTEST; | |
public class MyActivity extends Activity implements SensorEventListener { | |
private SensorManager mSensorManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |
// Sensorの取得とリスナーへの登録 | |
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); | |
if (sensors.size() > 0) { | |
Sensor sensor = sensors.get(0); | |
mSensorManager.registerListener(this, sensor, SENSOR_DELAY_FASTEST); | |
} | |
// Sensorの取得とリスナーへの登録 | |
List<Sensor> gyroSensors = mSensorManager.getSensorList(Sensor.TYPE_GYROSCOPE); | |
if (gyroSensors.size() > 0) { | |
Sensor gyroSensor = gyroSensors.get(0); | |
mSensorManager.registerListener(this, gyroSensor, SENSOR_DELAY_FASTEST); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.my, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | |
Log.d("SURFACE", "accelX:" + event.values[0]); | |
Log.d("SURFACE", "accelY:" + event.values[1]); | |
Log.d("SURFACE", "accelZ:" + event.values[2]); | |
} else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) { | |
Log.d("SURFACE", "yaw:" + event.values[0]); | |
Log.d("SURFACE", "low:" + event.values[1]); | |
Log.d("SURFACE", "pitch:" + event.values[2]); | |
} | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment