Hello,
Welcome to our Microsoft Q&A platform!
You need to add following uses-feature
in your AndroidManifest.xml
.
<uses-feature
android:name="android.hardware.sensor.stepcounter"
android:required="true"/>
<uses-feature
android:name="android.hardware.sensor.stepdetector"
android:required="true"/>
If you use Android 10 or later, you need to add following permission.
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
And request runtime permission in android.
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ActivityRecognition) != (int)Permission.Granted)
{
RequestPermissions(new string[] { Manifest.Permission.ActivityRecognition }, 0);
}
Then I find you sManager.RegisterListener
lack of sensor. Here is my editted StepCounter.cs
. I test it in my Google pixel, it OnSensorChanged will execute, and could get the steps.
using Android.App;
using Android.Content;
using Android.Hardware;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CustomEntryDemo.Droid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
[ assembly: Dependency(typeof(StepCounter))]
namespace CustomEntryDemo.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();
}
//private static final int sensorTypeD = Sensor.TYPE_STEP_DETECTOR;
// private static final int sensorTypeC = Sensor.TYPE_STEP_COUNTER;
private Sensor mStepCount;
private Sensor mStepDetector;
public void InitSensorService()
{
sManager = Android.App.Application.Context.GetSystemService(Context.SensorService) as SensorManager;
mStepCount = sManager.GetDefaultSensor(SensorType.StepDetector);
mStepDetector = sManager.GetDefaultSensor(SensorType.StepCounter);
sManager.RegisterListener(this, mStepCount, SensorDelay.Fastest);
sManager.RegisterListener(this, mStepDetector, SensorDelay.Fastest);
}
public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
{
Console.WriteLine("OnAccuracyChanged called");
}
public void OnSensorChanged(SensorEvent e)
{
Console.WriteLine(e.ToString());
if (e.Sensor.Type== SensorType.StepCounter) {
//setStepCount(event.values [0]);
}
if (e.Sensor.Type == SensorType.StepDetector) {
if (e.Values[0]==1.0) {
Steps++;
}
}
}
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);
}
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.