-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRNTDeviceMotion.m
86 lines (75 loc) · 6.07 KB
/
RNTDeviceMotion.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// RNTDeviceMotion.m
// RNTDeviceMotion
//
// Created by Param Aggarwal on 27/08/15.
// Copyright © 2015 Facebook. All rights reserved.
//
#import "RNTDeviceMotion.h"
#import "RCTEventDispatcher.h"
@import CoreMotion;
@implementation RNTDeviceMotion
{
CMMotionManager *_motionManager;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (id)init {
self = [super init];
if (self) {
_motionManager = [[CMMotionManager alloc] init];
}
return self;
}
RCT_EXPORT_METHOD(startDeviceMotionUpdatesWithUpdateInterval:(NSTimeInterval)updateInterval)
{
_motionManager.deviceMotionUpdateInterval = updateInterval;
[_motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
toQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error) {
if (error) {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"onDeviceMotionError"
body:@{
@"error": @{
@"description" : error.description ?: @"",
},
}];
}
[self.bridge.eventDispatcher sendDeviceEventWithName:@"onDeviceMotionUpdate"
body:@{
@"attitude": @{
@"roll" : @(motion.attitude.roll),
@"pitch" : @(motion.attitude.pitch),
@"yaw" : @(motion.attitude.yaw),
},
@"rotationRate": @{
@"x" : @(motion.rotationRate.x),
@"y" : @(motion.rotationRate.y),
@"z" : @(motion.rotationRate.z),
},
@"gravity": @{
@"x" : @(motion.gravity.x),
@"y" : @(motion.gravity.y),
@"z" : @(motion.gravity.z),
},
@"userAcceleration": @{
@"x" : @(motion.userAcceleration.x),
@"y" : @(motion.userAcceleration.y),
@"z" : @(motion.userAcceleration.z),
},
@"magneticField": @{
@"accuracy": @(motion.magneticField.accuracy),
@"field": @{
@"x" : @(motion.magneticField.field.x),
@"y" : @(motion.magneticField.field.y),
@"z" : @(motion.magneticField.field.z),
},
},
}];
}];
}
RCT_EXPORT_METHOD(stopDeviceMotionUpdates)
{
[_motionManager stopDeviceMotionUpdates];
}
@end