About using the pedometer sensor (TYPE_STEP_DETECTOR) while sleeping on Android.

Asked 2 years ago, Updated 2 years ago, 70 views

I create a service on Android and count the number of steps using a pedometer sensor (TYPE_STEP_DETECTOR).
When I started the application, it was counted without any problems, but when I was sleeping (the screen was completely dark), the count was sometimes extremely low.
I don't always have a small count when I'm asleep, but I don't really understand the reproduction pattern.

I don't know why it doesn't count, but is there a possibility that the pedometer sensor won't work during sleep?

Thank you for your advice.

public final class PedometerService extensions Service implements SensorEventListener{

  private SensorManager mSensorManager;
  private Sensor;
  private HandlerThread mSensorThread;

  @ Override
  public void onCreate(){
    mSensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
    mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
    mSensorThread = new HandlerThread("PedometerServiceThread");
    mSensorThread.start();
    mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_FAST,
                                    newHandler(mSensorThread.getLooper()));
  }

  @ Override
  public void onDestroy() {
    mSensorThread.getLooper().quit();
    mSensorManager.unregisterListener(this);
  }

  @ Override
  public int onStartCommand(Intent intint, int flags, int startId) {
    return START_STICKY;
  }

  @ Override
  public IBinder onBind (Intent int) {
    return null;
  }

  @ Override
  public void onSensorChanged (SensorEvent) {
    int value = (int) event.values [0];

    // Handling when walking is detected

  }

  @ Override
  public void onAccuracyChanged (Sensor sensor, intaccuracy) {}

}

android

2022-09-30 19:30

3 Answers

Application goes into Doze mode, so it seems that no longer counts.
To put it simply, Doze mode is a feature that stops almost all processing and enters power saving mode if you are not using a terminal to reduce battery consumption.

Doze mode was introduced from Android 6.0.
When I was on Android 6.0, I didn't enter Doze mode when I was walking with my device (the device is not stationary), but from Android 7.0, I started entering Doze mode even if it wasn't stationary.

Please check the official document for instructions on how to keep the app running while in Doze mode.
The Android M/N Doze restrictions and background task execution summary posted on Qitta is very well organized, so please refer to it.

As a countermeasure, Enroll in the whitelist.


2022-09-30 19:30

(It didn't fit in the comment section, so I will use the answer section.)

Mr. Takeru's answer is that if Android is in Sleep state, the sensor will not move.
Your response is that Doze and AppStandby since Android 6.0 have increased the opportunity to sleep.
That's true, but the conditions under which the app can run and the conditions under which the sensor can run are slightly different.

With SensorBatching added from Android 4.4, Android processor (hereinafter referred to as AP) accumulates information on Sensor chip even in Sleep state. When the AP released Sleep, the information that the Sensor had was passed to them all at once.

As a result, even if the application side cannot move in Doze mode, the sensor side accumulates information and
Historically, users can retrieve information when they view the app.

must be supported by a chip on the sensor side.
Older information accumulated prior to the Hardware FIFO limit on the Sensor side disappears.
(As far as I looked at various devices at 4.4 o'clock on Android, it was hardly compatible.I'm sorry I don't have any specific data...)

In short, the behavior of each device has changed depending on the hardware specification, not the software specification of Android.
If it's an application that you make for yourself using the above method, it's better.
I think it will be difficult to support apps that GooglePlay publishes to various devices.

There is a sensor called SignificantSensor(TYPE_SIGNIFICANT_MOTION), although the approach changes.
This is a sensor that can operate when the AP is asleep, and CDD guarantees that it will send a Trigger to the AP.(WakeUp Sensor)
If the app detects Sleep, I think it is possible to attach a SignificantSensor and wake up the AP when you start walking.(Of course, it's not as good as SensorBatching's approach.)
However, there are some devices that do not have SignificantSensor on themselves, so both of them are...


2022-09-30 19:30

Selfless.

I found that certain devices (Galaxy Android 6.0) have reproducibility.
I turned off the screen, and the number of steps I walked became extremely small.

Therefore, instead of Sensor.TYPE_STEP_DETECTOR, I set it to Sensor.TYPE_STEP_COUNTER, and while saving the accumulated number of steps since the terminal was started, I set the difference from the accumulated number of steps I saved last time as the number of steps.
Then, even if the screen is turned off, it is counted in its own way.

Is it better to use TYPE_STEP_COUNTER than TYPE_STEP_DETECTOR?

I'm not sure, but I decided to use TYPE_STEP_COUNTER.


2022-09-30 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.