androidsdksensorssensormanager

STEP_COUNTER and STEP_DETECTOR works with SDK 28, but not SDK 29?


I've made a service that regularly broadcasts counted steps from both the STEP_COUNTER and STEP_DETECTOR sensor. In one app I made it worked flawlessly, but after copying the exact same code to another project it stopped working. After hours of debugging, I figured out how to solve it. When I change the compileSdkVersion in the build.gradle to 28 it works fine, but changing it to 29 makes it stop working. Why is that?


Solution

  • As per the Privacy changes in Android 10 (which apply to apps that have a targetSdkVersion of 29 or higher):

    Android 10 introduces the android.permission.ACTIVITY_RECOGNITION runtime permission for apps that need to detect the user's step count or classify the user's physical activity, such as walking, biking, or moving in a vehicle. This is designed to give users visibility of how device sensor data is used in Settings.

    The only built-in sensors on the device that require you to declare this permission are the step counter and step detector sensors.

    Therefore you must:

    1. Add the android.permission.ACTIVITY_RECOGNITION to your app's manifest.

    2. Request the permission at runtime and have the user grant that permission.