Android 浅谈Sensor工作流程(二)

来源:互联网 发布:客所思kx2a mac 编辑:程序博客网 时间:2024/05/16 10:09

转自http://www.apkbus.com/android-14020-1-1.html
另外还有一些唤醒和设置延迟的动作

java代码:
  1. if (mListeners.size() == 0) {
  2. _sensors_control_wake();
  3. }

  4. if (minDelay >= 0) {
  5. _sensors_control_set_delay(minDelay);
  6. }
复制代码
   从上面可以看出来 对于底层而言只要知道上层怎么调用传感的接口就好 所以最关心的还是我标绿的 native 方法 上层的传感流程其实比较简单 就是标准的 service 管理和 notify 流程

java代码:
  1. private static native int _sensors_control_init();
  2. private static native ParcelFileDescriptor _sensors_control_open();
  3. private static native boolean _sensors_control_activate(int sensor, boolean activate)
  4. private static native int _sensors_control_set_delay(int ms);
  5. private static native int _sensors_control_wake();
复制代码
  1. manager 部分

  frameworks/base/core/jni/android_hardware_SensorManager.cpp
  先看一眼它的方法注册

java代码:
  1. static JNINativeMethod gMethods[] = {
  2. {"nativeClassInit", "()V", (void*)nativeClassInit },
  3. {"sensors_module_init","()I", (void*)sensors_module_init },
  4. {"sensors_module_get_next_sensor","(Landroid/hardware/Sensor;I)I",(void*)sensors_module_get_next_sensor },

  5. {"sensors_data_init", "()I", (void*)sensors_data_init },
  6. {"sensors_data_uninit", "()I", (void*)sensors_data_uninit },
  7. {"sensors_data_open", "(Ljava/io/FileDescriptor;)I", (void*)sensors_data_open },
  8. {"sensors_data_close", "()I", (void*)sensors_data_close },
  9. {"sensors_data_poll", "([F[I[J)I", (void*)sensors_data_poll },

  10. };
复制代码
java代码:
  1. sensors_data_open(JNIEnv *env, jclass clazz, jobject fdo){

  2. jclass FileDescriptor = env->FindClass("java/io/FileDescriptor");
  3. jfieldID offset = env->GetFieldID(FileDescriptor, "descriptor", "I");
  4. int fd = env->GetIntField(fdo, offset);
  5. return sSensorDevice->data_open(sSensorDevice, fd); // doesn't take ownership of fd
  6. }
复制代码
      调用到最后其实都是用的 sSensorDevice 的方法

java代码:
  1. static sensors_data_device_t* sSensorDevice = 0;
复制代码
  2.service 部分

java代码:
  1. static JNINativeMethod gMethods[] = {
  2. {"_sensors_control_init", "()I", (void*) android_init },
  3. {"_sensors_control_open", "()Landroid/os/ParcelFileDescriptor;", (void*) android_open },
  4. {"_sensors_control_activate", "(IZ)Z", (void*) android_activate },
  5. {"_sensors_control_wake", "()I", (void*) android_data_wake },
  6. {"_sensors_control_set_delay","(I)I", (void*) android_set_delay },
  7. };
复制代码
    然后上面的那些方法我就不一一贴了 给出一个例子 其实这么实现的

java代码:
  1. android_activate(JNIEnv *env, jclass clazz, jint sensor, jboolean activate){
  2. int active = sSensorDevice->activate(sSensorDevice, sensor, activate);
  3. return (active<0) ? false : true;
  4. }
复制代码
    所以最后调用的其实都是 sSensorDevice 的方法 其他的几个也是这样 sSensorDevice 是这个

java代码:
  1. static sensors_control_device_t* sSensorDevice = 0;
复制代码
   3.继续追 终于到了硬件层了 最后一切的方法其实就在这里了

java代码:
  1. struct sensors_control_device_t {
  2. struct hw_device_t common;

  3. /**
  4. * Returns the fd which will be the parameter to
  5. * sensors_data_device_t::open_data().
  6. * The caller takes ownership of this fd. This is intended to be
  7. * passed cross processes.
  8. *
  9. * @return a fd if successful, < 0 on error
  10. */

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

  12. /** Activate/deactivate one sensor.
  13. *
  14. * @param handle is the handle of the sensor to change.
  15. * @param enabled set to 1 to enable, or 0 to disable the sensor.
  16. *
  17. * @return 0 on success, negative errno code otherwise
  18. */

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

  20. /**
  21. * Set the delay between sensor events in ms
  22. *
  23. * @return 0 if successful, < 0 on error
  24. */

  25. int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);
  26. /**
  27. * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.
  28. */
  29. int (*wake)(struct sensors_control_device_t *dev);

  30. };
复制代码
java代码:
  1. struct sensors_data_device_t {
  2. struct hw_device_t common;

  3. /**
  4. * Prepare to read sensor data.
  5. *
  6. * This routine does NOT take ownership of the fd
  7. * and must not close it. Typically this routine would
  8. * use a duplicate of the fd parameter.
  9. *
  10. * @param fd from sensors_control_open.
  11. *
  12. * @return 0 if successful, < 0 on error
  13. */

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

  15. /**
  16. * Caller has completed using the sensor data.
  17. * The caller will not be blocked in sensors_data_poll
  18. * when this routine is called.
  19. *
  20. * @return 0 if successful, < 0 on error
  21. */

  22. int (*data_close)(struct sensors_data_device_t *dev);
  23. /**
  24. * Return sensor data for one of the enabled sensors.
  25. *
  26. * @return sensor handle for the returned data, 0x7FFFFFFF when
  27. * sensors_control_device_t.wake() is called and -errno on error
  28. *
  29. */

  30. int (*poll)(struct sensors_data_device_t *dev,
  31. sensors_data_t* data);
  32. };
复制代码
  最后一组函数

java代码:
  1. /** convenience API for opening and closing a device */

  2. static inline int sensors_control_open(const struct hw_module_t* module,
  3. struct sensors_control_device_t** device) {
  4. return module->methods->open(module,
  5. SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
  6. }


  7. static inline int sensors_control_close(struct sensors_control_device_t* device) {
  8. return device->common.close(&device->common);
  9. }

  10. static inline int sensors_data_open(const struct hw_module_t* module,
  11. struct sensors_data_device_t** device) {

  12. return module->methods->open(module,
  13. SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
  14. }

  15. static inline int sensors_data_close(struct sensors_data_device_t* device) {
  16. return device->common.close(&device->common);
  17. }
复制代码

原创粉丝点击