Peter Lapisu
Peter Lapisu

Reputation: 21005

CMMotionManager detecting when the device is standing still

How to setup the handling so i can detect when the device is standing still (in some threshold)

the code below isn't working as expected (the userAcceleration is just grater, depending on the device orientation, and the user even doesn't move the device (this value possibly comes from the gravity))

self.motionManager = [[CMMotionManager alloc] init];
        self.motionManager.accelerometerUpdateInterval = 1/2.0;
        [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {

            float accelerationThreshold = 0.75;
            CMAcceleration userAcceleration = accelerometerData.acceleration;
            if ((fabs(userAcceleration.x) > accelerationThreshold)
                || (fabs(userAcceleration.y) > accelerationThreshold)
                || (fabs(userAcceleration.z) > accelerationThreshold)) {
                self.deviceMoved = YES;
            } else {
                self.deviceMoved = NO;
            }

        }];

Upvotes: 0

Views: 698

Answers (1)

Peter Lapisu
Peter Lapisu

Reputation: 21005

needed to use the startDeviceMotionUpdatesToQueue

if (self.motionManager == nil) {
        self.motionManager = [[CMMotionManager alloc] init];
        self.motionManager.accelerometerUpdateInterval = 1/2.0;
        [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {

            float accelerationThreshold = 0.25;
            CMAcceleration userAcceleration = motion.userAcceleration;
            if ((fabs(userAcceleration.x) > accelerationThreshold)
                || (fabs(userAcceleration.y) > accelerationThreshold)
                || (fabs(userAcceleration.z) > accelerationThreshold)) {
                self.deviceMoved = YES;
            } else {
                self.deviceMoved = NO;
            }

        }];
    }

Upvotes: 1

Related Questions