HomeGuidesReferenceLearn

Reference version

Expo Barometer iconExpo Barometer

GitHub

npm

A library that provides access to device's barometer sensor.


Barometer from expo-sensors provides access to the device barometer sensor to respond to changes in air pressure. pressure is measured in hectopascals or hPa.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb

Installation

Terminal
npx expo install expo-sensors

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

Usage

Basic Barometer usage
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, TouchableOpacity, View, Platform } from 'react-native';
import { Barometer } from 'expo-sensors';

export default function App() {
  const [data, setData] = useState({});

  useEffect(() => {
    _toggle();
  }, []);

  useEffect(() => {
    return () => {
      _unsubscribe();
    };
  }, []);

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

  const _subscribe = () => {
    this._subscription = Barometer.addListener(barometerData => {
      setData(barometerData);
    });
  };

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

  const { pressure = 0, relativeAltitude = 0 } = data;

  return (
    <View style={styles.sensor}>
      <Text>Barometer:</Text>
      <Text>Pressure: {pressure * 100} Pa</Text>
      <Text>
        Relative Altitude:{' '}
        {Platform.OS === 'ios' ? `${relativeAltitude} m` : `Only available on iOS`}
      </Text>
      <View style={styles.buttonContainer}>
        <TouchableOpacity onPress={_toggle} style={styles.button}>
          <Text>Toggle</Text>
        </TouchableOpacity>
      </View>
    </View>
  );
}

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

API

import { Barometer } from 'expo-sensors';

Methods

Barometer.isAvailableAsync()

You should always check the sensor availability before attempting to use it.

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

Returns

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

Barometer.addListener((data: BarometerMeasurement) => void)

Subscribe for updates to the barometer.

const subscription = Barometer.addListener(({ pressure, relativeAltitude }) => {
  console.log({ pressure, relativeAltitude });
});

Arguments

  • 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.

Returns

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

Barometer.removeAllListeners()

Removes all listeners.

Types

BarometerMeasurement

The altitude data returned from the native sensors.

type BarometerMeasurement = {
  pressure: number;
  /* iOS Only */
  relativeAltitude?: number;
};
NameTypeFormatiOSAndroidWeb
pressurenumberhPa
relativeAltitudenumber | undefinedmeters

Units and providers

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.