Guides
Plan-enterprise-icon
Expo Application Services
API Reference

Barometer

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

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-failed-iconStatus-failed-icon

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 } from 'react';
import { StyleSheet, Text, TouchableOpacity, View, Platform } from 'react-native';
import { Barometer } from 'expo-sensors';

export default function App() {
  const [{ pressure, relativeAltitude }, setData] = useState({ pressure: 0, relativeAltitude: 0 });
  const [subscription, setSubscription] = useState(null);

  const toggleListener = () => {
    subscription ? unsubscribe() : subscribe();
  };

  const subscribe = () => {
    setSubscription(Barometer.addListener(setData));
  };

  const unsubscribe = () => {
    subscription && subscription.remove();
    setSubscription(null);
  };

  return (
    <View style={styles.wrapper}>
      <Text>Barometer: Listener {subscription ? 'ACTIVE' : 'INACTIVE'}</Text>
      <Text>Pressure: {pressure} hPa</Text>
      <Text>
        Relative Altitude:{' '}
        {Platform.OS === 'ios' ? `${relativeAltitude} m` : `Only available on iOS`}
      </Text>
      <TouchableOpacity onPress={toggleListener} style={styles.button}>
        <Text>Toggle listener</Text>
      </TouchableOpacity>
    </View>
  );
}

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

API

import { Barometer } from 'expo-sensors';

Classes

Barometer

Type: Class extends DeviceSensor<BarometerMeasurement>

Only for:
Android-iconAndroid
Apple-iconiOS

Barometer Methods

addListener(listener)

NameTypeDescription
listenerListener<BarometerMeasurement>

A callback that is invoked when a barometer update is available. When invoked, the listener is provided with a single argument that is BarometerMeasurement.

Subscribe for updates to the barometer.

Example

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

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

getListenerCount()

Returns the registered listeners count.

  • Undo-iconnumber

getPermissionsAsync()

Checks user's permissions for accessing sensor.

hasListeners()

Returns boolean which signifies if sensor has any listeners registered.

  • Undo-iconboolean

isAvailableAsync()

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

Check the availability of the device barometer. Requires at least Android 2.3 (API Level 9) and iOS 8.

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

removeAllListeners()

Removes all registered listeners.

  • Undo-iconvoid

removeSubscription(subscription)

NameTypeDescription
subscriptionSubscription

A subscription to remove.

Removes the given subscription.

  • Undo-iconvoid

requestPermissionsAsync()

Asks the user to grant permissions for accessing sensor.

setUpdateInterval(intervalMs)

NameTypeDescription
intervalMsnumber

Desired interval in milliseconds between sensor updates.

Info-icon

Starting from Android 12 (API level 31), the system has a 200ms limit for each sensor updates.

If you need an update interval less than 200ms, you should:

  • add android.permission.HIGH_SAMPLING_RATE_SENSORS to app.json permissions field
  • or if you are using bare workflow, add <uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS"/> to AndroidManifest.xml.

Set the sensor update interval.

  • Undo-iconvoid

Interfaces

PermissionResponse

An object obtained by permissions get and request functions.

NameTypeDescription
canAskAgainbooleanIndicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission.
expiresPermissionExpirationDetermines time when the permission expires.
grantedbooleanA convenience boolean that indicates if the permission is granted.
statusPermissionStatusDetermines the status of the permission.

Types

BarometerMeasurement

The altitude data returned from the native sensors.

NameTypeDescription
pressurenumberMeasurement in hectopascals (hPa).
relativeAltitude
(optional)
numberOnly for:
Apple-iconiOS

Measurement in meters (m).

PermissionExpiration

Permission expiration time. Currently, all permissions are granted permanently.

Acceptable values are: 'never', number.

Subscription

NameTypeDescription
remove() => voidA method to unsubscribe the listener.

Enums

PermissionStatus

PermissionStatus.DENIED = "denied"

User has denied the permission.

PermissionStatus.GRANTED = "granted"

User has granted the permission.

PermissionStatus.UNDETERMINED = "undetermined"

User hasn't granted or denied the permission yet.

Units and Providers

OSUnitsProviderDescription
iOShPaCMAltimeterAltitude events reflect the change in the current altitude, not the absolute altitude.
AndroidhPaSensor.TYPE_PRESSUREMonitoring air pressure changes.
WebStatus-failed-iconStatus-failed-iconThis sensor is not available on the web and cannot be accessed. An UnavailabilityError will be thrown if you attempt to get data.
  • Message-iconAsk a question on the forums about Barometer
  • Github-iconView open bug reports for Barometer
  • Code-iconView source code for Barometer
  • Build-iconView package in npm Registry
  • Edit-iconEdit this page

Was this doc helpful?