0

I am trying to use the PEDOMETER on Tizen wearable 5.5. I can get the sensor listener to work and return the data. But the data is aggregated to something I can't find documentation about. So then I found that I need to do the calculation of steps myself (and found a few samples in the Tizen forum and GitHub) but the behavior I am getting when using the recorder is quite weird.

I am running this on my Samsung Watch, not in the emulator.

Here is my code:

void get_initial_pedometer_data() {

  // Check the sensor recorder is supported
  bool recorderSupported;
  sensor_recorder_is_supported(SENSOR_HUMAN_PEDOMETER, &recorderSupported);
  if (!recorderSupported) {
    dlog_print(DLOG_WARN, LOG_TAG, "Recorder not supported.");
    return;
  }

  // Create the sensor recorder and query
  sensor_recorder_create_option(&option);
  sensor_recorder_option_set_int(option, SENSOR_RECORDER_OPTION_RETENTION_PERIOD, 24);
  sensor_recorder_start(SENSOR_HUMAN_PEDOMETER, option);
  sensor_recorder_query_h query;
  if (sensor_recorder_create_query(&query) != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot create query");
    return;
  }

  // Calculate and set the FROM/TO times for the query
  time_t nowTime, startTime;
  nowTime = time(NULL);
  struct tm *tmNow = localtime(&nowTime);
  dlog_print(DLOG_INFO, LOG_TAG, "Query end time [%ld]: %d %dh%dm%ds",
            nowTime, tmNow->tm_mday, tmNow->tm_hour, tmNow->tm_min,
            tmNow->tm_sec);

  startTime = nowTime - (tmNow->tm_hour * 3600) - (tmNow->tm_min * 60)
            - (tmNow->tm_sec);

  struct tm *tmStart = localtime(&startTime);
  dlog_print(DLOG_INFO, LOG_TAG, "Query start time [%ld]: %d %dh%dm%ds",
            startTime, tmStart->tm_mday, tmStart->tm_hour, tmStart->tm_min,
            tmStart->tm_sec);

  if (sensor_recorder_query_set_time(query, SENSOR_RECORDER_QUERY_START_TIME, startTime) != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot set query START option: %ld.", startTime);
    return;
  }
  if (sensor_recorder_query_set_time(query, SENSOR_RECORDER_QUERY_END_TIME, nowTime) != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot set query END option: %ld.", nowTime);
    return;
  }
  if (sensor_recorder_query_set_int(query, SENSOR_RECORDER_QUERY_TIME_INTERVAL, 24 * 60) != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot set query INTERVAL option: %d.", 24 * 60);
    return;
  }

  if (sensor_recorder_query_set_time(query, SENSOR_RECORDER_QUERY_ANCHOR_TIME, startTime) != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot set query ANCHOR option: %ld.", startTime);
    return;
  }

  // Query the data
  int error = sensor_recorder_read_sync(SENSOR_HUMAN_PEDOMETER, query, sensor_pedometer_data_cb, NULL);
  if (error != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot read query: %d", error);
  }
}

EDIT: I have fixed the code above with the correct values to query from 0.00 to current time.

Then the callback method is:

bool sensor_pedometer_data_cb(sensor_type_e type, sensor_recorder_data_h data, int remains, sensor_error_e error, void *user_data) {
  if (type != SENSOR_HUMAN_PEDOMETER) {
    return true;
  }

  if (error != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Sensor record callback error: %d", error);
    return true;
  }

  int step;
  time_t start;
  time_t end;

  // Get the data
  sensor_recorder_data_get_time(data, &start, &end);
  struct tm *tmStart = localtime(&start);

  // Print the START/END time
  dlog_print(DLOG_INFO, LOG_TAG, "Start time: %d %d:%d:%d", tmStart->tm_mday, tmStart->tm_hour, tmStart->tm_min, tmStart->tm_sec);
  struct tm *tmEnd = localtime(&end);
  dlog_print(DLOG_INFO, LOG_TAG, "End time: %d %d:%d:%d", tmEnd->tm_mday, tmEnd->tm_hour, tmEnd->tm_min, tmEnd->tm_sec);

  sensor_recorder_data_get_int(data, SENSOR_RECORDER_DATA_STEPS, &step);
  step_count += step;
  if (remains == 0) {
    total_steps = step_count;
    step_count = 0;
    int error = sensor_listener_start(listener);
    if (error != SENSOR_ERROR_NONE) {
    dlog_print(DLOG_WARN, LOG_TAG, "Cannot start listener. Error: %d", error);
    }
  }
  dlog_print(DLOG_INFO, LOG_TAG, "Step count: %d", total_steps);
  return true;
}

Now with the recorded data and the sensor data, I can't find how to calculate the number of step for the current day. Is there any document that can explain it?

JScoobyCed
  • 10,203
  • 6
  • 34
  • 58

1 Answers1

1

It would be more helpful to share more of the results of using this code(such as your logs).

This API seems to design for long-term records. Did you refer to this document? Briefly, It seems like a way to request a long-term record of the sensor and then query it later to get the data.

and you would better use SHealth SDK to get step count history on the Galaxy watch see: How to get data such as Heart Rate and Pedometer from Samsung Health to Tizen Application?

cocobang
  • 303
  • 2
  • 6
  • I think I was using this document on Tizen docs, but with a fresh new read (didn't get back to this project for 2 months) I see I can try update correctly using the long term data-recording. I'm +1 for now, and will mark the answer as valid if I can solve it that way. – JScoobyCed Apr 20 '21 at 13:48
  • Got it to work using the long-term data recording. I guess I wasn't looking correctly first time. Thank you again! – JScoobyCed Apr 20 '21 at 15:27