VIM4 SBC内置G-Sensor模块,进入设置-->Khadas设置-->重力感应器数据
可以查看X,Y,Z三轴加速度大小。
APP获取G-Sensor数据
APP获取G-Sensor数据有如下几个步骤:
获取Sensor服务 : getSystemService
。
获取G-Sensor对象 : getDefaultSensor
。
注册数据监听器 : registerListener
。
代码参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import android.app.Activity; import android.content.Context import android.hardware.Sensor; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.hardware.SensorEvent; import android.os.Bundle; import android.widget.TextView;
public class Gsensor_Preference extends Activity implements SensorEventListener {
private SensorManager mSensorManager; private TextView mTextView_x; private TextView mTextView_y; private TextView mTextView_z;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gsensor);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_NORMAL);
mTextView_x = findViewById(R.id.textView_x); mTextView_y = findViewById(R.id.textView_y); mTextView_z = findViewById(R.id.textView_z); }
@Override protected void onDestroy() { super.onDestroy(); mSensorManager.unregisterListener(this); }
@Override public void onSensorChanged(SensorEvent event) { if(event.sensor == null) return ; if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ mTextView_x.setText("X: " + event.values[0] + " m/s²"); mTextView_y.setText("Y: " + event.values[1] + " m/s²"); mTextView_z.setText("Z: " + event.values[2] + " m/s²"); } }
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
} }
|