diff --git a/README.md b/README.md index 3bf0319..7f3f42a 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,20 @@ Magnetometer.startMagnetometerUpdates(); // you'll start getting AccelerationDat Magnetometer.stopMagnetometerUpdates(); ``` +### Filtered DeviceMotion data (only gravity) +```js +DeviceMotion.setDeviceMotionUpdateInterval(0.1); // in seconds +DeviceEventEmitter.addListener('MotionData', function (data) { + /** + * data.gravity.x + * data.gravity.y + * data.gravity.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..f035686 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 */; }; + FF4578261D60724200F5D682 /* DeviceMotion.m in Sources */ = {isa = PBXBuildFile; fileRef = FF4578241D60724200F5D682 /* 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 = ""; }; + FF4578241D60724200F5D682 /* DeviceMotion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeviceMotion.m; sourceTree = ""; }; + FF4578251D60724200F5D682 /* DeviceMotion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceMotion.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -87,6 +90,8 @@ BB9CC7EF1AE0A2EB008A1D08 /* RNMotionManager */ = { isa = PBXGroup; children = ( + FF4578241D60724200F5D682 /* DeviceMotion.m */, + FF4578251D60724200F5D682 /* DeviceMotion.h */, BB9CC8071AE0A5FE008A1D08 /* Gyroscope.m */, BB9CC8091AE0A614008A1D08 /* Gyroscope.h */, BB9CC80A1AE0A707008A1D08 /* Accelerometer.h */, @@ -204,6 +209,7 @@ BB9CC80E1AE0A707008A1D08 /* Accelerometer.m in Sources */, BB9CC80F1AE0A707008A1D08 /* Magnetometer.m in Sources */, BB9CC8081AE0A5FE008A1D08 /* Gyroscope.m in Sources */, + FF4578261D60724200F5D682 /* DeviceMotion.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/RNMotionManager/DeviceMotion.h b/RNMotionManager/DeviceMotion.h new file mode 100644 index 0000000..e49909c --- /dev/null +++ b/RNMotionManager/DeviceMotion.h @@ -0,0 +1,19 @@ +// +// DeviceMotion.h +// +// Created by Tuan PM. +// + +#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..ff9fff5 --- /dev/null +++ b/RNMotionManager/DeviceMotion.m @@ -0,0 +1,101 @@ +// +// DeviceMotion.m +// +// Created by Tuan PM. +// + +#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]; + //Magnetometer + if([self->_motionManager isDeviceMotionAvailable]) + { + NSLog(@"deviceMotionAvailable available"); + /* Start the Magnetometer 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 x = self->_motionManager.deviceMotion.gravity.x; + double y = self->_motionManager.deviceMotion.gravity.y; + double z = self->_motionManager.deviceMotion.gravity.z; + + NSLog(@"getDeviceMotionData: %f, %f, %f", x, y, z); + + cb(@[[NSNull null], @{ + @"gravity": @{ + @"x" : [NSNumber numberWithDouble:x], + @"y" : [NSNumber numberWithDouble:y], + @"z" : [NSNumber numberWithDouble:z] + } + }] + ); +} + +RCT_EXPORT_METHOD(startDeviceMotionUpdates) { + NSLog(@"startMagnetometerUpdates"); + [self->_motionManager startDeviceMotionUpdates]; + + /* Receive the ccelerometer data on this block */ + [self->_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] + withHandler:^(CMDeviceMotion *motionData, NSError *error) + { + double x = motionData.gravity.x; + double y = motionData.gravity.y; + double z = motionData.gravity.z; + NSLog(@"startDeviceMotionUpdates: %f, %f, %f", x, y, z); + + [self.bridge.eventDispatcher sendDeviceEventWithName:@"MotionData" body:@{ + @"gravity": @{ + @"x" : [NSNumber numberWithDouble:x], + @"y" : [NSNumber numberWithDouble:y], + @"z" : [NSNumber numberWithDouble:z] + } + }]; + }]; + +} + +RCT_EXPORT_METHOD(stopDeviceMotionUpdates) { + NSLog(@"stopDeviceMotionUpdates"); + [self->_motionManager stopDeviceMotionUpdates]; +} + +@end