Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 10 cannot register Sensor.TYPE_STEP_COUNTER

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.

The question that now arises is whether this has to do with my code or if it is an Issue with Android 10?

I found the following warning when running the application:

  • 2020-01-06 17:13:30.381 24261-24261/? D/SensorManager: registerListener fail (1) :: 17, SAMSUNG Step Counter Sensor, 200000, 0,

Some Additional Debug info:

  • Sensor name="SAMSUNG Step Detector Sensor", vendor="Samsung Inc.", version=1, type=18, maxRange=1.0, resolution=1.0, power=0.3, minDelay=0

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.

like image 536
Thierry jegen Avatar asked Jan 04 '20 16:01

Thierry jegen


1 Answers

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.

like image 59
Thierry jegen Avatar answered Nov 20 '22 16:11

Thierry jegen