2015-04-02 76 views
1

我想通过程序找到设备空闲状态。即找到设备未移动或移动的状态。 我尝试了以下加速计和核心运动API。但是,我无法从它给出的值中理解设备空闲状态。寻找iOS设备空闲状态

任何建议,将不胜感激。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 
    currentMaxAccelX = 0; 
    currentMaxAccelY = 0; 
    currentMaxAccelZ = 0; 

    currentMaxRotX = 0; 
    currentMaxRotY = 0; 
    currentMaxRotZ = 0; 

    value = 0; 

    self.motionManager = [[CMMotionManager alloc] init]; 
    self.motionManager.accelerometerUpdateInterval = .2; 
    self.motionManager.gyroUpdateInterval = .2; 

    [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
              withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
               [self outputAccelertionData:accelerometerData.acceleration]; 
               if(error){ 

                NSLog(@"%@", error); 
               } 
              }]; 

    [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] 
            withHandler:^(CMGyroData *gyroData, NSError *error) { 
             [self outputRotationData:gyroData.rotationRate]; 
            }]; 
} 

-(void)outputAccelertionData:(CMAcceleration)acceleration 
{ 
    self.accX.text = [NSString stringWithFormat:@"accX: %.2fg",acceleration.x]; 
    if(fabs(acceleration.x) > fabs(currentMaxAccelX)) 
    { 
     currentMaxAccelX = acceleration.x; 
    } 

    self.accY.text = [NSString stringWithFormat:@"accY: %.2fg",acceleration.y]; 
    if(fabs(acceleration.y) > fabs(currentMaxAccelY)) 
    { 
     currentMaxAccelY = acceleration.y; 
    } 

    if (fabs(acceleration.y) - fabs(value) > 0.2 || fabs(value) - fabs(acceleration.y) > 0.2) 
    { 
     NSLog(@"Not idle"); 
    } 
    value = acceleration.y; 


    self.accZ.text = [NSString stringWithFormat:@"accZ: %.2fg",acceleration.z]; 
    if(fabs(acceleration.z) > fabs(currentMaxAccelZ)) 
    { 
     currentMaxAccelZ = acceleration.z; 
    } 

    self.maxAccX.text = [NSString stringWithFormat:@"maxAccX: %.2f",currentMaxAccelX]; 
    self.maxAccY.text = [NSString stringWithFormat:@"maxAccY: %.2f",currentMaxAccelY]; 
    self.maxAccZ.text = [NSString stringWithFormat:@"maxAccZ: %.2f",currentMaxAccelZ]; 

} 

-(void)outputRotationData:(CMRotationRate)rotation 
{ 
    self.rotX.text = [NSString stringWithFormat:@"rotX: %.2fr/s",rotation.x]; 
    if(fabs(rotation.x) > fabs(currentMaxRotX)) 
    { 
     currentMaxRotX = rotation.x; 
    } 
    self.rotY.text = [NSString stringWithFormat:@"rotY: %.2fr/s",rotation.y]; 
    if(fabs(rotation.y) > fabs(currentMaxRotY)) 
    { 
     currentMaxRotY = rotation.y; 
    } 
    self.rotZ.text = [NSString stringWithFormat:@"rotZ: %.2fr/s",rotation.z]; 
    if(fabs(rotation.z) > fabs(currentMaxRotZ)) 
    { 
     currentMaxRotZ = rotation.z; 
    } 
    self.maxRotX.text = [NSString stringWithFormat:@"maxRotX: %.2f",currentMaxRotX]; 
    self.maxRotY.text = [NSString stringWithFormat:@"maxRotY: %.2f",currentMaxRotY]; 
    self.maxRotZ.text = [NSString stringWithFormat:@"maxRotZ: %.2f",currentMaxRotZ]; 
} 
+0

“我无法从它给出的值中理解设备空闲状态。”它给了什么价值? – matt 2015-04-02 17:43:51

+0

你能建议我所尝试的是正确的方法吗?你能否建议一些方法在iOS设备上查找设备空闲时间? – Stella 2015-04-06 05:30:29

+0

如果通过闲置,您的意思是_not moving _...请将问题文本更改为使用单词** stationary **代替空闲_....用于电话(或一般电子设备)空闲状态是指没有正在运行的程序或一般没有数字活动)等。 – lukya 2015-04-10 18:59:23

回答

0

您可以查看该参考文献CmMotionManager reference

self.deviceQueue = [[NSOperationQueue alloc] init]; 
self.motionManager = [[CMMotionManager alloc] init]; 
self.motionManager.deviceMotionUpdateInterval = 5.0/60.0; 

// UIDevice *device = [UIDevice currentDevice]; 

[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical 
                toQueue:self.deviceQueue 
               withHandler:^(CMDeviceMotion *motion, NSError *error) 
{ 
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
    CGFloat x = motion.gravity.x; 
    CGFloat y = motion.gravity.y; 
    CGFloat z = motion.gravity.z; 
}]; 
}]; 
0

与测量加速度的方法是正确的,所以如果你设法获得加速度矢量,你应该简单地计算the norm of the vector,并检查值刨丝器比一些常数。

CGFloat norm = sqrt(acceleration.x * acceleration.x + acceleration.y * acceleration.y + acceleration.z * acceleration.z); 
NSLog(@"Norm of the vector = %f", norm); 
CGFloat minimumValueToConsiderThatDeviceIsMoving = 0.2; // obtain more accurate value experimentally 
if (norm > minimumValueToConsiderThatDeviceIsMoving) { 
    NSLog(@"Device is moving!"); 
} else { 
    NSLog(@"Device is still!"); 
}