Skip to content

Instantly share code, notes, and snippets.

@thomhines
Last active April 18, 2020 21:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomhines/a31f8a33b7bc28dfc55381be3b91dd60 to your computer and use it in GitHub Desktop.
Save thomhines/a31f8a33b7bc28dfc55381be3b91dd60 to your computer and use it in GitHub Desktop.
Capacitor iosMotion - Simple, partially-functional workaround for Capacitor Motion plugin (uses Native API instead of Web API). Place the .swift and .h files in the root of your App folder in Xcode, then add the javascript stuff to your app's code.
// Register the plugin with Capacitor
import { Plugins } from '@capacitor/core';
const { Keyboard } = Plugins;
// ORIENTATION
// Start tracking device orientation. This must be run in order for the following event listener to work
iosMotion.getOrientation()
// Set up listener for non-ios devices
var orientation_event_listener = Motion.addListener("orientation", function(e) {
// Do something with orientation data. e will contain values for alpha, beta, and gamma in degrees
})
// Replace listener on ios
if(device.platform == 'ios') orientation_event_listener = iosMotion.addListener('orientation', function(e) {
// Do something with orientation data. e will contain values for alpha, beta (not at all correct), and gamma in degrees
});
// When finished, stop tracking device orientation
iosMotion.stopOrientation()
// ACCELERATION
// Start tracking device acceleration. This must be run in order for the following event listener to work
iosMotion.getAcceleration()
// Set up listener for non-ios devices
var acceleration_event_listener = Motion.addListener("accel", function(e) {
// Do something with orientation data. e will contain values for alpha, beta, and gamma in degrees
})
// Replace listener on ios
if(device.platform == 'ios') acceleration_event_listener = iosMotion.addListener('accel', function(e) {
// Do something with orientation data. e will contain values for alpha, beta (not at all correct), and gamma in degrees
// Note: these values seem to work, but might require being multiplied by some constant to get a value similar to the Web API values
});
// When finished, stop tracking device acceleration
iosMotion.stopAcceleration()
//
// iosMotion.m
// App
//
// Created by Thom Hines on 12/8/19.
//
#import <Foundation/Foundation.h>
#import <Capacitor/Capacitor.h>
CAP_PLUGIN(iosMotion, "iosMotion",
CAP_PLUGIN_METHOD(getOrientation, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(stopOrientation, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAcceleration, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(stopAcceleration, CAPPluginReturnPromise);
)
//
// iosMotion.swift
// App
//
// Created by Thom Hines on 12/8/19.
//
import Foundation
import Capacitor
import CoreMotion
var motionManager: CMMotionManager!
@objc(iosMotion)
public class iosMotion: CAPPlugin {
@objc func getOrientation(_ call: CAPPluginCall) {
motionManager = CMMotionManager()
if motionManager.isDeviceMotionAvailable {
motionManager.deviceMotionUpdateInterval = 0.01
motionManager.startDeviceMotionUpdates(to: OperationQueue.main) { (odata, error) in
// Values come in as yaw, pitch, roll
var alpha = (odata?.attitude.yaw ?? 0.0) * 180 / 3.14159 - 90;
var beta = (odata?.attitude.pitch ?? 0.0) * 180 / 3.14159;
var gamma = (odata?.attitude.roll ?? 0.0) * 180 / 3.14159;
if gamma > -90.0 {
alpha = alpha + 180
}
// Beta is completely wrong! I have no idea how to convert this. Sorry!
// beta = beta * 1
if gamma <= -90.0 && gamma >= -180.0 {
gamma = -180.0 - gamma
}
print(["alpha": alpha, "beta": beta, "gamma": gamma])
self.notifyListeners("orientation", data: ["alpha": alpha, "beta": beta, "gamma": gamma])
}
}
}
@objc func stopOrientation(_ call: CAPPluginCall) {
motionManager.stopDeviceMotionUpdates()
}
@objc func getAcceleration(_ call: CAPPluginCall) {
motionManager = CMMotionManager()
if motionManager.isDeviceMotionAvailable {
motionManager.deviceMotionUpdateInterval = 0.01
motionManager.startDeviceMotionUpdates(to: OperationQueue.main) { (data, error) in
// Note: these values seem to work, but might require being multiplied by some constant to get a value similar to the Web API values
self.notifyListeners("accel", data: ["x": data?.userAcceleration.x, "y": data?.userAcceleration.y, "z": data?.userAcceleration.z])
}
}
}
@objc func stopAcceleration(_ call: CAPPluginCall) {
motionManager.stopAccelerometerUpdates()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment