使用Android NDK通过回调进行磁力计采样

时间:2012-12-05 15:18:14

标签: android-ndk

我一直在使用NDK API通过回调函数从磁力计​​中获取样本。根据文档,即头文件sensor.hlooper.h,我只需创建一个事件队列并将回调函数作为参数传递。


    /*
     * Creates a new sensor event queue and associate it with a looper.
     */
    ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
            ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);

创建队列并启用传感器后,回调发生,我可以在触发事件时检索样本。到现在为止还挺好。问题是,当我从Java调用本机函数时,它执行正确的初始化,并且我想从初始化函数访问重新获取的样本(考虑N个样本)。同样,根据API,如果返回1并且返回0以停止回调,则总是会发生回调。


    /**
     * For callback-based event loops, this is the prototype of the function
     * that is called.  It is given the file descriptor it is associated with,
     * a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT),
     * and the data pointer that was originally supplied.
     *
     * Implementations should return 1 to continue receiving callbacks, or 0
     * to have this file descriptor and callback unregistered from the looper.
     */
    typedef int (*ALooper_callbackFunc)(int fd, int events, void* data);

问题是我无法弄清楚这些回调是如何运作的。我的意思是,在初始化事件队列之后,回调以正确的方式发生,似乎在返回0之后无法从回调函数外部访问检索到的样本。实际上,回调函数显然是循环直到0退回。反正有没有这样做?这个回调函数如何真正起作用?

1 个答案:

答案 0 :(得分:0)

这是我的一个愚蠢的问题。声明一个静态数组,无论是全局还是非全局,以保存数据显然是诀窍。然后,在收集N个样本后,只需通过回调函数内的JNI将数组传递给Java。