Receiving sensor events in worker thread (Android native)

1.5k Views Asked by At

I've try to get the sensor data in Android native layer, and it worked. And now,

I want to receive sensor data in a worker thread. Here is my code, First, the

native function HelloThread is called, and it will create a thread to

register and receive sensor data, but it's fail, the callback get_sensor_events

hasn't been called.

Thanks in advance.

void* threadFunction(void* irrelevant)
{
    int tid = syscall(__NR_gettid);
    LOGI("thread id(thread function) = %d", tid);

    looper = ALooper_forThread();

    while(looper == NULL)
    {
        looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);

        if(looper == NULL)
            continue;
        else
            break;
    }

    LOGI("Finished prepare");

    sensorManager = NULL;
    accSensor = NULL;
    gyroSensor = NULL;
    magSensor = NULL;

    sensor_data = (void*)malloc(1000);

    if(looper == NULL)
        LOGI("Looper is null");

    sensorManager = ASensorManager_getInstance();

    if(sensorManager == NULL)
        LOGI("sensorManager == NULL");

    accSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
    gyroSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_GYROSCOPE);
    magSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_MAGNETIC_FIELD);

    if(accSensor == NULL)
        LOGI("accSensor == NULL");
    if(gyroSensor == NULL)
        LOGI("gyroSensor == NULL");
    if(magSensor == NULL)
        LOGI("magSensor == NULL");

    sensorEventQueue = ASensorManager_createEventQueue(sensorManager, looper, 3, get_sensor_events, sensor_data);

    LOGI("Finished ASensorManager_createEventQueue()");

    if(accSensor)
    {   
        ASensorEventQueue_enableSensor(sensorEventQueue, accSensor);
        ASensorEventQueue_setEventRate(sensorEventQueue, accSensor, 20000);
    }
    if(gyroSensor)
    {
        ASensorEventQueue_enableSensor(sensorEventQueue, gyroSensor);
        ASensorEventQueue_setEventRate(sensorEventQueue, gyroSensor, 20000);
    }
    if(magSensor)
    {
        ASensorEventQueue_enableSensor(sensorEventQueue, magSensor);
        ASensorEventQueue_setEventRate(sensorEventQueue, magSensor, 20000);
    }

    LOGI("Finish enable sensor");

    HasAllocateLooper = true;
}

static int get_sensor_events(int fd, int events, void* data) {
  ASensorEvent event;

  int tid = syscall(__NR_gettid);
  LOGI("thread id(get sensor data) = %d", tid);

  while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) {
        if(event.type == ASENSOR_TYPE_ACCELEROMETER) {
            LOGI("Acc:%f,%f,%f, time = %f", event.acceleration.x, event.acceleration.y, event.acceleration.z, ((double)(event.timestamp-lastAccTime))/1000000000.0);
            lastAccTime = event.timestamp;
        }
        else if(event.type == ASENSOR_TYPE_GYROSCOPE) {
            LOGI("Gyro:%f,%f,%f, time = %f", event.acceleration.x, event.acceleration.y, event.acceleration.z, ((double)(event.timestamp-lastGyroTime))/1000000000.0);
            lastGyroTime = event.timestamp;
        }
        else if(event.type == ASENSOR_TYPE_MAGNETIC_FIELD) {
            LOGI("Mag:%f,%f,%f, time = %f", event.acceleration.x, event.acceleration.y, event.acceleration.z, ((double)(event.timestamp-lastMagTime))/1000000000.0);
            lastMagTime = event.timestamp;
        }
  }
  //should return 1 to continue receiving callbacks, or 0 to unregister
  return 1;
}

JNIEXPORT jint JNICALL Java_asus_ndksample_MainActivity_HelloThread
(JNIEnv * env, jobject obj)
{
    int tid = syscall(__NR_gettid);
    LOGI("thread id(Java thread) = %d", tid);

    int ret = 0;

    pthread_t hThread;
    pthread_create(&hThread, NULL, &threadFunction, (void *)1);

    while(true)
    {
        if(HasAllocateLooper)
        {
            break;
        }
    }
    return ret;
}
2

There are 2 best solutions below

0
On

Add following code to the end of method threadFunction and then have a try, it should work.

while (1) {
    int ret = ALooper_pollOnce(100, NULL, NULL, NULL);
    if (ret == ALOOPER_POLL_WAKE) {
        LOGI("ALooper_pollOnce break");
        break;
    }
}
0
On

you have to pass ALOOPER_POLL_CALLBACK as parameter ident to ASensorManager_createEventQueue()