xamarin 传感器步数计数器和步数探测器不工作

lx0bsm1f  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(115)

我有一个非常简单的应用程序,其中有一个按钮和一个标签.
我初始化我的传感器如下-

public void InitSensorService()
    {

        sManager = Android.App.Application.Context.GetSystemService(Context.SensorService) as SensorManager;

        sManager.RegisterListener(this, sManager.GetDefaultSensor(SensorType.StepCounter), SensorDelay.Normal);

    }

    public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
    {
        Console.WriteLine("OnAccuracyChanged called");
    }

   
      public void OnSensorChanged(SensorEvent e)
    {
        if (running)
        {
            totalSteps = e.Values[0];
            var currentSteps = Convert.ToInt32(totalSteps) - Convert.ToInt32(previousTotalSteps);

            // It will show the current steps to the user
            stepCounter = currentSteps;

        }
    }

但是步数非常非常不准确......我尝试了步数计数器、步数检测器,甚至是带有一些逻辑的加速计......它们都没有给出准确的步数......因为远加速计更近,但是步数计数器和步数检测器根本不工作

xpcnnkqh

xpcnnkqh1#

首先,你可以写一个接口来实现步数计数器.

public interface IStepCounter
{
    int Steps { get; set; }

    bool IsAvailable();

    void InitSensorService();

    void StopSensorService();
}

第二,使用Android平台中的计步器。

[assembly: Dependency(typeof(StepCounter))]
 namespace App64.Droid
 {
public class StepCounter : Java.Lang.Object, IStepCounter, ISensorEventListener
{

        private int StepsCounter = 0;
        private SensorManager sManager;

        public int Steps
        {
            get { return StepsCounter; }
            set { StepsCounter = value; }
        }

        public new void Dispose()
        {
            sManager.UnregisterListener(this);
            sManager.Dispose();
        }

        public void InitSensorService()
        {

            sManager = Android.App.Application.Context.GetSystemService(Context.SensorService) as SensorManager;

            sManager.RegisterListener(this, sManager.GetDefaultSensor(SensorType.StepDetector), SensorDelay.Fastest);
        }

        public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
        {

        Console.WriteLine("OnAccuracyChanged called");
        }

        public void OnSensorChanged(SensorEvent e)
        {
        StepsCounter ++;

            Console.WriteLine(e.ToString());
        }

        public void StopSensorService()
        {
            sManager.UnregisterListener(this);
        }

    public bool IsAvailable()
    {
        return Android.App.Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureSensorStepCounter) && Android.App.Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureSensorStepDetector);
    }
}

第三,使用Mainpage.xaml中的步进计数器。

<StackLayout>
    <Label x:Name="mylabel"/>
    <!-- Place new controls here -->
    <Button Text="click" Clicked="Button_Clicked" x:Name="myBtn" IsVisible="False"/>
</StackLayout>

Mainpage.xaml背后的代码。

[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        if (DependencyService.Get<IStepCounter>().IsAvailable())
        {
            DependencyService.Get<IStepCounter>().InitSensorService();

            myBtn.IsVisible = true;
        }
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
            mylabel.Text = DependencyService.Get<IStepCounter>().Steps.ToString();

    }
}

相关问题