在工作线程中接收传感器事件(Android原生)

时间:2015-06-11 03:11:19

标签: android multithreading android-ndk sensor

我尝试在Android原生图层中获取传感器数据,但它确实有效。现在,

我想在工作线程中接收传感器数据。这是我的代码,首先是

调用本机函数 HelloThread ,它将创建一个

的线程

注册并接收传感器数据,但它失败了,回调get_sensor_events

没有被召唤。

提前致谢。

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 个答案:

答案 0 :(得分:0)

您必须将ALOOPER_POLL_CALLBACK作为参数ident传递给ASensorManager_createEventQueue()

答案 1 :(得分:0)

将以下代码添加到方法threadFunction的末尾,然后尝试一下,它应该可以正常工作。

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