YHNativeMotion.m
4.13 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// YHNativeMotion.m
// YohoExplorerDemo
//
// Created by gaoqiang xu on 4/13/15.
// Copyright (c) 2015 gaoqiang xu. All rights reserved.
//
#import "YHNativeMotion.h"
@import UIKit;
@import AudioToolbox;
@import CoreMotion;
NSString * const YHNative_Motion = @"Native_Motion";
// g constant: -9.81 m/s^2
#define kGravitationalConstant -9.81
@interface YHNativeMotion ()
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (assign) BOOL haveReturnedResult;
@property (assign) BOOL isRunning;
@property (assign) double x;
@property (assign) double y;
@property (assign) double z;
@property (assign) NSTimeInterval timestamp;
@property (assign) NSTimeInterval period;// 单位是毫秒
@end
@implementation YHNativeMotion
- (instancetype)init
{
self = [super init];
if (self) {
_isRunning = NO;
_haveReturnedResult = NO;
_period = 10;// 默认10ms
_timestamp = 0;
}
return self;
}
- (void)dealloc
{
[self clearWatchAcceleration];
}
- (NSString *)actionName
{
return YHNative_Motion;
}
- (CMMotionManager *)motionManager
{
if (!_motionManager) {
_motionManager = [[CMMotionManager alloc] init];
}
return _motionManager;
}
- (NSArray *)availableFunctions
{
return @[ @"vibrate", @"watchAcceleration", @"clearWatchAcceleration" ];
}
- (void)vibrate
{
NSInteger cycle = 1;
if (self.options && self.options[@"duration"]) {
float duration = [self.options[@"duration"] floatValue];
cycle = ceilf(duration / .4);
if (cycle < 1) {
cycle = 1;
}
}
for (NSUInteger i=0; i<cycle; i++) {
if (i==0) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * .3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
});
}
}
self.successCallBack(nil, NO, nil);
}
- (void)clearWatchAcceleration
{
if ([self.motionManager isAccelerometerAvailable]) {
if (self.haveReturnedResult == NO){
// block has not fired before stop was called, return whatever result we currently have
[self returnInfo];
}
[self.motionManager stopAccelerometerUpdates];
}
self.isRunning = NO;
self.successCallBack(nil, NO, nil);
}
- (void)watchAcceleration
{
if (self.options && self.options[@"period"]) {
self.period = [self.options[@"period"] doubleValue];
}
self.haveReturnedResult = NO;
if ([self.motionManager isAccelerometerAvailable]) {
// Assign the update interval to the motion manager and start updates
[self.motionManager setAccelerometerUpdateInterval:self.period/1000]; // expected in seconds
__weak typeof(self)weakSelf = self;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
NSTimeInterval ts = ([[NSDate date] timeIntervalSince1970] * 1000);
weakSelf.x = accelerometerData.acceleration.x;
weakSelf.y = accelerometerData.acceleration.y;
weakSelf.z = accelerometerData.acceleration.z;
weakSelf.timestamp = ts;
[weakSelf returnInfo];
}];
if (!self.isRunning) {
self.isRunning = YES;
}
} else {
self.failureCallBack(@"This device does not support accelerometer !");
}
}
- (void)returnInfo
{
// Create an acceleration object
NSMutableDictionary* accelProps = [NSMutableDictionary dictionaryWithCapacity:4];
[accelProps setValue:[NSNumber numberWithDouble:self.x * kGravitationalConstant] forKey:@"x"];
[accelProps setValue:[NSNumber numberWithDouble:self.y * kGravitationalConstant] forKey:@"y"];
[accelProps setValue:[NSNumber numberWithDouble:self.z * kGravitationalConstant] forKey:@"z"];
[accelProps setValue:[NSNumber numberWithDouble:self.timestamp] forKey:@"timestamp"];
self.successCallBack(accelProps, YES, nil);
self.haveReturnedResult = YES;
}
@end