Android获取这颗Sensor对象

Android获取这颗Sensor对象

本文基于aosp/android11-release -- 清华大学开源软件镜像站AOSP

1.应用获取这颗Sensor

可以查看SensorService数据传递给APK

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

通过SensorManager的继承类SystemSensorManager获取这颗sensor,这里我们只关注的这个sensor对象,不关注SensorEventListener监听。获取SystemSensorManager初始化对象mFullSensorsList中的Sensor.TYPE_ACCELEROMETER这颗sensor。

frameworks/base/core/java/android/hardware/SensorManager.java
frameworks/base/core/java/android/hardware/SystemSensorManager.java
frameworks/base/core/jni/android_hardware_SensorManager.cpp

在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

2.通过JNI实际获取

通过JNI中nativeGetSensorAtIndex,最终在SensorService.cpp中SensorService::getSensorList获取SensorList,即mSensors.getUserSensors(),最终获取在SensorList.cpp的e.si->getSensor()获取。

frameworks/base/core/jni/android_hardware_SensorManager.cpp
frameworks/native/libs/sensor/SensorManager.cpp
frameworks/native/libs/sensor/include/sensor/SensorManager.h
frameworks/native/libs/sensor/ISensorServer.cpp
frameworks/native/services/sensorservice/SensorService.cpp
frameworks/native/services/sensorservice/SensorList.cpp

在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

3. SensorService中mSensors

mSensors注册SensorService::registerSensor这个sensor时添加。SensorService 创建 onFirstRef 时,会注册相关的 HardwareSensor 和 VirtualSensor。SensorService启动流程请查看:SensorService启动

frameworks/native/services/sensorservice/SensorService.cpp
frameworks/native/services/sensorservice/SensorService.h
frameworks/native/services/sensorservice/SensorList.cpp
frameworks/native/services/sensorservice/SensorList.h
SensorServiceUtil::SensorList mSensors;

在这里插入图片描述
在这里插入图片描述

4.这颗sensor对象转换Java中Sensor.java

基本获取脉络已经清楚,那么这个C++层怎么转换成Java的呢?在上面“2.通过JNI实际获取”nativeGetSensorAtIndex方法中调用translateNativeSensorToJavaSensor方法JNI转换

frameworks/core/java/android/hardware/Sensor.java
frameworks/base/core/jni/android_hardware_SensorManager.cpp

在这里插入图片描述在这里插入图片描述

5.Sensor HAL获取Sensor对象

  上面已经获取到Sensor.java对象,但是Sensor数据都是硬件支持上传数据,不管是硬件直接支持的 HardwareSensor,还是虚拟 VirtualSensor。不同的是VirtualSensor通过SensorFusion结合多个HardwareSensor上报的数据,进行 process 计算再上报。更详细可以查看:Android Sensor 概览

5.1 HardwareSensor 获取

SensorService 构建时,onFirstRef 中 registerSensor( new HardwareSensor(list[i]) ) 将 HardwareSensor 添加 mSensors(实质是SensorList.cpp)中。SensorDevice 初始化通过 connectHidlService() 使 mSensors(这个与SensorService不同) 连接硬件设备,并 initializeSensorList 初始化 mSensorList 获取硬件定义sensor_t。这里我们不关注硬件设备的连接和定义,这是特定设备和供应商的事情,属于商业机密。

frameworks/native/services/sensorservice/SensorService.cpp
frameworks/native/services/sensorservice/SensorDevice.cpp

在这里插入图片描述在这里插入图片描述在这里插入图片描述

5.1.1 查看硬件设备需要的 sensor_t

Android 已经以 HAL 接口的形式(在 hardware/libhardware 中定义为 C 头文件)定义了许多此类接口。

# 常用设备HAL标准接口的头文件
hardware/libhardware/include/hardware
# 常用设备HAL标准接口实现
hardware/libhardware/modules/
hardware/libhardware/include/hardware/sensors.h
struct sensor_t {
    
    

    /* Name of this sensor.
     * All sensors of the same "type" must have a different "name".
     */
    const char*     name;

    /* vendor of the hardware part */
    const char*     vendor;

    /* version of the hardware part + driver. The value of this field
     * must increase when the driver is updated in a way that changes the
     * output of this sensor. This is important for fused sensors when the
     * fusion algorithm is updated.
     */
    int             version;

    /* handle that identifies this sensors. This handle is used to reference
     * this sensor throughout the HAL API.
     */
    int             handle;

    /* this sensor's type. */
    int             type;

    /* maximum range of this sensor's value in SI units */
    float           maxRange;

    /* smallest difference between two values reported by this sensor */
    float           resolution;

    /* rough estimate of this sensor's power consumption in mA */
    float           power;

    /* this value depends on the reporting mode:
     *
     *   continuous: minimum sample period allowed in microseconds
     *   on-change : 0
     *   one-shot  :-1
     *   special   : 0, unless otherwise noted
     */
    int32_t         minDelay;

    /* number of events reserved for this sensor in the batch mode FIFO.
     * If there is a dedicated FIFO for this sensor, then this is the
     * size of this FIFO. If the FIFO is shared with other sensors,
     * this is the size reserved for that sensor and it can be zero.
     */
    uint32_t        fifoReservedEventCount;

    /* maximum number of events of this sensor that could be batched.
     * This is especially relevant when the FIFO is shared between
     * several sensors; this value is then set to the size of that FIFO.
     */
    uint32_t        fifoMaxEventCount;

    /* type of this sensor as a string.
     *
     * If type is OEM specific or sensor manufacturer specific type
     * (>=SENSOR_TYPE_DEVICE_PRIVATE_BASE), this string must be defined with reserved domain of
     * vendor/OEM as a prefix, e.g. com.google.glass.onheaddetector
     *
     * For sensors of Android defined types, Android framework will override this value. It is ok to
     * leave it pointing to an empty string.
     */
    const char*    stringType;

    /* permission required to see this sensor, register to it and receive data.
     * Set to "" if no permission is required. Some sensor types like the
     * heart rate monitor have a mandatory require_permission.
     * For sensors that always require a specific permission, like the heart
     * rate monitor, the android framework might overwrite this string
     * automatically.
     */
    const char*    requiredPermission;

    /* This value is defined only for continuous mode and on-change sensors. It is the delay between
     * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
     * frequencies are requested through batch()/setDelay() the events will be generated at this
     * frequency instead. It can be used by the framework or applications to estimate when the batch
     * FIFO may be full.
     *
     * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
     *              continuous, on-change: maximum sampling period allowed in microseconds.
     *              one-shot, special : 0
     *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
     *      on 64 bit architectures only for binary compatibility reasons.
     * Availability: SENSORS_DEVICE_API_VERSION_1_3
     */
    #ifdef __LP64__
       int64_t maxDelay;
    #else
       int32_t maxDelay;
    #endif

    /* Flags for sensor. See SENSOR_FLAG_* above. Only the least significant 32 bits are used here.
     * It is declared as 64 bit on 64 bit architectures only for binary compatibility reasons.
     * Availability: SENSORS_DEVICE_API_VERSION_1_3
     */
    #ifdef __LP64__
       uint64_t flags;
    #else
       uint32_t flags;
    #endif

    /* reserved fields, must be zero */
    void*           reserved[2];
};

5.1.1 这颗sensor对象转换C++中Sensor.cpp

SensorService的 onFirstRef 中SensorService获取出来的任然是sensor_t,registerSensor时初始化mSensor

frameworks/native/services/sensorservice/SensorInterface.h
frameworks/native/services/sensorservice/SensorInterface.cpp
frameworks/native/libs/sensor/include/sensor/Sensor.h
frameworks/native/libs/sensor/Sensor.cpp

在这里插入图片描述在这里插入图片描述

5.2 VirtualSensor 获取

与上面”5.1 HardwareSensor 获取“一样,不同的是多了一个SensorFusion成员,并需要定义sensor_t,如RotationVectorSensor
在这里插入图片描述

frameworks/native/services/sensorservice/RotationVectorSensor.cpp
frameworks/native/services/sensorservice/RotationVectorSensor.h

在这里插入图片描述

小结

frameworks/core/java/android/hardware/Sensor.java new一个对象给到JNI
frameworks/base/core/jni/android_hardware_SensorManager.cpp 通过JNI连通
frameworks/native/libs/sensor/Sensor.cpp 初始化是与 sensor_t 对应
hardware/libhardware/include/hardware/sensors.h 中结构体sensor_t

猜你喜欢

转载自blog.csdn.net/qq_23452385/article/details/109692705
今日推荐