您的位置:首页 > 移动开发 > Android开发

Android 浅谈Sensor工作流程(二)

2013-01-14 01:04 393 查看
转自http://www.apkbus.com/android-14020-1-1.html

另外还有一些唤醒和设置延迟的动作

java代码:

if (mListeners.size() == 0) {

_sensors_control_wake();

}

if (minDelay >= 0) {

_sensors_control_set_delay(minDelay);

}

复制代码
从上面可以看出来 对于底层而言只要知道上层怎么调用传感的接口就好 所以最关心的还是我标绿的 native 方法 上层的传感流程其实比较简单 就是标准的 service 管理和 notify 流程

java代码:

private static native int _sensors_control_init();

private static native ParcelFileDescriptor _sensors_control_open();

private static native boolean _sensors_control_activate(int sensor, boolean activate)

private static native int _sensors_control_set_delay(int ms);

private static native int _sensors_control_wake();

复制代码
1. manager 部分

  frameworks/base/core/jni/android_hardware_SensorManager.cpp

  先看一眼它的方法注册

java代码:

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", (void*)sensors_data_open },

{"sensors_data_close", "()I", (void*)sensors_data_close },

{"sensors_data_poll", "([F[I[J)I", (void*)sensors_data_poll },

};

复制代码
java代码:

sensors_data_open(JNIEnv *env, jclass clazz, jobject fdo){

jclass FileDescriptor = env->FindClass("java/io/FileDescriptor");

jfieldID offset = env->GetFieldID(FileDescriptor, "descriptor", "I");

int fd = env->GetIntField(fdo, offset);

return sSensorDevice->data_open(sSensorDevice, fd); // doesn't take ownership of fd

}

复制代码
调用到最后其实都是用的 sSensorDevice 的方法

java代码:

static sensors_data_device_t* sSensorDevice = 0;

复制代码
2.service 部分

java代码:

static JNINativeMethod gMethods[] = {

{"_sensors_control_init", "()I", (void*) android_init },

{"_sensors_control_open", "()Landroid/os/ParcelFileDescriptor;", (void*) android_open },

{"_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 },

};

复制代码
然后上面的那些方法我就不一一贴了 给出一个例子 其实这么实现的

java代码:

android_activate(JNIEnv *env, jclass clazz, jint sensor, jboolean activate){

int active = sSensorDevice->activate(sSensorDevice, sensor, activate);

return (active<0) ? false : true;

}

复制代码
所以最后调用的其实都是 sSensorDevice 的方法 其他的几个也是这样 sSensorDevice 是这个

java代码:

static sensors_control_device_t* sSensorDevice = 0;

复制代码
3.继续追 终于到了硬件层了 最后一切的方法其实就在这里了

java代码:

struct sensors_control_device_t {

struct hw_device_t common;

/**

* Returns the fd which will be the parameter to

* sensors_data_device_t::open_data().

* The caller takes ownership of this fd. This is intended to be

* passed cross processes.

*

* @return a fd if successful, < 0 on error

*/

int (*open_data_source)(struct sensors_control_device_t *dev);

/** Activate/deactivate one sensor.

*

* @param handle is the handle of the sensor to change.

* @param enabled set to 1 to enable, or 0 to disable the sensor.

*

* @return 0 on success, negative errno code otherwise

*/

int (*activate)(struct sensors_control_device_t *dev,int handle, int enabled);

/**

* Set the delay between sensor events in ms

*

* @return 0 if successful, < 0 on error

*/

int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);

/**

* Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.

*/

int (*wake)(struct sensors_control_device_t *dev);

};

复制代码
java代码:

struct sensors_data_device_t {

struct hw_device_t common;

/**

* Prepare to read sensor data.

*

* This routine does NOT take ownership of the fd

* and must not close it. Typically this routine would

* use a duplicate of the fd parameter.

*

* @param fd from sensors_control_open.

*

* @return 0 if successful, < 0 on error

*/

int (*data_open)(struct sensors_data_device_t *dev, int fd);

/**

* Caller has completed using the sensor data.

* The caller will not be blocked in sensors_data_poll

* when this routine is called.

*

* @return 0 if successful, < 0 on error

*/

int (*data_close)(struct sensors_data_device_t *dev);

/**

* Return sensor data for one of the enabled sensors.

*

* @return sensor handle for the returned data, 0x7FFFFFFF when

* sensors_control_device_t.wake() is called and -errno on error

*

*/

int (*poll)(struct sensors_data_device_t *dev,

sensors_data_t* data);

};

复制代码
最后一组函数

java代码:

/** convenience API for opening and closing a device */

static inline int sensors_control_open(const struct hw_module_t* module,

struct sensors_control_device_t** device) {

return module->methods->open(module,

SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);

}

static inline int sensors_control_close(struct sensors_control_device_t* device) {

return device->common.close(&device->common);

}

static inline int sensors_data_open(const struct hw_module_t* module,

struct sensors_data_device_t** device) {

return module->methods->open(module,

SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);

}

static inline int sensors_data_close(struct sensors_data_device_t* device) {

return device->common.close(&device->common);

}

复制代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: