下面列出了android.hardware.Sensor#TYPE_PRESSURE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case (Sensor.TYPE_AMBIENT_TEMPERATURE):
mLastTemperature = event.values[0];
break;
case (Sensor.TYPE_RELATIVE_HUMIDITY):
mLastHumidity = event.values[0];
break;
case (Sensor.TYPE_PRESSURE):
mLastPressure = event.values[0];
break;
case (Sensor.TYPE_LIGHT):
mLastLight = event.values[0];
break;
default: break;
}
}
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
mGravity = event.values.clone();
//onAccelerometerChanged(values[0],values[1],values[2]);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mMagnetic = event.values.clone();
break;
case Sensor.TYPE_PRESSURE:
pressure = event.values[0];
pressure = pressure*100;
default:
return;
}
if(mGravity != null && mMagnetic != null)
{
getDirection();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
updateSensorData(latestSampledData.accelerate, event);
} else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
updateSensorData(latestSampledData.gyroscope, event);
} else if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
updateSensorData(latestSampledData.gravity, event);
} else if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
latestSampledData.light = event.values[0];
} else if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
latestSampledData.pressure = event.values[0];
} else if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
latestSampledData.temperature = event.values[0];
} else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
updateSensorData(latestSampledData.magnetic, event);
} else if (event.sensor.getType() == Sensor.TYPE_GAME_ROTATION_VECTOR) {
updateSensorData(latestSampledData.game_rotation_vector, event);
}
}
public void sensorChanged(int sensorType, float[] values) {
if(sensorType == Sensor.TYPE_PRESSURE) {
float pressure_value = values[0];
double altitude = _sensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_value);
_advancedLocation.onAltitudeChanged(altitude);
try {
_callback.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
final float value = event.values[0];
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
updateTemperatureDisplay(value);
} else if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
updateBarometerDisplay(value);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
if (event.accuracy == SensorManager.SENSOR_STATUS_NO_CONTACT || event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
setValue(Gauge.TYPE_ELEVATION, Float.NaN);
} else {
// https://en.wikipedia.org/wiki/Pressure_altitude (converted to meters)
float elevation = (float) ((1 - Math.pow(event.values[0] / SensorManager.PRESSURE_STANDARD_ATMOSPHERE, 0.190284)) * 145366.45 / 3.281);
setValue(Gauge.TYPE_ELEVATION, elevation);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_PRESSURE) {
if (accuracy == SensorManager.SENSOR_STATUS_NO_CONTACT || accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
setValue(Gauge.TYPE_ELEVATION, Float.NaN);
}
}
public static float getMinimumValueForSensor(Sensor sensor) {
float minimumValue;
switch (sensor.getType()) {
case Sensor.TYPE_HEART_RATE:
case Sensor.TYPE_LIGHT:
case Sensor.TYPE_PROXIMITY:
case Sensor.TYPE_STEP_COUNTER:
case Sensor.TYPE_PRESSURE:
minimumValue = 0;
break;
default:
minimumValue = -sensor.getMaximumRange();
}
return minimumValue;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
recentPressureReading = event.values[0];
updateVisibleReading();
} else if (event.sensor.getType() == TYPE_AMBIENT_TEMPERATURE) {
recentTemperatureReading = event.values[0];
updateVisibleReading();
} else if (event.sensor.getType() == TYPE_RELATIVE_HUMIDITY) {
recentHumidityReading = event.values[0];
updateVisibleReading();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor.getType() == Sensor.TYPE_PRESSURE) {
recentPressureAccuracy = accuracy;
log("cbservice accuracy changed, new barometer accuracy " + recentPressureAccuracy);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
if(event.values.length > 0) {
if(event.values[0] >= 0) {
log("cbservice sensor; new pressure reading " + event.values[0]);
recentPressureReading = event.values[0];
} else {
log("cbservice sensor; pressure reading is 0 or negative" + event.values[0]);
}
} else {
log("cbservice sensor; no event values");
}
}
if(stopSoonCalls<=1) {
stopSoon();
}
/*
batchReadingCount++;
if(batchReadingCount>2) {
log("batch readings " + batchReadingCount + ", stopping");
stopCollectingData();
} else {
log("batch readings " + batchReadingCount + ", not stopping");
}
*/
}
@SuppressWarnings("deprecation")
private boolean isRestricted(XParam param, int type) throws Throwable {
if (type == Sensor.TYPE_ALL)
return false;
else if (type == Sensor.TYPE_ACCELEROMETER || type == Sensor.TYPE_LINEAR_ACCELERATION) {
if (isRestricted(param, "acceleration"))
return true;
} else if (type == Sensor.TYPE_GRAVITY) {
if (isRestricted(param, "gravity"))
return true;
} else if (type == Sensor.TYPE_RELATIVE_HUMIDITY) {
if (isRestricted(param, "humidity"))
return true;
} else if (type == Sensor.TYPE_LIGHT) {
if (isRestricted(param, "light"))
return true;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD || type == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) {
if (isRestricted(param, "magnetic"))
return true;
} else if (type == Sensor.TYPE_SIGNIFICANT_MOTION) {
if (isRestricted(param, "motion"))
return true;
} else if (type == Sensor.TYPE_ORIENTATION || type == Sensor.TYPE_GYROSCOPE
|| type == Sensor.TYPE_GYROSCOPE_UNCALIBRATED) {
if (isRestricted(param, "orientation"))
return true;
} else if (type == Sensor.TYPE_PRESSURE) {
if (isRestricted(param, "pressure"))
return true;
} else if (type == Sensor.TYPE_PROXIMITY) {
if (isRestricted(param, "proximity"))
return true;
} else if (type == Sensor.TYPE_GAME_ROTATION_VECTOR || type == Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR
|| type == Sensor.TYPE_ROTATION_VECTOR) {
if (isRestricted(param, "rotation"))
return true;
} else if (type == Sensor.TYPE_TEMPERATURE || type == Sensor.TYPE_AMBIENT_TEMPERATURE) {
if (isRestricted(param, "temperature"))
return true;
} else if (type == Sensor.TYPE_STEP_COUNTER || type == Sensor.TYPE_STEP_DETECTOR) {
if (isRestricted(param, "step"))
return true;
} else if (type == Sensor.TYPE_HEART_RATE) {
if (isRestricted(param, "heartrate"))
return true;
} else if (type == 22) {
// 22 = TYPE_TILT_DETECTOR
// Do nothing
} else if (type == 23 || type == 24 || type == 25) {
// 23 = TYPE_WAKE_GESTURE
// 24 = TYPE_GLANCE_GESTURE
// 25 = TYPE_PICK_UP_GESTURE
// 23/24 This sensor is expected to only be used by the system ui
// 25 Expected to be used internally for always on display
} else
Util.log(this, Log.WARN, "Unknown sensor type=" + type);
return false;
}
public PBarometer(AppRunner appRunner) {
super(appRunner);
type = Sensor.TYPE_PRESSURE;
}
public static String[] getLabelsForSensor(Context context, Sensor sensor) {
String[] labels;
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
labels = context.getResources().getStringArray(R.array.accelerometer_values);
break;
case Sensor.TYPE_AMBIENT_TEMPERATURE:
labels = context.getResources().getStringArray(R.array.ambient_temperature_values);
break;
case Sensor.TYPE_GAME_ROTATION_VECTOR:
labels = context.getResources().getStringArray(R.array.game_rotation_vector_values);
break;
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
labels = context.getResources().getStringArray(R.array.rotation_vector_values);
break;
case Sensor.TYPE_GRAVITY:
labels = context.getResources().getStringArray(R.array.gravity_values);
break;
case Sensor.TYPE_GYROSCOPE:
labels = context.getResources().getStringArray(R.array.gyroscore_values);
break;
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
labels = context.getResources().getStringArray(R.array.gyroscore_uncalibrated_values);
break;
case Sensor.TYPE_HEART_RATE:
labels = context.getResources().getStringArray(R.array.heart_rate_values);
break;
case Sensor.TYPE_LIGHT:
labels = context.getResources().getStringArray(R.array.light_values);
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
labels = context.getResources().getStringArray(R.array.linear_acceleration_values);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
labels = context.getResources().getStringArray(R.array.magnetic_values);
break;
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
labels = context.getResources().getStringArray(R.array.magnetic_field_uncalibrated_values);
break;
case Sensor.TYPE_PRESSURE:
labels = context.getResources().getStringArray(R.array.pressure_values);
break;
case Sensor.TYPE_PROXIMITY:
labels = context.getResources().getStringArray(R.array.proximity_values);
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
labels = context.getResources().getStringArray(R.array.relative_humidity_values);
break;
case Sensor.TYPE_ROTATION_VECTOR:
labels = context.getResources().getStringArray(R.array.rotation_vector_values);
break;
case Sensor.TYPE_STEP_COUNTER:
labels = context.getResources().getStringArray(R.array.step_counter_values);
break;
default:
labels = new String[]{};
}
return labels;
}
@Nullable
public static String getHumanStringType(Sensor sensor) {
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
return "Accelerometer";
case Sensor.TYPE_AMBIENT_TEMPERATURE:
return "Ambient Temperature";
case Sensor.TYPE_GAME_ROTATION_VECTOR:
return "Game Rotation Vector";
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
return "Geomagnetic Rotation Vector";
case Sensor.TYPE_GRAVITY:
return "Gravity";
case Sensor.TYPE_GYROSCOPE:
return "Gyroscope";
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
return "Gyroscope (Uncalibrated)";
case Sensor.TYPE_HEART_RATE:
return "Heart Rate";
case Sensor.TYPE_LIGHT:
return "Light";
case Sensor.TYPE_LINEAR_ACCELERATION:
return "Linear Acceleration";
case Sensor.TYPE_MAGNETIC_FIELD:
return "Magnetic Field";
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
return "Magnetic Field (Uncalibrated)";
case Sensor.TYPE_PRESSURE:
return "Pressure";
case Sensor.TYPE_PROXIMITY:
return "Proximity";
case Sensor.TYPE_RELATIVE_HUMIDITY:
return "Relative Humidity";
case Sensor.TYPE_ROTATION_VECTOR:
return "Rotation Vector";
case Sensor.TYPE_SIGNIFICANT_MOTION:
return "Significant Motion";
case Sensor.TYPE_STEP_COUNTER:
return "Step Counter";
case Sensor.TYPE_STEP_DETECTOR:
return "Step Detector";
case Sensor.TYPE_ORIENTATION:
return "Orientation";
case Sensor.TYPE_TEMPERATURE:
return "Temperature";
}
return null;
}
public static String getDescription(Sensor sensor) {
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
return "Measures the acceleration force in m/s² that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.";
case Sensor.TYPE_AMBIENT_TEMPERATURE:
return "Measures the ambient room temperature in degrees Celsius (°C).";
case Sensor.TYPE_GRAVITY:
return "Measures the force of gravity in m/s² that is applied to a device on all three physical axes (x, y, z).";
case Sensor.TYPE_GYROSCOPE:
return "Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).";
case Sensor.TYPE_HEART_RATE:
return "Measures heart rate.";
case Sensor.TYPE_LIGHT:
return "Measures the ambient light level (illumination) in lx.";
case Sensor.TYPE_LINEAR_ACCELERATION:
return "Measures the acceleration force in m/s² that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.";
case Sensor.TYPE_MAGNETIC_FIELD:
return "Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μT.";
case Sensor.TYPE_PRESSURE:
return "Measures the ambient air pressure in hPa or mbar.";
case Sensor.TYPE_PROXIMITY:
return "Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear.";
case Sensor.TYPE_RELATIVE_HUMIDITY:
return "Measures the relative ambient humidity in percent (%).";
case Sensor.TYPE_ROTATION_VECTOR:
return "Measures the orientation of a device by providing the three elements of the device's rotation vector.";
case Sensor.TYPE_ORIENTATION:
return "Measures degrees of rotation that a device makes around all three physical axes (x, y, z). ";
case Sensor.TYPE_TEMPERATURE:
return "Measures the temperature of the device in degrees Celsius (°C). ";
default:
return "Information about this sensor is unavailable.";
}
}
private void populateTypeField( int type ) {
if( type == 0 || mTypeRow == null || mType == null )
return;
String typeName;
switch( type ) {
case Sensor.TYPE_ACCELEROMETER: {
typeName = "Accelerometer";
break;
}
case Sensor.TYPE_AMBIENT_TEMPERATURE: {
typeName = "Ambient Temperature";
break;
}
case Sensor.TYPE_GAME_ROTATION_VECTOR: {
typeName = "Game Rotation Vector";
break;
}
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR: {
typeName = "Geomagnetic Rotation Vector";
break;
}
case Sensor.TYPE_GRAVITY: {
typeName = "Gravity";
break;
}
case Sensor.TYPE_GYROSCOPE: {
typeName = "Gyroscope";
break;
}
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED: {
typeName = "Uncalibrated Gyroscope";
break;
}
case Sensor.TYPE_LIGHT: {
typeName = "Light";
break;
}
case Sensor.TYPE_LINEAR_ACCELERATION: {
typeName = "Linear Acceleration";
break;
}
case Sensor.TYPE_MAGNETIC_FIELD: {
typeName = "Magnetic Field";
break;
}
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: {
typeName = "Uncalibrated Magnetic Field";
break;
}
case Sensor.TYPE_PRESSURE: {
typeName = "Pressure";
break;
}
case Sensor.TYPE_PROXIMITY: {
typeName = "Proximity";
break;
}
case Sensor.TYPE_RELATIVE_HUMIDITY: {
typeName = "Relative Humidity";
break;
}
case Sensor.TYPE_ROTATION_VECTOR: {
typeName = "Rotation Vector";
break;
}
case Sensor.TYPE_SIGNIFICANT_MOTION: {
typeName = "Significant Motion";
break;
}
case Sensor.TYPE_STEP_COUNTER: {
typeName = "Step Counter";
break;
}
case Sensor.TYPE_STEP_DETECTOR: {
typeName = "Step Detector";
break;
}
default: {
typeName = "Other";
}
}
mType.setText( typeName );
mTypeRow.setVisibility( View.VISIBLE );
}
@SuppressWarnings({ "unchecked", "deprecation" })
public EnvironmentalSucker(Context context) {
super(context);
setSucker(this);
sm = (SensorManager)context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
availableSensors = sm.getSensorList(Sensor.TYPE_ALL);
for(Sensor s : availableSensors) {
switch(s.getType()) {
case Sensor.TYPE_AMBIENT_TEMPERATURE:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_PRESSURE:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_LIGHT:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
case Sensor.TYPE_TEMPERATURE:
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
break;
}
}
setTask(new TimerTask() {
@Override
public void run() {
if(currentAmbientTemp != null)
sendToBuffer(currentAmbientTemp);
if(currentDeviceTemp != null)
sendToBuffer(currentDeviceTemp);
if(currentHumidity != null)
sendToBuffer(currentHumidity);
if(currentPressure != null)
sendToBuffer(currentPressure);
if(currentLight != null)
sendToBuffer(currentLight);
}
});
getTimer().schedule(getTask(), 0, Environment.LOG_RATE);
}
/**
*
Sensor Sensor event data Units of measure Data description
TYPE_AMBIENT_TEMPERATURE event.values[0] °C Ambient air temperature.
TYPE_LIGHT event.values[0] lx Illuminance.
TYPE_PRESSURE event.values[0] hPa or mbar Ambient air pressure.
TYPE_RELATIVE_HUMIDITY event.values[0] % Ambient relative humidity.
TYPE_TEMPERATURE event.values[0] °C Device temperature.1
*/
@SuppressWarnings("deprecation")
@Override
public void onSensorChanged(SensorEvent event) {
synchronized(this) {
if(getIsRunning()) {
ILogPack sVals = new ILogPack();
try {
switch(event.sensor.getType()) {
case Sensor.TYPE_AMBIENT_TEMPERATURE:
sVals.put(Environment.Keys.AMBIENT_TEMP_CELSIUS, event.values[0]);
currentAmbientTemp = sVals;
break;
case Sensor.TYPE_TEMPERATURE:
sVals.put(Environment.Keys.DEVICE_TEMP_CELSIUS, event.values[0]);
currentDeviceTemp = sVals;
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
sVals.put(Environment.Keys.HUMIDITY_PERC,event.values[0]);
currentHumidity = sVals;
break;
case Sensor.TYPE_PRESSURE:
sVals.put(Environment.Keys.PRESSURE_MBAR, event.values[0]);
//TODO we need to get real local sea level pressure here from a dynamic source
//as the default value doesn't cut it
float altitudeFromPressure = SensorManager.getAltitude(mPressureSeaLevel, event.values[0]);
sVals.put(Environment.Keys.PRESSURE_ALTITUDE, altitudeFromPressure);
currentPressure = sVals;
break;
case Sensor.TYPE_LIGHT:
sVals.put(Environment.Keys.LIGHT_METER_VALUE, event.values[0]);
currentLight = sVals;
break;
}
} catch(JSONException e) {}
}
}
}
/**
* Creates a new Barometer component.
*
* @param container ignored (because this is a non-visible component)
*/
public Barometer(ComponentContainer container) {
super(container.$form(), Sensor.TYPE_PRESSURE);
}