ASensorEventQueue_hasEvents返回-1,ALooper_pollAll在基本用法上返回-4

时间:2018-04-06 22:41:08

标签: android android-ndk accelerometer android-looper

我正在尝试编写一个Android应用程序,它使用Java SDK进行GUI,NDK用于绘制内容并读取线性加速度传感器(基于加速度计,但不包括重力)。基本上,我想知道设备何时被动摇并且我的应用程序处于活动状态。

我认为,我以相当基本的方式使用ASensorEventQueue_hasEvents和ALooper_pollAll,但它们分别返回-1和-4。这些都是错误,但我不知道它们的意思或我可能做错了什么。欢迎任何帮助,谢谢!以下是一些代码剪辑:

为了开始收听线性加速度传感器的数据,我做了:

//sensorManager = ASensorManager_getInstance();
sensorManager = ASensorManager_getInstanceForPackage(mypackage);
sensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_LINEAR_ACCELERATION);
if (sensor == nullptr) {
    return env->NewStringUTF("Linear accelerometer is not present");
}

looper = ALooper_prepare(0);
if (looper == nullptr) {
    return env->NewStringUTF("Could not get looper.");
}

eventQueue = ASensorManager_createEventQueue(sensorManager, looper, EVENT_TYPE_LINEAR_ACCELEROMETER, nullptr, nullptr);
if (eventQueue == nullptr) {
    return env->NewStringUTF("Could not create event queue for sensor");
}

//ASensorEventQueue_disableSensor(eventQueue, sensor);
int minDelay = ASensor_getMinDelay(sensor);
minDelay = std::max(minDelay, MAX_EVENT_REPORT_TIME);

int rc = ASensorEventQueue_registerSensor(eventQueue, sensor, minDelay, minDelay);
rc = ASensorEventQueue_setEventRate(eventQueue, sensor, minDelay);
return env->NewStringUTF("");

我的绘图循环如下:

    int rc = ASensorEventQueue_enableSensor(eventQueue, sensor);
    std::vector<std::string> results;
    while (!stopDrawing) {
        int event = 0;
        void *data = nullptr;
        int rc;
        rc = ASensorEventQueue_hasEvents(eventQueue); // testing
        rc = ALooper_pollAll(wait_timeout, nullptr, &event, &data);
        if (rc == ALOOPER_POLL_TIMEOUT) {
            waiting += wait_timeout;

            if (waiting > EVENT_LOOP_TIMEOUT) {
                // If no data has been received, we have been waiting for a while, so assume
                // the shaking has stopped
                ASensorEventQueue_disableSensor(eventQueue, sensor);
            }
        } else if (rc == ALOOPER_POLL_ERROR) {
             return env->NewStringUTF("A looper error occurred");
        } else if (rc >= 0 && event == EVENT_TYPE_LINEAR_ACCELEROMETER) {
            std::vector<ASensorEvent> events;
            events.resize(100);
            while (ASensorEventQueue_hasEvents(eventQueue) == 1) {
                ssize_t nbrEvents = ASensorEventQueue_getEvents(eventQueue, events.data(), events.size());
                if (nbrEvents < 0) {
                    // an error has occurred
                    return env->NewStringUTF("");
                }

                float x = 0;
                float y = 0;
                float z = 0;
                for (int i = 0; i < nbrEvents; i++) {
                    x += events[i].acceleration.x;
                    y += events[i].acceleration.y;
                    z += events[i].acceleration.z;
                }

                graphics.updateAcceleration(x, y, z);
            }
        }
        graphics.updateUniformBuffer();
        graphics.drawFrame();
    }

注意:在ALooper_pollAll之前调用ASensorEventQueue_hasEvents只是为了让我可以看到它返回的内容,因为ALooper_pollAll调用总是返回错误。在调试器中运行它时,我看到hasEvents返回-1并且pollAll返回-4。这在SDK版本24和26中发生(我没有尝试过27并且24以下不支持vulkan)。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我有这里描述的确切症状,对我来说,事实证明,我正在与轮询不同的线程上准备弯针。