Skip to content

Motion

iOS, Android, Electron, PWA

The Motion API tracks accelerometer and device orientation (compass heading, etc.)

Methods

MotionBridge.AddAccelListener()

MotionBridge.AddOrientationListener()

MotionBridge.RemoveListener()

Example

    private MotionEventResult CurrentAccel;
    private string MotionAccelWatcherID;
    private async Task MotionAccel() {
        if (MotionAccelWatcherID == null) {
            MotionAccelWatcherID = "my-motion-accel-listener";
            try {
                await MotionBridge.AddAccelListener(MotionAccelWatcherID, async (result) => {
                    CurrentAccel = result;

                });
            }
            catch (Exception e) {
                // Handle error
            }
        }
        else {
            try {
                await MotionBridge.RemoveListener(MotionAccelWatcherID);
                MotionAccelWatcherID = null;
                CurrentAccel = null;
            }
            catch (Exception e) {
                // Handle error
            }
        }
    }

    private MotionOrientationEventResult CurrentOrientation;
    private string MotionOrientationWatcherID;
    private async Task MotionOrientation() {
        if (MotionOrientationWatcherID == null) {
            MotionOrientationWatcherID = "my-motion-orientation-listener";
            try {
                await MotionBridge.AddOrientationListener(MotionOrientationWatcherID, async (result) => {
                    CurrentOrientation = result;
                });
            }
            catch (Exception e) {
                // Handle error
            }
        }
        else {
            try {
                await MotionBridge.RemoveListener(MotionAccelWatcherID);
                MotionOrientationWatcherID = null;
                CurrentOrientation = null;
            }
            catch (Exception e) {
                // Hasndle error
            }
        }
    }

API

AddAccelListener

Listen for accelerometer data

static async Task AddAccelListener(string id, Action<MotionEventResult> callback)

AddOrientationListener

Listen for device orientation change (compass heading, etc.)

static async Task AddOrientationListener(string id, Action<MotionOrientationEventResult> callback)

RemoveListener

Remove listener with given id

static Task RemoveListener(string id)

Models

MotionEventResult

    public class MotionEventResult {
        public MotionCoords acceleration;
        public MotionCoords accelerationIncludingGravity;
        public MotionOrientationEventResult rotationRate;
        public long interval;
    }

MotionCoords

    public class MotionCoords {
        public long x, y, z;
    }

MotionOrientationEventResult

    public class MotionOrientationEventResult {
        public long alpha, beta, gamma;
    }

Comments