![](/img/trans.png)
[英]iOS device goes idle, network stops. How do i get notified about entering the 'idle' state?
[英]Finding iOS device idle state
我想通過程序找到設備的空閑狀態。 即找到設備未移動或未移動時的狀態。 我嘗試了以下加速度計和核心運動API。 但是,我無法從設備提供的值中了解設備的空閑狀態。
任何建議將不勝感激。
- (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];
}
您可以檢查此參考CmMotionManager參考 。
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;
}];
}];
測量加速度的方法是正確的,因此,如果設法獲得加速度矢量,則只需計算該矢量的范數,並檢查該值是否比某個常數大。
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!");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.