在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。
这与预期不符,完全无法对应相关数据。
相关权限已注册。
参考这个用例改下: