首页指南参考教程

Expo 传感器 iconExpo 传感器

提供对设备的加速计、气压计、运动、陀螺仪、磁力计和计步器的访问的库。

Android
iOS
Web

expo-sensors 提供各种 API 用于访问设备传感器,以测量运动、方向、压力、磁场、环境光和步数。

¥expo-sensors provide various APIs for accessing device sensors to measure motion, orientation, pressure, magnetic fields, ambient light, and step count.

安装

¥Installation

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

API

import * as Sensors from 'expo-sensors';
// OR
import {
  Accelerometer,
  Barometer,
  DeviceMotion,
  Gyroscope,
  LightSensor,
  Magnetometer,
  MagnetometerUncalibrated,
  Pedometer,
} from 'expo-sensors';

权限

¥Permissions

安卓

¥Android

从 Android 12(API 级别 31)开始,系统对每个传感器更新的频率限制为 200Hz。

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

如果你需要小于 200Hz 的更新间隔,则必须将以下权限添加到 expo.android.permissions 数组内的 app.json 中。

¥If you need an update interval of less than 200Hz, you must add the following permissions to your app.json inside the expo.android.permissions array.

Android PermissionDescription

HIGH_SAMPLING_RATE_SENSORS

Allows an app to access sensor data with a sampling rate greater than 200 Hz.

Are you using this library in a bare React Native app?

了解如何在 expo-sensors 存储库中的安装说明 文件中配置原生项目。

¥Learn how to configure the native projects in the installation instructions in the expo-sensors repository.

可用传感器

¥Available sensors

有关更多信息,请参阅你感兴趣的传感器的文档:

¥For more information, please see the documentation for the sensor you are interested in:

加速度计

测量所有平台上的设备加速。

晴雨表

衡量 Android 和 iOS 平台上的压力。

DeviceMotion

测量所有平台上的设备运动。

陀螺仪

测量所有平台上的设备旋转。

磁力计

在 Android 和 iOS 平台上测量磁场。

LightSensor

在 Android 平台上测量环境光。

计步器

Android 和 iOS 平台上的测量步数。

Expo 中文网 - 粤ICP备13048890号