I am currently developing a Application to track Steps.
In order to track steps i am using the following Sensor of the phone: Sensor.TYPE_STEP_COUNTER
This worked fine for all devices I tested it with. Recently I had the chance to test the application on Samsung S10 device, but it did not track the steps anymore whereas it worked on Android 9 of the Samsung S9. It also works fine on a Google Nexus running Android 6.
I found the following warning when running the application:
Some Additional Debug info:
Debug Infos are somewhat mixed as I tested both the Step Counter Sensor and the Step Detector Sensor both yield the same result.
My Code to register the Sensor:
private SensorEventListener sensorEventListener = new SensorEventListener() {
/**
* This Method gets called on each Sensor Trigger event
*
* @param sensorEvent Event created by Sensor
*/
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
//ME COUNTING STEPS
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//Nothing to do here
}
};
private SensorManager sensorManager;
/**
* Function that initialises all the Sensors
* Sets SensorManager
* Sets Sensor to monitor to STEP Counter
* Also Registers the Sensor
*/
private void initSensors() {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
assert sensorManager != null;
Sensor stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (stepSensor == null) {
createToastMessage("Sensor not found.");
selectedFragment = new NoSensor_Fragment();
activeFragment = 5;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
} else {
sensorManager.registerListener(sensorEventListener, stepSensor,
SensorManager.SENSOR_DELAY_NORMAL);
hasSensor = true;
}
}
I found a solution for this issue
In my case it was simple mistake with Permissions. On Android 10 you need to ask for Permissions to gain Access to the Step Sensors. Which is something I overlooked when reading the changes for Android 10. I Added the following to the Manifest.xml
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
And added the following in the onCreate Method:
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
//ask for permission
requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PHYISCAL_ACTIVITY);
}
Hope this helps people facing the same issue as me.
Possible Solution
In my case it was simple mistake with Permissions. On Android 10 you need to ask for Permissions to gain Access to the Step Sensors. Which is something I overlooked when reading the changes for Android 10. I Added the following to the Manifest.xml
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
And added the following in the onCreate Method:
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
//ask for permission
requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PHYISCAL_ACTIVITY);
}
Hope this helps people facing the same issue as me.