I have been using NDK APIs to obtain samples from the magnetometer through a callback function. According to the documentation, that is the header files sensor.h and looper.h, I simply have to create an event queue and pass as an argument the callback function.
/*
* 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);
After creating the queue and enabling the sensor, the callbacks occur and I am able to retrieve samples as events are triggered. So far so good. The problem is that when I call from Java the native function that does the proper initializations and I want to access the retrived samples (consider N samples) from the initialization function. Again, according to the API the callbacks always occur if 1 is returned and 0 is returned to stop the callbacks.
/**
* 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);
The thing is I can’t figure out how these callbacks really work. I mean, after initializating the event queue and forth, the callbacks occur right way and it seems that there is no way to access the retrieved samples from outside of the callback function after returning 0. In fact, the callback function is apparently looping until 0 is returned. Is there anyway to do this? How does this callback function really work?
This was a silly question from me. Declaring a static array, global or not, to save data did, obviously, the trick. Then, just pass the array to Java through JNI inside the callback function after collecting N samples.