Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample for Reading Sensor Value on Android
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
public class SensorActivity extends AppCompatActivity{
private static final String TAG = SensorActivity.class.getSimpleName();
private SensorManager mSensorManager;
private Sensor mPressure;
private Sensor mTemperature;
private TextView mTextViewPressure;
private TextView mTextViewTemperature;
private SensorEventListener mPressureListener;
private SensorEventListener mTemperatureListener;
private Sensor mHumidity;
private SensorEventListener mHumidityListener;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
mTemperature = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
mHumidity = mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);
mTextViewPressure = (TextView) findViewById(R.id.text_view_pressure);
mTextViewTemperature = (TextView) findViewById(R.id.text_view_temperature);
mHumidityListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float humidity = event.values[0];
// Do something with this sensor data.
Log.d(TAG, "Humidity: " + String.valueOf(humidity) + "%");
// mTextViewTemperature.setText(String.valueOf(temperature_value));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mTemperatureListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float temperature_value = event.values[0];
// Do something with this sensor data.
Log.d(TAG, "Temperature: " + String.valueOf(temperature_value));
mTextViewTemperature.setText(String.valueOf(temperature_value));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mPressureListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float millibars_of_pressure = event.values[0];
// Do something with this sensor data.
Log.d(TAG, "Pressure: " + String.valueOf(millibars_of_pressure));
mTextViewPressure.setText(String.valueOf(millibars_of_pressure));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(mTemperatureListener, mTemperature, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(mHumidityListener, mHumidity, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(mPressureListener, mPressure, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(mPressureListener);
mSensorManager.unregisterListener(mHumidityListener);
mSensorManager.unregisterListener(mTemperatureListener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.