0

I have a very simple app which have a button and a label.

I initialize my sensor like below-

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;

        }
    }   
    

But step counts are very very inaccurate...I tred with step counter ,step detector,even accelerometer with some logics...None of them gives accurate steps...As far accelerometer is closer,but step counter and step detector not working at all

uncle_scrooge
  • 409
  • 1
  • 5
  • 28
  • SensorEvent contains data about the event. You are assuming it fires once per step, which is not correct. There are numerous articles and samples on how to build step counters – Jason Mar 10 '23 at 13:40
  • Thanks for replying,I have gone through lot of articles...like i mentioned apart from above code I have applied lot of logics as well...but none of them give accurate results...if you can give one valid article or example..where it is working perfectly ..that would be more helpful(I know only java and c#) – uncle_scrooge Mar 10 '23 at 14:17
  • https://montemagno.com/tag/my-stepcounter/ – Jason Mar 10 '23 at 14:19
  • already tried this...not working ..downloaded their codes-https://github.com/MikeCodesDotNET/My-StepCounter,deployed it...it is not working...even their playstore url is not working – uncle_scrooge Mar 10 '23 at 14:27
  • moreover...those are 7-9 years old codes – uncle_scrooge Mar 10 '23 at 14:27
  • Here is a sample from a few months ago - https://www.geeksforgeeks.org/how-to-build-a-step-counting-application-in-android-studio/ – Jason Mar 10 '23 at 16:02
  • Ha ha...if i follow geekforgeeks article...the first step value it show is 4209 – uncle_scrooge Mar 10 '23 at 16:30
  • are you reading any of the docs? The step counter tracks total steps, not just steps for the current session. That's why the code uses a local counter that is set to zero at the start of the session – Jason Mar 10 '23 at 16:35
  • Just followed the article that you have given..updated my question codes as per the article codes... – uncle_scrooge Mar 10 '23 at 16:41

1 Answers1

-1

First, you can write an interface to achieve the stepcounter.

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

    bool IsAvailable();

    void InitSensorService();

    void StopSensorService();
}

Second, use the stepcounter in the Android Platform.

[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);
    }
}

Third, use the StepCounter in the Mainpage.xaml.

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

The code behind the 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();


    }
}
Guangyu Bai - MSFT
  • 2,555
  • 1
  • 2
  • 8