简体   繁体   中英

pressure sensors in smartphones

Could anyone help me understand the pressure sensors in smart phones.I am guessing the TYPE_PRESSURE is used to query the atmospheric pressure. It is not clear where the values are stored. Is it stored in the SensorManager.values field? What could be the other possible uses of this sensor?

The pressure sensor gives the ambient air pressure in hPa or mbar. Its values are stored in the event.values[0]. Check the reference about environment sensors in de Android Dev Centre.

Here you have an example of how to use it:

 public class SensorActivity extends Activity implements SensorEventListener {
 private SensorManager mSensorManager;
 private Sensor mPressure;

 @Override
 public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // 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);
  }

  @Override
  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
  }

  @Override
  public final void onSensorChanged(SensorEvent event) {
    float millibars_of_pressure = event.values[0];
    // Do something with this sensor data.
  }

  @Override
  protected void onResume() {
    // Register a listener for the sensor.
    super.onResume();
    mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    // Be sure to unregister the sensor when the activity pauses.
    super.onPause();
    mSensorManager.unregisterListener(this);
  }
}

Its main uses would be to tell elevation changes, however i am sure that there are some intelligent ways to use this sensor to do multiple other tasks.

As for getting the variables out I am pretty sure it works as the other android sensors do. You register a sensor event listener and then in your on sensor changed you get the values out of event.values.clone() eg.

switch (event.sensor.getType())
        {
        case Sensor.TYPE_PRESSURE:
            m_fPressureVal = event.values.clone();
            break;
                     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM