diff --git a/README.md b/README.md index 88b35cf..b947b45 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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) diff --git a/RNMotionManager.xcodeproj/project.pbxproj b/RNMotionManager.xcodeproj/project.pbxproj index 1caee22..de2fbe4 100644 --- a/RNMotionManager.xcodeproj/project.pbxproj +++ b/RNMotionManager.xcodeproj/project.pbxproj @@ -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 */ @@ -45,6 +46,8 @@ BB9CC80B1AE0A707008A1D08 /* Accelerometer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Accelerometer.m; sourceTree = ""; }; BB9CC80C1AE0A707008A1D08 /* Magnetometer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Magnetometer.h; sourceTree = ""; }; BB9CC80D1AE0A707008A1D08 /* Magnetometer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Magnetometer.m; sourceTree = ""; }; + E9D0E4851E0BB165008503BD /* DeviceMotion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceMotion.h; sourceTree = ""; }; + E9D0E4861E0BB165008503BD /* DeviceMotion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeviceMotion.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -93,6 +96,8 @@ BB9CC80B1AE0A707008A1D08 /* Accelerometer.m */, BB9CC80C1AE0A707008A1D08 /* Magnetometer.h */, BB9CC80D1AE0A707008A1D08 /* Magnetometer.m */, + E9D0E4851E0BB165008503BD /* DeviceMotion.h */, + E9D0E4861E0BB165008503BD /* DeviceMotion.m */, ); path = RNMotionManager; sourceTree = ""; @@ -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; }; diff --git a/RNMotionManager/DeviceMotion.h b/RNMotionManager/DeviceMotion.h new file mode 100644 index 0000000..fae5ee9 --- /dev/null +++ b/RNMotionManager/DeviceMotion.h @@ -0,0 +1,13 @@ +#import "RCTBridgeModule.h" +#import + +@interface DeviceMotion : NSObject { + CMMotionManager *_motionManager; +} +- (void) setDeviceMotionUpdateInterval:(double) interval; +- (void) getDeviceMotionUpdateInterval:(RCTResponseSenderBlock) cb; +- (void) getDeviceMotionData:(RCTResponseSenderBlock) cb; +- (void) startDeviceMotionUpdates; +- (void) stopDeviceMotionUpdates; + +@end diff --git a/RNMotionManager/DeviceMotion.m b/RNMotionManager/DeviceMotion.m new file mode 100644 index 0000000..e8977b8 --- /dev/null +++ b/RNMotionManager/DeviceMotion.m @@ -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