Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DeviceMotion (Acceleration Data) #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# react-native-motion-manager

CMMotionManager wrapper for react-native, exposing Accelerometer, Gyroscope, and Magnetometer.
CMMotionManager wrapper for react-native, exposing Accelerometer, Gyroscope, Magnetometer, and DeviceMotion.

### Add it to your project

Expand All @@ -22,7 +22,8 @@ If you get stuck, take a look at [Brent Vatne's blog](http://brentvatne.ca/packa
var {
Accelerometer,
Gyroscope,
Magnetometer
Magnetometer,
DeviceMotion
} = require('NativeModules');
var {
DeviceEventEmitter // will emit events that you can listen to
Expand Down Expand Up @@ -72,6 +73,23 @@ Magnetometer.startMagnetometerUpdates(); // you'll start getting MagnetomerData
Magnetometer.stopMagnetometerUpdates();
```

### DeviceMotion (Acceleration Data)
```js
DeviceMotion.setDeviceMotionUpdateInterval(0.1); // in seconds
DeviceEventEmitter.addListener('MotionData', function (data) {
/**
* data.gravity.x
* data.gravity.y
* data.gravity.z
* data.userAcceleration.x
* data.userAcceleration.y
* data.userAcceleration.z
**/
});
DeviceMotion.startDeviceMotionUpdates(); // you'll start getting MotionData events above
DeviceMotion.stopDeviceMotionUpdates();
```

# Example

This repo contains an example react-native app to help get you started. [Source code here.](https://github.com/pwmckenna/react-native-motion-manager/tree/master/Example/MotionExample)
Expand Down
6 changes: 6 additions & 0 deletions RNMotionManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BB9CC8081AE0A5FE008A1D08 /* Gyroscope.m in Sources */ = {isa = PBXBuildFile; fileRef = BB9CC8071AE0A5FE008A1D08 /* Gyroscope.m */; };
BB9CC80E1AE0A707008A1D08 /* Accelerometer.m in Sources */ = {isa = PBXBuildFile; fileRef = BB9CC80B1AE0A707008A1D08 /* Accelerometer.m */; };
BB9CC80F1AE0A707008A1D08 /* Magnetometer.m in Sources */ = {isa = PBXBuildFile; fileRef = BB9CC80D1AE0A707008A1D08 /* Magnetometer.m */; };
E9D0E4871E0BB165008503BD /* DeviceMotion.m in Sources */ = {isa = PBXBuildFile; fileRef = E9D0E4861E0BB165008503BD /* DeviceMotion.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -45,6 +46,8 @@
BB9CC80B1AE0A707008A1D08 /* Accelerometer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Accelerometer.m; sourceTree = "<group>"; };
BB9CC80C1AE0A707008A1D08 /* Magnetometer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Magnetometer.h; sourceTree = "<group>"; };
BB9CC80D1AE0A707008A1D08 /* Magnetometer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Magnetometer.m; sourceTree = "<group>"; };
E9D0E4851E0BB165008503BD /* DeviceMotion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceMotion.h; sourceTree = "<group>"; };
E9D0E4861E0BB165008503BD /* DeviceMotion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeviceMotion.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -93,6 +96,8 @@
BB9CC80B1AE0A707008A1D08 /* Accelerometer.m */,
BB9CC80C1AE0A707008A1D08 /* Magnetometer.h */,
BB9CC80D1AE0A707008A1D08 /* Magnetometer.m */,
E9D0E4851E0BB165008503BD /* DeviceMotion.h */,
E9D0E4861E0BB165008503BD /* DeviceMotion.m */,
);
path = RNMotionManager;
sourceTree = "<group>";
Expand Down Expand Up @@ -204,6 +209,7 @@
BB9CC80E1AE0A707008A1D08 /* Accelerometer.m in Sources */,
BB9CC80F1AE0A707008A1D08 /* Magnetometer.m in Sources */,
BB9CC8081AE0A5FE008A1D08 /* Gyroscope.m in Sources */,
E9D0E4871E0BB165008503BD /* DeviceMotion.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 13 additions & 0 deletions RNMotionManager/DeviceMotion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "RCTBridgeModule.h"
#import <CoreMotion/CoreMotion.h>

@interface DeviceMotion : NSObject <RCTBridgeModule> {
CMMotionManager *_motionManager;
}
- (void) setDeviceMotionUpdateInterval:(double) interval;
- (void) getDeviceMotionUpdateInterval:(RCTResponseSenderBlock) cb;
- (void) getDeviceMotionData:(RCTResponseSenderBlock) cb;
- (void) startDeviceMotionUpdates;
- (void) stopDeviceMotionUpdates;

@end
111 changes: 111 additions & 0 deletions RNMotionManager/DeviceMotion.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "DeviceMotion.h"

@implementation DeviceMotion

@synthesize bridge = _bridge;

RCT_EXPORT_MODULE();

- (id) init {
self = [super init];
NSLog(@"DeviceMotion");

if (self) {
self->_motionManager = [[CMMotionManager alloc] init];
// DeviceMotion
if([self->_motionManager isDeviceMotionAvailable])
{
NSLog(@"DeviceMotion available");
/* Start the DeviceMotion if it is not active already */
if([self->_motionManager isDeviceMotionActive] == NO)
{
NSLog(@"DeviceMotion active");
} else {
NSLog(@"DeviceMotion not active");
}
}
else
{
NSLog(@"DeviceMotion not Available!");
}
}
return self;
}

RCT_EXPORT_METHOD(setDeviceMotionUpdateInterval:(double) interval) {
NSLog(@"setDeviceMotionUpdateInterval: %f", interval);
[self->_motionManager setDeviceMotionUpdateInterval:interval];
}

RCT_EXPORT_METHOD(getDeviceMotionUpdateInterval:(RCTResponseSenderBlock) cb) {
double interval = self->_motionManager.deviceMotionUpdateInterval;
NSLog(@"getDeviceMotionUpdateInterval: %f", interval);
cb(@[[NSNull null], [NSNumber numberWithDouble:interval]]);
}

RCT_EXPORT_METHOD(getDeviceMotionData:(RCTResponseSenderBlock) cb) {
double gravity_x = self->_motionManager.deviceMotion.gravity.x;
double gravity_y = self->_motionManager.deviceMotion.gravity.y;
double gravity_z = self->_motionManager.deviceMotion.gravity.z;
double userAcceleration_x = self->_motionManager.deviceMotion.userAcceleration.x;
double userAcceleration_y = self->_motionManager.deviceMotion.userAcceleration.y;
double userAcceleration_z = self->_motionManager.deviceMotion.userAcceleration.z;

NSLog(@"getDeviceMotionData (gravity): %f, %f, %f", gravity_x, gravity_y, gravity_z);
NSLog(@"getDeviceMotionData (userAcceleration): %f, %f, %f", userAcceleration_x, userAcceleration_y, userAcceleration_z);

cb(@[[NSNull null], @{
@"gravity": @{
@"x" : [NSNumber numberWithDouble:gravity_x],
@"y" : [NSNumber numberWithDouble:gravity_y],
@"z" : [NSNumber numberWithDouble:gravity_z]
},
@"userAcceleration": @{
@"x" : [NSNumber numberWithDouble:userAcceleration_x],
@"y" : [NSNumber numberWithDouble:userAcceleration_y],
@"z" : [NSNumber numberWithDouble:userAcceleration_z]
}
}]
);
}

RCT_EXPORT_METHOD(startDeviceMotionUpdates) {
NSLog(@"startMagnetometerUpdates");
[self->_motionManager startDeviceMotionUpdates];

/* Receive the DeviceMotion data on this block */
[self->_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motionData, NSError *error)
{
double gravity_x = motionData.gravity.x;
double gravity_y = motionData.gravity.y;
double gravity_z = motionData.gravity.z;
double userAcceleration_x = motionData.userAcceleration.x;
double userAcceleration_y = motionData.userAcceleration.y;
double userAcceleration_z = motionData.userAcceleration.z;
NSLog(@"startDeviceMotionUpdates (gravity): %f, %f, %f", gravity_x, gravity_y, gravity_z);
NSLog(@"startDeviceMotionUpdates (userAcceleration): %f, %f, %f", userAcceleration_x, userAcceleration_y, userAcceleration_z);

[self.bridge.eventDispatcher sendDeviceEventWithName:@"MotionData" body:@{
@"gravity": @{
@"x" : [NSNumber numberWithDouble:gravity_x],
@"y" : [NSNumber numberWithDouble:gravity_y],
@"z" : [NSNumber numberWithDouble:gravity_z]
},
@"userAcceleration": @{
@"x" : [NSNumber numberWithDouble:userAcceleration_x],
@"y" : [NSNumber numberWithDouble:userAcceleration_y],
@"z" : [NSNumber numberWithDouble:userAcceleration_z]
},
}];
}];
}

RCT_EXPORT_METHOD(stopDeviceMotionUpdates) {
NSLog(@"stopDeviceMotionUpdates");
[self->_motionManager stopDeviceMotionUpdates];
}

@end