最近在解关于Gsensor的Bug,Bug还没解掉,反而把Android的Gsensor流程走了一遍。好久不写博客了,不能偷懒啊,学了东西还是得总结一下,好让后来人不要费多少工夫,同时抛砖引玉,大家一起讨论一下,有什么错误我好纠正一下,对自己也是一种激励。言归正传,本文主要从上层的Activity一直分析到kernel的driver,路比较长,不过我喜欢搞清楚架构。
目录:
一、应用层的API;
二、Framwork中的处理;
三、C++中的JNI;
四、Kernel的Gsensor-driver;
一、应用层的API
先看一个例子,这个例子是Gsensor的最简单应用,只是用来打印x,y,z的三个值:
- public
class main extends Activity { -
private float x, y, z; -
protected void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
SensorManager mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE); -
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); -
SensorEventListener lsn = new SensorEventListener() { -
-
public void onSensorChanged(SensorEvent e) { -
System.out.println(e.value[0]); -
System.out.println(e.value[1]); -
System.out.println(e.value[2]); -
} -
public void onAccuracyChanged(Sensor s, int accuracy) { -
} -
}; -
mSensorManager.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME); - }
二、Framework中的处理
三、C++中的JNI
四、Kernel中的驱动
五、SensorService(对比SensorManager)
- static
JNINativeMethod gMethods[] = { -
{"_sensors_control_init", "()I", (void*) android_init }, -
{"_sensors_control_open", "()Landroid/os/Bundle;", (void*) android_open }, -
{"_sensors_control_close", "()I", (void*) android_close }, -
{"_sensors_control_activate", "(IZ)Z", (void*) android_activate }, -
{"_sensors_control_wake", "()I", (void*) android_data_wake }, -
{"_sensors_control_set_delay","(I)I", (void*) android_set_delay }, - };
- static
JNINativeMethod gMethods[] = { -
{"nativeClassInit", "()V", (void*)nativeClassInit }, -
{"sensors_module_init","()I", (void*)sensors_module_init }, -
{"sensors_module_get_next_sensor","(Landroid/hardware/Sensor;I)I", -
(void*)sensors_module_get_next_sensor }, -
{"sensors_data_init", "()I", (void*)sensors_data_init }, -
{"sensors_data_uninit", "()I", (void*)sensors_data_uninit }, -
{"sensors_data_open", "([Ljava/io/FileDescriptor;[I)I", (void*)sensors_data_open }, -
{"sensors_data_close", "()I", (void*)sensors_data_close }, -
{"sensors_data_poll", "([F[I[J)I", (void*)sensors_data_poll }, - };