This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo 气压计
一个可访问设备气压计传感器的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
Barometer 来自 expo-sensors,用于访问设备的气压计传感器,以响应空气压力的变化,空气压力以百帕(hPa)为单位进行测量。
安装
- npx expo install expo-sensorsIf 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>Barometer: 监听器 {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
Type: Class extends DeviceSensor<BarometerMeasurement>
Barometer Methods
| Parameter | Type | Description |
|---|---|---|
| listener | Listener<BarometerMeasurement> | A callback that is invoked when a barometer update is available. When invoked, the listener is provided with a single argument that is |
Subscribe for updates to the barometer.
EventSubscriptionA 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 }); });
Checks user's permissions for accessing sensor.
Promise<PermissionResponse>Returns boolean which signifies if sensor has any listeners registered.
booleanYou 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.
Promise<boolean>A promise that resolves to a boolean denoting the availability of the sensor.
Deprecated: use subscription.remove() instead.
| Parameter | Type |
|---|---|
| subscription | EventSubscription |
voidAsks the user to grant permissions for accessing sensor.
Promise<PermissionResponse>| Parameter | Type | Description |
|---|---|---|
| intervalMs | number | Desired interval in milliseconds between sensor updates.
|
Set the sensor update interval.
voidInterfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
The altitude data returned from the native sensors.
| Property | Type | Description |
|---|---|---|
| pressure | number | Measurement in hectopascals ( |
| relativeAltitude(optional) | number | Only for: iOS Measurement in meters ( |
| timestamp | number | Timestamp of the measurement in seconds. |
Literal Type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number
An object obtained by permissions get and request functions.
| Property | Type | Description |
|---|---|---|
| canAskAgain | boolean | 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. |
| expires | PermissionExpiration | Determines time when the permission expires. |
| granted | boolean | A convenience boolean that indicates if the permission is granted. |
| status | PermissionStatus | Determines the status of the permission. |
Enums
单位和提供方
| 操作系统 | 单位 | 提供方 | 描述 |
|---|---|---|---|
| iOS | hPa | CMAltimeter | 海拔事件反映的是当前海拔的变化,而不是绝对海拔。 |
| Android | hPa | Sensor.TYPE_PRESSURE | 监测空气压力变化。 |
| Web | 此传感器在 Web 上不可用,无法访问。如果你尝试获取数据,将抛出 UnavailabilityError。 |