Expo DeviceMotion
一个提供访问设备运动和方向传感器的库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-sensors 中的 DeviceMotion 提供对设备运动和方向传感器的访问。所有数据都以穿过设备的三轴来表示。根据竖屏方向:X 轴从左到右,Y 轴从下到上,Z 轴垂直于屏幕,从后到前。
安装
- npx expo install expo-sensorsIf you are installing this in an existing React Native app, make sure to install expo in your project.
在 app 配置中进行配置
如果你的项目中使用了配置插件(Continuous Native Generation (CNG)),你可以使用 DeviceMotion 的内置 config plugin 来配置 expo-sensor。该插件允许你配置一些无法在运行时设置、且需要构建新的应用二进制文件才能生效的属性。如果你的应用没有使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-sensors", { "motionPermission": "允许 $(PRODUCT_NAME) 访问你的设备运动。" } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
motionPermission | "允许 $(PRODUCT_NAME) 访问你的设备运动" | Only for: iOS 用于设置 |
Are you using this library in an existing React Native app?
如果你没有使用 Continuous Native Generation(CNG),或者你在手动使用原生 ios 项目,那么你需要在原生项目中配置 NSMotionUsageDescription 键,以便访问 DeviceMotion 统计信息:
<key>NSMotionUsageDescription</key> <string>允许 $(PRODUCT_NAME) 访问你的设备运动</string>
API
import { DeviceMotion } from 'expo-sensors';
Constants
Type: number
Constant value representing standard gravitational acceleration for Earth (9.80665 m/s^2).
Classes
Type: Class extends DeviceSensor<DeviceMotionMeasurement>
A base class for subscribable sensors. The events emitted by this class are measurements
specified by the parameter type Measurement.
DeviceMotion Properties
DeviceMotion Methods
| Parameter | Type | Description |
|---|---|---|
| listener | Listener<DeviceMotionMeasurement> | A callback that is invoked when a device motion sensor update is available. When invoked,
the listener is provided a single argument that is a |
Subscribe for updates to the device motion sensor.
EventSubscriptionA subscription that you can call remove() on when you would like to unsubscribe the listener.
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.
Returns whether the accelerometer is enabled on the device.
On mobile web, you must first invoke DeviceMotion.requestPermissionsAsync() in a user interaction (i.e. touch event) before you can use this module.
If the status is not equal to granted then you should inform the end user that they may have to open settings.
On web this starts a timer and waits to see if an event is fired. This should predict if the iOS device has the device orientation API disabled in
Settings > Safari > Motion & Orientation Access. Some devices will also not fire if the site isn't hosted with HTTPS as DeviceMotion is now considered a secure API.
There is no formal API for detecting the status of DeviceMotion so this API can sometimes be unreliable on web.
Promise<boolean>A promise that resolves to a boolean denoting the availability of device motion 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
| Property | Type | Description |
|---|---|---|
| acceleration | null | {
timestamp: number,
x: number,
y: number,
z: number
} | Device acceleration on the three axis as an object with |
| accelerationIncludingGravity | {
timestamp: number,
x: number,
y: number,
z: number
} | Device acceleration with the effect of gravity on the three axis as an object with |
| interval | number | Interval at which data is obtained from the native platform. Expressed in milliseconds (ms). |
| orientation | DeviceMotionOrientation | Device orientation based on screen rotation. Value is one of:
|
| rotation | {
alpha: number,
beta: number,
gamma: number,
timestamp: number
} | Device's orientation in space as an object with alpha, beta, gamma keys where alpha is for rotation around Z axis, beta for X axis rotation and gamma for Y axis rotation. |
| rotationRate | null | {
alpha: number,
beta: number,
gamma: number,
timestamp: number
} | Device's rate of rotation in space expressed in degrees per second (deg/s). |
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
此库使用以下用途说明键: