我正在进行一个项目,希望简要地对Android传感器进行采样。这里,简短地说,大约是200到400毫秒(从字面上讲,大约是眨眼)。
我想避免Android的异步性,使用ALooper_pollOnce
或ALooper_pollAll
。避免它简化了编程和审计,因为我可以使用梯形图而不是状态机来对系统进行建模。时间如此之短,在实践中不应该成为一个问题。
以下代码:
#include "android_native_app_glue.h"
...
// Should be defined in app_glue
#ifndef LOOPER_ID_USER
# define LOOPER_ID_USER 3
#endif
...
static const int LOOPER_ID_PRNG = LOOPER_ID_USER + 1;
...
ALooper* looper = ALooper_forThread();
if (looper == NULL)
looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
...
ASensorEventQueue* queue = ASensorManager_createEventQueue(sensorManager,
looper, LOOPER_ID_PRNG, NULL /*SensorEvent*/,
reinterpret_cast<void*>(&context));
在logcat中导致以下错误:
09-03 01:30:00.100: E/Looper(4026): Invalid attempt to set NULL callback but not
allowed for this looper.
如何创建允许轮询的环套?或者,如果活套存在,我如何修改它以允许轮询?
struct Sensor {
Sensor() :
m_type(-1), m_sensor(NULL) {
}
explicit Sensor(int type, string name, const ASensor* sensor) :
m_type(type), m_name(name), m_sensor(sensor) {
}
int m_type;
string m_name;
const ASensor* m_sensor;
};
typedef vector<Sensor> SensorList;
和:
static SensorList s_list;
// Double checked initialization omitted
ASensorList sensorList;
ASensorManager* sensorManager = ASensorManager_getInstance();
int n = ASensorManager_getSensorList(sensorManager, &sensorList);
if( n < 0)
{
LOG_ERROR("SensorList: failed to retrieve list");
}
else if (n == 0)
{
LOG_DEBUG("SensorList: no sensors available");
}
else
{
s_list.reserve(static_cast<size_t>(n));
for (int i = 0; i < n; i++)
{
const ASensor* sensor = sensorList[i];
if (sensor == NULL)
continue;
const char* name = ASensor_getName(sensor);
const char* vendor = ASensor_getVendor(sensor);
int type = ASensor_getType(sensor);
int min_delay = ASensor_getMinDelay(sensor);
float resolution = ASensor_getResolution(sensor);
LOG_DEBUG("SensorList: %s (%s) %d %d %f", name, vendor, type, min_delay, resolution);
// These sensors require callbacks. Avoid them for simplicity.
if (type == ASENSOR_TYPE_LIGHT || min_delay == 0)
continue;
s_list.push_back(Sensor(type, name, sensor));
}
}
下面,光传感器需要回调,因为它的min_delay
为0。它没有添加到SensorList
中。将其他传感器添加到CCD_ 5中。
输出由以下行创建:
LOG_DEBUG("SensorList: %s (%s) %d %d %f", name, vendor, type, min_delay, resolution);
D/PRNG ( 3950): SensorList: MPL rotation vector (Invensense) 11 20000 1.000000
D/PRNG ( 3950): SensorList: MPL linear accel (Invensense) 10 20000 1.000000
D/PRNG ( 3950): SensorList: MPL gravity (Invensense) 9 20000 1.000000
D/PRNG ( 3950): SensorList: MPL Gyro (Invensense) 4 20000 1.000000
D/PRNG ( 3950): SensorList: MPL accel (Invensense) 1 20000 1.000000
D/PRNG ( 3950): SensorList: MPL magnetic field (Invensense) 2 20000 1.000000
D/PRNG ( 3950): SensorList: MPL Orientation (Invensense) 3 20000 1.000000
D/PRNG ( 3950): SensorList: Lite-On al3000a Ambient Light Sensor (Lite-On) 5 0 1.000000
D/PRNG ( 3950): SensorList: added 7 sensors
本部分
ALooper* looper = ALooper_forThread();
if (looper == NULL)
looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
可以简化为
ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
根据looper.h
中的文档,由于ALooper_prepare
返回与线程关联的活套,如果它存在的话。这甚至可能解决您的问题b/c它可能会更改活套,使其允许非回调,但我不确定。
尽管要注意,你应该自己完全控制looper,也就是说,如果一个looper已经在其他地方进行了轮询,我认为你自己无法在不干扰的情况下轮询同一个loover。因此,要么在android_native_app_glue
中修改活套,要么在新线程中编写新的活套。这是我的理解。