如何使用Xamarin

时间:2019-01-28 22:08:50

标签: c# ios xamarin pedometer

我编写了一个接口和android实现,用于使用Android SensorService计数步骤。现在,我正在尝试对iOS执行相同的操作。除了这个https://github.com/xamarin/ios-samples/blob/master/PrivacyPrompts/PrivacyPrompts/PrivacyManager/MotionPrivacyManager.cs之外,我找不到任何有关此主题的代码示例,该示例返回两个时间间隔之间的步数。但是我需要实时监控步数。或接近它。 Android SensorService有一些延迟,但它是实时进行的。我需要对iOS执行相同的操作。 iOS中有办法吗?

也许我需要制作一个2个时间范围的时间窗口,然后尝试以这种方式监视数据?但是时间范围可能很小,从一秒到5分钟。它甚至可以工作吗?

using CoreMotion;
using Foundation;
using UIKit;

namespace PrivacyPrompts
{
public class MotionPrivacyManager : IPrivacyManager, IDisposable
{
    CMStepCounter stepCounter;
    string motionStatus = "Indeterminate";
    nint steps = 0;

    CMMotionManager motionManger; // before iOS 8.0
    CMPedometer pedometer; // since iOS 8.0

    public MotionPrivacyManager ()
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
            pedometer = new CMPedometer ();
            motionStatus = CMPedometer.IsStepCountingAvailable ? "Available" : "Not available";
        } else {
            stepCounter = new CMStepCounter ();
            motionManger = new CMMotionManager ();
            motionStatus = motionManger.DeviceMotionAvailable ? "Available" : "Not available";
        }
    }

    public Task RequestAccess ()
    {
        var yesterday = NSDate.FromTimeIntervalSinceNow (-60 * 60 * 24);

        if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
            if(!CMPedometer.IsStepCountingAvailable)
                return Task.FromResult<object> (null);

            return pedometer.QueryPedometerDataAsync (yesterday, NSDate.Now)
                .ContinueWith (PedometrQueryContinuation);
        } else {
            if (!motionManger.DeviceMotionAvailable)
                return Task.FromResult<object> (null);

            return stepCounter.QueryStepCountAsync (yesterday, NSDate.Now, NSOperationQueue.MainQueue)
                .ContinueWith (StepQueryContinuation);
        }

    }

    void PedometrQueryContinuation(Task<CMPedometerData> t)
    {
        if (t.IsFaulted) {
            var code = ((NSErrorException)t.Exception.InnerException).Code;
            if (code == (int)CMError.MotionActivityNotAuthorized)
                motionStatus = "Not Authorized";
            return;
        }

        steps = t.Result.NumberOfSteps.NIntValue;
    }

    void StepQueryContinuation(Task<nint> t)
    {
        if (t.IsFaulted) {
            var code = ((NSErrorException)t.Exception.InnerException).Code;
            if (code == (int)CMError.MotionActivityNotAuthorized)
                motionStatus = "Not Authorized";
            return;
        }

        steps = t.Result;
    }

    public string CheckAccess ()
    {
        return motionStatus;
    }

    public string GetCountsInfo()
    {
        return steps > 0 ? string.Format ("You have taken {0} steps in the past 24 hours", steps) : string.Empty;
    }

    public void Dispose ()
    {
        motionManger.Dispose ();
        stepCounter.Dispose ();
    }
}

0 个答案:

没有答案