Example usage for android.hardware SensorManager getAltitude

List of usage examples for android.hardware SensorManager getAltitude

Introduction

In this page you can find the example usage for android.hardware SensorManager getAltitude.

Prototype

public static float getAltitude(float p0, float p) 

Source Link

Document

Computes the Altitude in meters from the atmospheric pressure and the pressure at sea level.

Usage

From source file:root.gast.playground.sensor.altitude.DetermineAltitudeActivity.java

@Override
public void onSensorChanged(SensorEvent event) {
    float altitude;
    currentBarometerValue = event.values[0];

    double currentTimestamp = event.timestamp / NS_TO_MS_CONVERSION;
    double elapsedTime = currentTimestamp - lastBarometerAltitudeTimestamp;
    if (lastBarometerAltitudeTimestamp == -1 || elapsedTime > TIMEOUT) {
        altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, currentBarometerValue);
        barometerAltitudeView.setText(String.valueOf(altitude));

        if (mslp != null) {
            altitude = SensorManager.getAltitude(mslp, currentBarometerValue);
            mslpBarometerAltitudeView.setText(String.valueOf(altitude));
            mslpView.setText(String.valueOf(mslp));
        }// w w w .  j  av a 2 s  .  c o  m

        lastBarometerAltitudeTimestamp = (long) currentTimestamp;
    }
}

From source file:root.gast.playground.sensor.altitude.DetermineAltitudeActivity.java

public void onToggleClick(View view) {
    if (((ToggleButton) view).isChecked()) {
        lastGpsAltitude = currentGpsAltitude;
        lastBarometerValue = currentBarometerValue;
        gpsRelativeAltitude.setVisibility(View.INVISIBLE);
        barometerRelativeAltitude.setVisibility(View.INVISIBLE);

        if (mslp != null) {
            mslpBarometerRelativeAltitude.setVisibility(View.INVISIBLE);
        }//from  w  w  w  .  j av  a  2  s. c o  m
    } else {
        double delta;

        delta = currentGpsAltitude - lastGpsAltitude;
        gpsRelativeAltitude.setText(String.valueOf(delta));
        gpsRelativeAltitude.setVisibility(View.VISIBLE);

        delta = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, currentBarometerValue)
                - SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, lastBarometerValue);

        barometerRelativeAltitude.setText(String.valueOf(delta));
        barometerRelativeAltitude.setVisibility(View.VISIBLE);

        if (mslp != null) {
            delta = SensorManager.getAltitude(mslp, currentBarometerValue)
                    - SensorManager.getAltitude(mslp, lastBarometerValue);
            mslpBarometerRelativeAltitude.setText(String.valueOf(delta));
            mslpBarometerRelativeAltitude.setVisibility(View.VISIBLE);
        }
    }
}

From source file:tv.piratemedia.flightcontroller.BluetoothComputerFragment.java

public void setupAtimiter() {
    SensorManager mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_PRESSURE);
    SensorEventListener sensorListener = new SensorEventListener() {
        @Override// ww w.  j  a  va  2s . c  o m
        public void onSensorChanged(SensorEvent event) {
            if (currentPreasure > event.values[0] - 0.5 || currentPreasure < event.values[0] + 0.5) {
                currentPreasure = event.values[0];
                currentAltitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE,
                        currentPreasure);

                JSONObject msg = new JSONObject();
                try {
                    msg.put("action", "altitude_update");
                    msg.put("value", currentAltitude);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                sendTextMessage(msg.toString());
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
    if (sensors.size() > 0) {
        sensor = sensors.get(0);
        mSensorManager.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    } else {

    }
}