Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 气压计 iconExpo 气压计

一个提供访问设备气压传感器的库。

Android
iOS (device only)
Included in Expo Go
Recommended version:
~57.0.0

expo-sensors 中的 Barometer 提供了访问设备气压计传感器的能力,以响应空气压力的变化,空气压力的测量单位为百帕(hPa)。

安装

Terminal
npx expo install expo-sensors

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基础气压计用法
import { 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>气压计:监听器 {subscription ? 'ACTIVE' : 'INACTIVE'}</Text> <Text>气压:{pressure} hPa</Text> <Text> 相对海拔:{' '} {Platform.OS === 'ios' ? `${relativeAltitude} m` : `仅在 iOS 上可用`} </Text> <TouchableOpacity onPress={toggleListener} style={styles.button}> <Text>切换监听器</Text> </TouchableOpacity> </View> ); } 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

Only for:
Android
iOS

Type: Class extends DeviceSensor<BarometerMeasurement>

Barometer Methods

addListener(listener)

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

Returns:
EventSubscription

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

Example

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

getListenerCount()

Returns the registered listeners count.

Returns:
number

getPermissionsAsync()

Checks user's permissions for accessing sensor.

Returns:
Promise<PermissionResponse>

hasListeners()

Returns boolean which signifies if sensor has any listeners registered.

Returns:
boolean

isAvailableAsync()

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

Returns:
Promise<boolean>

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

removeAllListeners()

Removes all registered listeners.

Returns:
void

removeSubscription(subscription)

ParameterType
subscriptionEventSubscription

Returns:
void

requestPermissionsAsync()

Asks the user to grant permissions for accessing sensor.

Returns:
Promise<PermissionResponse>

setUpdateInterval(intervalMs)

ParameterTypeDescription
intervalMsnumber

Desired interval in milliseconds between sensor updates.


Set the sensor update interval.

Returns:
void

Interfaces

Subscription

A subscription object that allows to conveniently remove an event listener from the emitter.

Subscription Methods

remove()

Removes an event listener for which the subscription has been created. After calling this function, the listener will no longer receive any events from the emitter.

Returns:
void

Types

BarometerMeasurement

The altitude data returned from the native sensors.

PropertyTypeDescription
pressurenumber

Measurement in hectopascals (hPa).

relativeAltitude(optional)number
Only for:
iOS

Measurement in meters (m).

timestampnumber

Timestamp of the measurement in seconds.

PermissionExpiration

Literal type: union

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

Acceptable values are: 'never' | number

PermissionResponse

An object obtained by permissions get and request functions.

PropertyTypeDescription
canAskAgainboolean

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

expiresPermissionExpiration

Determines time when the permission expires.

grantedboolean

A convenience boolean that indicates if the permission is granted.

statusPermissionStatus

Determines the status of the permission.

Enums

PermissionStatus

DENIED

PermissionStatus.DENIED = "denied"

User has denied the permission.

GRANTED

PermissionStatus.GRANTED = "granted"

User has granted the permission.

UNDETERMINED

PermissionStatus.UNDETERMINED = "undetermined"

User hasn't granted or denied the permission yet.

单位和提供方

OSUnitsProviderDescription
iOShPaCMAltimeter海拔事件反映的是当前海拔的变化,而不是绝对海拔。
AndroidhPaSensor.TYPE_PRESSURE监测气压变化。
Web此传感器在 Web 上不可用,无法访问。如果你尝试获取数据,将抛出 UnavailabilityError