Android程序获取手机IMU数据

    安卓手机IMU数据包括加速度计(3轴)、陀螺仪(3轴)、磁力计(3轴)共9项数据,通过9个TextView显示在手机界面上。

    MainActivity.java代码:

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.widget.TextView;

public class MainActivity extends Activity {

private SensorManager sensorManager;
private TextView accxText;
private TextView accyText;
private TextView acczText;
private TextView gyroxText;
private TextView gyroyText;
private TextView gyrozText;
private TextView magxText;
private TextView magyText;
private TextView magzText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accxText = (TextView) findViewById(R.id.accx);
accyText = (TextView) findViewById(R.id.accy);
acczText = (TextView) findViewById(R.id.accz);
gyroxText = (TextView) findViewById(R.id.gyrox);
gyroyText = (TextView) findViewById(R.id.gyroy);
gyrozText = (TextView) findViewById(R.id.gyroz);
magxText = (TextView) findViewById(R.id.magx);
magyText = (TextView) findViewById(R.id.magy);
magzText = (TextView) findViewById(R.id.magz);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensora = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(listenera, sensora, SensorManager.SENSOR_DELAY_GAME);
Sensor sensorg = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(listenerg, sensorg, SensorManager.SENSOR_DELAY_GAME);
Sensor sensorm = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(listenerm, sensorm, SensorManager.SENSOR_DELAY_GAME);
}

private SensorEventListener listenera = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float accx = event.values[0];
float accy = event.values[1];
float accz = event.values[2];
accxText.setText("accx:" + accx);
accyText.setText("accy:" + accy);
acczText.setText("accz:" + accz);
}

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

}
};

private SensorEventListener listenerg = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float gyrox = event.values[0];
float gyroy = event.values[1];
float gyroz = event.values[2];
gyroxText.setText("gyrox:" + gyrox);
gyroyText.setText("gyroy:" + gyroy);
gyrozText.setText("gyroz:" + gyroz);
}

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

}
};

private SensorEventListener listenerm = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float magx = event.values[0];
float magy = event.values[1];
float magz = event.values[2];
magxText.setText("magx:" + magx);
magyText.setText("magy:" + magy);
magzText.setText("magz:" + magz);
}

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

}
};

@Override
protected void onDestroy() {
super.onDestroy();
if (sensorManager != null) {
sensorManager.unregisterListener(listenera);
sensorManager.unregisterListener(listenerg);
sensorManager.unregisterListener(listenerm);
}
}

}

你可能感兴趣的:(Android)