Skip to content

Latest commit

 

History

History
82 lines (48 loc) · 3.45 KB

devicemotion.md

File metadata and controls

82 lines (48 loc) · 3.45 KB
title sourceCodeUrl
DeviceMotion

import InstallSection from '/components/plugins/InstallSection'; import PlatformsSection from '/components/plugins/PlatformsSection'; import TableOfContentSection from '~/components/plugins/TableOfContentSection';

DeviceMotion from expo-sensors provides access to the device motion and orientation sensors. All data is presented in terms of three axes that run through a device. According to portrait orientation: X runs from left to right, Y from bottom to top and Z perpendicularly through the screen from back to front.

Installation

API

import { DeviceMotion } from 'expo-sensors';

<TableOfContentSection title='Methods' contents={['DeviceMotion.isAvailableAsync()', 'DeviceMotion.addListener(listener)', 'DeviceMotion.removeAllListeners()', 'DeviceMotion.setUpdateInterval(intervalMs)']} />

Methods

DeviceMotion.isAvailableAsync()

You should always check the sensor availability before attempting to use it.

Returns whether the DeviceMotion API is enabled on the device.

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.

Returns

  • A promise that resolves to a boolean denoting the availability of the sensor.

DeviceMotion.addListener(listener)

Subscribe for updates to DeviceMotion.

Arguments

  • listener (function) -- A callback that is invoked when a DeviceMotion update is available. When invoked, the listener is provided a single argument that is an object containing following fields:

    • interval (number) -- Interval at which data is obtained from the native platform. Expressed in milliseconds.

    • acceleration (object) -- Device acceleration on the three axis as an object with x, y, z keys. Expressed in m/s2.

    • accelerationIncludingGravity (object) -- Device acceleration with the effect of gravity on the three axis as an object with x, y, z keys. Expressed in m/s2.

    • rotation (object) -- 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 (object) -- Device's rate of rotation in space expressed in degrees per second (deg/s).

      • alpha (number): X axis rotation.
      • beta (number): Y axis rotation.
      • gamma (number): Z axis rotation.
    • orientation (number) -- Device orientation based on screen rotation. Value is on of 0 (portrait), 90 (right landscape), 180 (upside down), -90 (left landscape).

Returns

  • A subscription that you can call remove() on when you would like to unsubscribe the listener.

DeviceMotion.removeAllListeners()

Remove all listeners.

DeviceMotion.setUpdateInterval(intervalMs)

Subscribe for updates to DeviceMotion.

Arguments

  • intervalMs (number) Desired interval in milliseconds between DeviceMotion updates.