Expo 传感器 iconExpo 传感器

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

Android
iOS
Web
Included in Expo Go
Bundled version:
~15.0.8

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

安装

🌐 Installation

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.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),可以使用其内置的 配置插件 来配置 expo-sensors。该插件允许你配置无法在运行时设置并且需要构建新的应用二进制文件才能生效的各种属性。如果你的应用使用 CNG,那么你需要手动配置该库。

🌐 You can configure expo-sensors using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-sensors", { "motionPermission": "Allow $(PRODUCT_NAME) to access your device motion" } ] ] } }

Configurable properties

NameDefaultDescription
motionPermission"允许 $(PRODUCT_NAME) 访问你的设备运动"
Only for:
iOS

一个字符串,用于设置 NSMotionUsageDescription 权限消息,或使用 false 来禁用运动权限。

应用接口

🌐 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 的更新间隔,你必须在 app.json 中的 expo.android.permissions 数组里添加以下权限。

🌐 If you need an update interval of greater 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 an existing React Native app?

如果你没有使用连续本地生成(CNG)或者你正在手动使用原生 android 项目,请将 HIGH_SAMPLING_RATE_SENSORS 权限添加到你项目的 android/app/src/main/AndroidManifest.xml 中:

🌐 If you're not using Continuous Native Generation (CNG) or you're using native android project manually, add HIGH_SAMPLING_RATE_SENSORS permission to your project's android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />

iOS

该库使用以下用法描述键:

🌐 The following usage description keys are used by this library:

Info.plist KeyDescription

NSMotionUsageDescription

A message that tells the user why the app is requesting access to the device’s motion data.

可用传感器

🌐 Available sensors

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

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

加速度计

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

晴雨表

测量 Android 和 iOS 平台的压力。

DeviceMotion

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

陀螺仪

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

磁力计

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

LightSensor

在 Android 平台上测量环境光。

计步器

在 Android 和 iOS 平台上计算步数。