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 gravity data #23

Open
wants to merge 3 commits 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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 */; };
FF4578261D60724200F5D682 /* DeviceMotion.m in Sources */ = {isa = PBXBuildFile; fileRef = FF4578241D60724200F5D682 /* 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>"; };
FF4578241D60724200F5D682 /* DeviceMotion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeviceMotion.m; sourceTree = "<group>"; };
FF4578251D60724200F5D682 /* DeviceMotion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceMotion.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -87,6 +90,8 @@
BB9CC7EF1AE0A2EB008A1D08 /* RNMotionManager */ = {
isa = PBXGroup;
children = (
FF4578241D60724200F5D682 /* DeviceMotion.m */,
FF4578251D60724200F5D682 /* DeviceMotion.h */,
BB9CC8071AE0A5FE008A1D08 /* Gyroscope.m */,
BB9CC8091AE0A614008A1D08 /* Gyroscope.h */,
BB9CC80A1AE0A707008A1D08 /* Accelerometer.h */,
Expand Down Expand Up @@ -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;
};
Expand Down
19 changes: 19 additions & 0 deletions RNMotionManager/DeviceMotion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// DeviceMotion.h
//
// Created by Tuan PM.
//

#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
101 changes: 101 additions & 0 deletions RNMotionManager/DeviceMotion.m
Original file line number Diff line number Diff line change
@@ -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