我一直在使用 NDK API 通过回调函数从磁力计获取样本。根据文档,即头文件sensor.h
和looper.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被退回。有没有办法做到这一点?这个回调函数究竟是如何工作的?