替代CMPedometer在iOS上使用加速度计计算步数

Axi*_*xil 5 iphone accelerometer ios gyroscope

由于CMPedometer不适用于iPhone5S以下.

CMPedometer StepCounting不可用

是否有一个算法代码可以用来在ios上用加速度计编程步数?

谢谢

Kuc*_*rei 1

您可以使用来自的加速度计数据检测步骤事件CMMotionManager

protected CMMotionManager _motionManager;
public event EventHandler<bool> OnMotion;

public double ACCEL_DETECTION_LIMIT = 0.31;
private const double ACCEL_REDUCE_SPEED = 0.9;

private double accel = -1;
private double accelCurrent = 0;

private void StartAccelerometerUpdates()
    {
        if (_motionManager.AccelerometerAvailable)
            _motionManager.AccelerometerUpdateInterval = ACCEL_UPDATE_INTERVAL;
        _motionManager.StartAccelerometerUpdates (NSOperationQueue.MainQueue, AccelerometerDataUpdatedHandler);
    }   

public void AccelerometerDataUpdatedHandler(CMAccelerometerData data, NSError error)
        {           
        double x = data.Acceleration.X;
        double y = data.Acceleration.Y;
        double z = data.Acceleration.Z;

        double accelLast = accelCurrent;
        accelCurrent = Math.Sqrt(x * x + y * y + z * z);
        double delta = accelCurrent - accelLast;
        accel = accel * ACCEL_REDUCE_SPEED + delta;

        var didStep = OnMotion;

        if (accel > ACCEL_DETECTION_LIMIT) 
        {                                           
            didStep (this, true);//maked a step
        } else {
            didStep (this, false);
        }
    }
Run Code Online (Sandbox Code Playgroud)