HarmonyOS c++中使用SENSOR_TYPE_GYROSCOPE相关问题

在c++中代码如下:

_subscriptionId = OH_Sensor_CreateSubscriptionId();
_subscriberAttr = OH_Sensor_CreateSubscriptionAttribute();
_subscriber = OH_Sensor_CreateSubscriber();
if (_subscriptionId != NULL && _subscriberAttr != NULL && _subscriber != NULL) {
  OH_SensorSubscriptionId_SetType(_subscriptionId, SENSOR_TYPE_GYROSCOPE);
  OH_SensorSubscriptionAttribute_SetSamplingInterval(_subscriberAttr, 500 * 1000 * 1000);
  OH_SensorSubscriber_SetCallback(_subscriber, SensorEventCallback);
  if (OH_Sensor_Subscribe(_subscriptionId, _subscriberAttr, _subscriber) != SENSOR_SUCCESS) {
    ReleaseGyroscope();
  }
}
static void SensorEventCallback(Sensor_Event *callback) {
  Sensor_Type type;
  OH_SensorEvent_GetType(callback, &type);
  DEBUG("Sensor_Event type: %{public}d", type);
  if (type == SENSOR_TYPE_GYROSCOPE) {
    float* data1;
    uint32_t length;
    OH_SensorEvent_GetData(callback, &data1, &length);
    DEBUG("Sensor_Event data: %{public}d  %{public}d %{public}d ", *data1, data1[0], *(data1+2));
  }
}

输出结果是Sensor_Event data: 32869 32869 1734765920。

这与预期不符,完全无法对应相关数据。

相关权限已注册。

HarmonyOS
7天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

参考这个用例改下:

g_user = OH_Sensor_CreateSubscriber();
int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCallbackImpl);
ASSERT_EQ(ret, SENSOR_SUCCESS);

Sensor_SubscriptionId *id = OH_Sensor_CreateSubscriptionId();
ret = OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
ASSERT_EQ(ret, SENSOR_SUCCESS);

Sensor_SubscriptionAttribute *attr = OH_Sensor_CreateSubscriptionAttribute();
ret = OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD);
ASSERT_EQ(ret, SENSOR_SUCCESS);

ret = OH_Sensor_Subscribe(id, attr, g_user);
ASSERT_EQ(ret, SENSOR_SUCCESS);

std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
ret = OH_Sensor_Unsubscribe(id, g_user);
ASSERT_EQ(ret, SENSOR_SUCCESS);
if (id != nullptr) {
  OH_Sensor_DestroySubscriptionId(id);
}
if (attr != nullptr) {
  OH_Sensor_DestroySubscriptionAttribute(attr);
}
if (g_user != nullptr) {
  OH_Sensor_DestroySubscriber(g_user);
  g_user = nullptr;
}

void SensorDataCallbackImpl(Sensor_Event *event)
{
  if (event == nullptr) {
    SEN_HILOGE("event is null");
    return;
  }
  int64_t timestamp = INVALID_VALUE;
  int32_t ret = OH_SensorEvent_GetTimestamp(event, &timestamp);
  ASSERT_EQ(ret, SENSOR_SUCCESS);
  Sensor_Type sensorType;
  ret = OH_SensorEvent_GetType(event, &sensorType);
  ASSERT_EQ(ret, SENSOR_SUCCESS);
  Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE;
  ret = OH_SensorEvent_GetAccuracy(event, &accuracy);
  ASSERT_EQ(ret, SENSOR_SUCCESS);
  float *data = nullptr;
  uint32_t length = 0;
  ret = OH_SensorEvent_GetData(event, &data, &length);
  ASSERT_EQ(ret, SENSOR_SUCCESS);
  SEN_HILOGI("sensorType:%{public}d, dataLen:%{public}d, accuracy:%{public}d", sensorType, length, accuracy);
  for (uint32_t i = 0; i < length; ++i) {
  SEN_HILOGI("data[%{public}d]:%{public}f", i, data[i]);
}
}
分享
微博
QQ
微信
回复
7天前
相关问题
如何在C++项目中使用pthread
2245浏览 • 1回复 待解决
使用C++进行HarmonyOS开发的问题
931浏览 • 2回复 待解决
HarmonyOS C++ VideoDecoder组件使用
222浏览 • 1回复 待解决
c/c++层主动调用ArkTS存在问题
518浏览 • 1回复 待解决
HarmonyOS C++怎么使用HiTraceChain工具
79浏览 • 1回复 待解决
HarmonyOS 使用C++开发相机崩溃
114浏览 • 1回复 待解决
HarmonyOS C++开发
88浏览 • 1回复 待解决
如何修改C++版本?C++支持情况?
1243浏览 • 1回复 待解决
HarmonyOS 媒体文件 C++ 访问的问题
117浏览 • 1回复 待解决
HarmonyOS 项目使用c++库导出函数报错
418浏览 • 1回复 待解决
C++源码如何编译到HarmonyOS使用
578浏览 • 1回复 待解决
HarmonyOS C++使用aki库,编译报错
419浏览 • 1回复 待解决
HarmonyOS ArkTS与C/C++的交互
858浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人