Hello friends
The gyroscope, or gyro for short, adds an additional dimension to the information supplied by the accelerometer by tracking rotation or twist. An accelerometer measures linear acceleration of movement, while a gyro on the other hand measures the angular rotational velocity.
TextView et1;
TextView et2;
SensorManager mSensorManager;
Sensor mSensor;
et=(TextView) findViewById(R.id.textView);
et1=(TextView) findViewById(R.id.textView2);
et2=(TextView) findViewById(R.id.textView3);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
Toast.makeText(MainActivity.this,"yup sensor found", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Sorry! sensor not found", Toast.LENGTH_SHORT).show();
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float axisX = event.values[0]; //X axis value
float axisY = event.values[1]; // Y axis value
float axisZ = event.values[2]; // Z axis value
et.setText(String.valueOf(axisX));
et1.setText(String.valueOf(axisY));
et2.setText(String.valueOf(axisZ));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
}
Step:6
Now Just connect your android mobile phone with your pc(Make sure mobile phone must be in debugging mode) and run application on phone.
For any query please comment below.......!
The gyroscope, or gyro for short, adds an additional dimension to the information supplied by the accelerometer by tracking rotation or twist. An accelerometer measures linear acceleration of movement, while a gyro on the other hand measures the angular rotational velocity.
Every smart mobile phone has various sensor in built.Today we are going to learn "How to take data from mobile gyroscope".
Let's start...............
Step:1
Just open android studio and click on make new project.Now enter into programming/Development mode.
If you don't know how to do that then go to this tutorial.
Step:2
Now just drag and drop 3 tax view into virtual mobile as shown in figure below.
Step:3
Now implements SensorEventListener in MainActivity.
Step:4
Now just copy and paste following code above OnCreate method...
TextView et;TextView et1;
TextView et2;
SensorManager mSensorManager;
Sensor mSensor;
Now copy and paste following code into OnCreate method..
et=(TextView) findViewById(R.id.textView);
et1=(TextView) findViewById(R.id.textView2);
et2=(TextView) findViewById(R.id.textView3);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
Toast.makeText(MainActivity.this,"yup sensor found", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Sorry! sensor not found", Toast.LENGTH_SHORT).show();
}
Step:5
just copy and paste following code anywhere into MainActivity....
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float axisX = event.values[0]; //X axis value
float axisY = event.values[1]; // Y axis value
float axisZ = event.values[2]; // Z axis value
et.setText(String.valueOf(axisX));
et1.setText(String.valueOf(axisY));
et2.setText(String.valueOf(axisZ));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
}
Step:6
Now Just connect your android mobile phone with your pc(Make sure mobile phone must be in debugging mode) and run application on phone.
For any query please comment below.......!
Comments
Post a Comment