提供对设备气压计传感器的访问的库。
Barometer
和 expo-sensors
提供对设备气压计传感器的访问,以响应气压变化,气压以百帕斯卡 (hPa
) 为单位进行测量。
¥Barometer
from expo-sensors
provides access to the device barometer sensor to respond to changes in air pressure, which is measured in hectopascals (hPa
).
¥Installation
-
npx expo install expo-sensors
If you are installing this in an existing React Native app (bare workflow), start by installing expo
in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.
¥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>
);
}
const styles = StyleSheet.create({
button: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eee',
padding: 10,
marginTop: 15,
},
wrapper: {
flex: 1,
alignItems: 'stretch',
justifyContent: 'center',
paddingHorizontal: 20,
},
});
import { Barometer } from 'expo-sensors';
Barometer
Type: Class extends DeviceSensor<BarometerMeasurement>
Barometer Methods
addListener(listener)
Name | 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.
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 });
});
hasListeners()
Returns boolean which signifies if sensor has any listeners registered.
boolean
isAvailableAsync()
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.
Promise<boolean>
A promise that resolves to a boolean
denoting the availability of the sensor.
removeSubscription(subscription)
Name | Type | Description |
---|---|---|
subscription | Subscription | A subscription to remove. |
Removes the given subscription.
void
setUpdateInterval(intervalMs)
Name | Type | Description |
---|---|---|
intervalMs | number | Desired interval in milliseconds between sensor updates.
|
Set the sensor update interval.
void
PermissionResponse
An object obtained by permissions get and request functions.
PermissionResponse Properties
Name | 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. |
BarometerMeasurement
The altitude data returned from the native sensors.
Name | Type | Description |
---|---|---|
pressure | number | Measurement in hectopascals ( |
relativeAltitude (optional) | number | Only for: iOS Measurement in meters ( |
timestamp | number | Timestamp of the measurement in seconds. |
PermissionExpiration
Literal Type: multiple types
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never'
| number
Subscription
Name | Type | Description |
---|---|---|
remove | () => void | A method to unsubscribe the listener. |
PermissionStatus
PermissionStatus Values
UNDETERMINED
PermissionStatus.UNDETERMINED = "undetermined"
User hasn't granted or denied the permission yet.
¥Units and providers
OS | 单位 | 提供者 | 描述 |
---|---|---|---|
iOS 系统 | hPa | CMAltimeter | 海拔事件反映的是当前海拔的变化,而不是绝对海拔。 |
安卓 | hPa | Sensor.TYPE_PRESSURE | 监测气压变化。 |
Web | 该传感器在网络上不可用,无法访问。如果你尝试获取数据,将会抛出 UnavailabilityError 。 |