传感器记录器时间段不遵循查询设置

Sensor recorder time period doesn't follow query settings

我正在尝试在 Tizen 可穿戴设备 5.5 上使用计步器。我可以让传感器侦听器工作并 return 数据。但是数据汇总到我找不到相关文档的内容。 然后我发现我需要自己计算步骤(并在 Tizen 论坛和 GitHub 中找到了一些示例)但是我在使用记录器时得到的行为很奇怪。

我 运行 这是在我的 Samsung Watch 上,而不是在模拟器上。

这是我的代码:

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);
  }
}

编辑:我已经用正确的值修复了上面的代码,以便从 0.00 到当前时间进行查询。

那么回调方式为:

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;
}

现在有了记录数据和传感器数据,我找不到如何计算当天的步数。有文档可以解释吗?

分享更多使用此代码的结果(例如您的日志)会更有帮助。

这个API好像是为了长期记录而设计的。你参考过这份文件吗? 简而言之,这似乎是一种请求传感器的长期记录然后稍后查询以获取数据的方法。

你最好使用 SHealth SDK 获取 Galaxy 手表上的步数历史记录 参见: