Expo

Documentation

Barometer

Access the device barometer sensor to respond to changes in air pressure. pressure is measured in hectopascals or hPa.

Warning: This API is not yet available in the managed apps.
To use it in a bare React Native app, follow its installation instructions.

// in managed apps:
import { Barometer } from 'expo';

// in bare apps:
import { Barometer } from 'expo-sensors';
OSUnitsProviderDescription
iOShPaCMAltimeterAltitude events reflect the change in the current altitude, not the absolute altitude.
AndroidhPaSensor.TYPE_PRESSUREMonitoring air pressure changes.
WebN/AN/AThis sensor is not available on the web and cannot be accessed. An UnavailabilityError will be thrown if you attempt to get data.

Returns a promise which resolves into a boolean denoting the availability of the device barometer.
OSAvailability
iOSiOS 8+
AndroidAndroid 2.3+ (API Level 9+)
WebN/A

  • A promise that resolves to a boolean denoting the availability of the sensor.

Subscribe for updates to the barometer.
const subscription = Barometer.addListener(({ pressure, relativeAltitude }) => {
  console.log({ pressure, relativeAltitude });
});

  • listener (function) -- A callback that is invoked when an barometer update is available. When invoked, the listener is provided a single argument that is an object containing: pressure: number (hPa). On iOS the relativeAltitude: number (meters) value will also be available.

  • A subscription that you can call remove() on when you would like to unsubscribe the listener.

Remove all listeners.

The altitude data returned from the native sensors.
type BarometerMeasurement = {
  pressure: number,
  /* iOS Only */
  relativeAltitude?: number,
};
NameTypeFormatiOSAndroidWeb
pressurenumberhPa
relativeAltitudenumber | undefinedmeters

import React from 'react';
import { Barometer } from 'expo-sensors';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';

export default class BarometerSensor extends React.Component {
  state = {
    data: {},
  };

  componentDidMount() {
    this._toggle();
  }

  componentWillUnmount() {
    this._unsubscribe();
  }

  _toggle = () => {
    if (this._subscription) {
      this._unsubscribe();
    } else {
      this._subscribe();
    }
  };

  _subscribe = () => {
    this._subscription = Barometer.addListener(data => {
      this.setState({ data });
    });
  };

  _unsubscribe = () => {
    this._subscription && this._subscription.remove();
    this._subscription = null;
  };

  render() {
    const { pressure = 0 } = this.state.data;

    return (
      <View style={styles.sensor}>
        <Text>Barometer:</Text>
        <Text>{pressure * 100} Pa</Text>

        <View style={styles.buttonContainer}>
          <TouchableOpacity onPress={this._toggle} style={styles.button}>
            <Text>Toggle</Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  buttonContainer: {
    flexDirection: 'row',
    alignItems: 'stretch',
    marginTop: 15,
  },
  button: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#eee',
    padding: 10,
  },
  sensor: {
    marginTop: 15,
    paddingHorizontal: 10,
  },
});