Sensor StepCounter sometime not work well on SamSu

2019-07-11 14:24发布

I am create a demo from receive step from reboot like this.

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager sensorManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);    
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
            Log.i("TAG","step from reboot" + String.valueOf(event.values[0]));
        }
    }
}

But when I test on my device (SamSung Galaxy S4), the STEP_COUNTER sometime not work well :(. I figure out it by compare with SHealth

After many many test I found

Almost time, STEP_COUNTER return the step equals SHealth BUT sometime different. Therefore I think SHealth may use another sensor for counting step but I don't know which sensor? I think it is not STEP_DETECTOR because STEP_DETECTOR return very few step when turn off the screen.

Sometime, STEP_COUNTER stop working while SHealth still return the step, for example, I walk about 100 steps => SHealth display 110 and demo app display 100, then I continue walk about 200 steps => SHealth display 305 and demo app display 121 (seem like it stop work)

I also receive the report from many user with different device (with low rating :( ) but I can only reproduce it on my device.

I don't know how to fix this problem. I think STEP_COUNTER is the best sensor for receive step (compare with STEP_DETECTOR)

Any help or suggestion would be great appreciated.

2条回答
We Are One
2楼-- · 2019-07-11 14:41

Do you unregister the sensor when you leave the app? According to the SDK documentation :

"If you want to continuously track the number of steps over a long period of time, do NOT unregister for this sensor, so that it keeps counting steps in the background even when the AP is in suspend mode and report the aggregate count when the AP is awake".

Hope this will help...

查看更多
贼婆χ
3楼-- · 2019-07-11 14:42

We've also encountered in this problem, when using the default steps sensors of the phone. However, we're using TYPE_STEP_DETECTOR - and we don't have the issue you have(more than 100000 users), so perhaps something is wrong there? This issue is quite resemble in the way that you can't rely on such mechanism. For our case, we've received a lot of steps in a short amount of time. Even if you're a tiger, you can't make that many steps.

Here's the thing, Samsung manipulates the OS and there are a lot of stuff that works well on say, nexus devices, BUT not on Samsung devices. After trying to fix the behaviour of the step sensor, we've figured out we need an alternative for Samsung phones, or, for phones that do not support it.

FYI, We've encountered that some phones (mostly Motorola) doesn't have the steps sensors in the device, so trying to find the sensors, returns null.

  SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  Sensor stepDetector = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
  if (stepsDetector != null) {
     //Do something
  }

You could implement your own mechanism, using wakelock and accelerometer(which has a huge battery consumption, watch out).

查看更多
登录 后发表回答