|
| 1 | +package com.codingblocks.sensors; |
| 2 | + |
| 3 | +import android.hardware.Sensor; |
| 4 | +import android.hardware.SensorEvent; |
| 5 | +import android.hardware.SensorEventListener; |
| 6 | +import android.hardware.SensorManager; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.support.v7.app.AppCompatActivity; |
| 9 | +import android.util.Log; |
| 10 | + |
| 11 | +public class MainActivity extends AppCompatActivity { |
| 12 | + |
| 13 | + public static final String TAG = "MainActivity"; |
| 14 | + @Override |
| 15 | + protected void onCreate(Bundle savedInstanceState) { |
| 16 | + super.onCreate(savedInstanceState); |
| 17 | + setContentView(R.layout.activity_main); |
| 18 | + |
| 19 | + SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); |
| 20 | + |
| 21 | + Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 22 | + |
| 23 | +// List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL); |
| 24 | + |
| 25 | +// for (int i =0; i <sensorList.size(); i++){ |
| 26 | +// |
| 27 | +// Sensor currentSensor = sensorList.get(i); |
| 28 | +// |
| 29 | +// } |
| 30 | + |
| 31 | + |
| 32 | +// for (Sensor s : sensorList){ |
| 33 | +// |
| 34 | +// Log.d(TAG, "onCreate: NAME " + s.getName()); |
| 35 | +// Log.d(TAG, "onCreate: VERSION " + s.getVersion()); |
| 36 | +// Log.d(TAG, "onCreate: RANGE " + s.getMaximumRange()); |
| 37 | +// Log.d(TAG, "onCreate: TYPE " + s.getType()); |
| 38 | +// Log.d(TAG, "onCreate: POWER " + s.getPower()); |
| 39 | +// Log.d(TAG, "onCreate: VENDOR " + s.getVendor()); |
| 40 | +// Log.d(TAG, "--------------------------------"); |
| 41 | +// |
| 42 | +// } |
| 43 | + |
| 44 | + |
| 45 | + sensorManager.registerListener(new SensorEventListener() { |
| 46 | + @Override |
| 47 | + public void onSensorChanged(SensorEvent sensorEvent) { |
| 48 | + Log.d(TAG, "onSensorChanged: accel in x " + sensorEvent.values[0]); |
| 49 | + Log.d(TAG, "onSensorChanged: accel in y " + sensorEvent.values[1]); |
| 50 | + Log.d(TAG, "onSensorChanged: accel in z " + sensorEvent.values[2]); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onAccuracyChanged(Sensor sensor, int i) { |
| 55 | + |
| 56 | + } |
| 57 | + },accel,1000000); |
| 58 | + |
| 59 | +// |
| 60 | +// Log.d(TAG, "onCreate: NAME" + accel.getName()); |
| 61 | +// Log.d(TAG, "onCreate: VERSION" + accel.getVersion()); |
| 62 | +// Log.d(TAG, "onCreate: RANGE" + accel.getMaximumRange()); |
| 63 | +// Log.d(TAG, "onCreate: TYPE" + accel.getType()); |
| 64 | +// Log.d(TAG, "onCreate: POWER" + accel.getPower()); |
| 65 | +// Log.d(TAG, "onCreate: VENDOR" + accel.getVendor()); |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + } |
| 71 | +} |
0 commit comments