APP获取Sensor对象-Android12
Posted xhBruce
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了APP获取Sensor对象-Android12相关的知识,希望对你有一定的参考价值。
APP获取Sensor对象-android12
android12-release
1. getSystemService(SENSOR_SERVICE)获取服务
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
- 查看图片Context关联类,
Activity -> ContextImpl -> SystemServiceRegistry
frameworks/base/core/java/android/app/ContextImpl.java
@Override
public Object getSystemService(String name)
// ... ...
return SystemServiceRegistry.getSystemService(this, name);
- 获取SensorManager子类
SystemSensorManager
registerService(Context.SENSOR_SERVICE, SensorManager.class,
new CachedServiceFetcher<SensorManager>()
@Override
public SensorManager createService(ContextImpl ctx)
return new SystemSensorManager(ctx.getOuterContext(),
ctx.mMainThread.getHandler().getLooper());
);
2. getDefaultSensor(Sensor.TYPE_ACCELEROMETER) 获取Sensor对象
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- 最终在
mFullSensorsList
中找到对应Sensor
frameworks/base/core/java/android/hardware/SensorManager.java
public Sensor getDefaultSensor(int type)
// TODO: need to be smarter, for now, just return the 1st sensor
List<Sensor> l = getSensorList(type);
boolean wakeUpSensor = false;
// ... ...
public List<Sensor> getSensorList(int type)
// cache the returned lists the first time
List<Sensor> list;
final List<Sensor> fullList = getFullSensorList();
synchronized (mSensorListByType)
list = mSensorListByType.get(type);
if (list == null)
if (type == Sensor.TYPE_ALL)
list = fullList;
else
list = new ArrayList<Sensor>();
for (Sensor i : fullList)
if (i.getType() == type)
list.add(i);
list = Collections.unmodifiableList(list);
mSensorListByType.append(type, list);
return list;
frameworks/base/core/java/android/hardware/SystemSensorManager.java
protected List<Sensor> getFullSensorList()
return mFullSensorsList;
mFullSensorsList数据
nativeGetSensorAtIndex
JNI获取SensorManager.cpp中数据mSensorList
frameworks/base/core/java/android/hardware/SystemSensorManager.java
public SystemSensorManager(Context context, Looper mainLooper)
// ... ...
// initialize the sensor list
for (int index = 0;; ++index)
Sensor sensor = new Sensor();
if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
mFullSensorsList.add(sensor);
mHandleToSensor.put(sensor.getHandle(), sensor);
frameworks/base/core/jni/android_hardware_SensorManager.cpp
static jboolean
nativeGetSensorAtIndex(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensor, jint index)
SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager);
Sensor const* const* sensorList;
ssize_t count = mgr->getSensorList(&sensorList);
if (ssize_t(index) >= count)
return false;
return translateNativeSensorToJavaSensor(env, sensor, *sensorList[index]) != NULL;
mSensorServer->getSensorList()
Binder调用,从SensorService.cpp中SensorList mSensors
获取
frameworks/native/libs/sensor/SensorManager.cpp
status_t SensorManager::assertStateLocked()
// ... ...
if (initSensorManager)
waitForSensorService(&mSensorServer);
LOG_ALWAYS_FATAL_IF(mSensorServer == nullptr, "getService(SensorService) NULL");
// ... ...
mSensors = mSensorServer->getSensorList(mOpPackageName);
// ... ...
for (size_t i=0 ; i<count ; i++)
mSensorList[i] = mSensors.array() + i;
return NO_ERROR;
ssize_t SensorManager::getSensorList(Sensor const* const** list)
Mutex::Autolock _l(mLock);
status_t err = assertStateLocked();
if (err < 0)
return static_cast<ssize_t>(err);
*list = mSensorList;
return static_cast<ssize_t>(mSensors.size());
frameworks/native/services/sensorservice/SensorService.cpp
Vector<Sensor> SensorService::getSensorList(const String16& opPackageName)
char value[PROPERTY_VALUE_MAX];
property_get("debug.sensors", value, "0");
const Vector<Sensor>& initialSensorList = (atoi(value)) ?
mSensors.getUserDebugSensors() : mSensors.getUserSensors();
// ... ...
SensorList mSensors
SensorService启动-Android12 过程 registerSensor() 注册时添加,包含HardwareSensor、VirtualSensor
const Sensor& SensorService::registerSensor(SensorInterface* s, bool isDebug, bool isVirtual)
int handle = s->getSensor().getHandle();
int type = s->getSensor().getType();
if (mSensors.add(handle, s, isDebug, isVirtual))
mRecentEvent.emplace(handle, new SensorServiceUtil::RecentEventLogger(type));
return s->getSensor();
else
return mSensors.getNonSensor();
frameworks/native/services/sensorservice/SensorList.cpp
bool SensorList::add(
int handle, SensorInterface* si, bool isForDebug, bool isVirtual)
std::lock_guard<std::mutex> lk(mLock);
if (handle == si->getSensor().getHandle() &&
mUsedHandle.insert(handle).second)
// will succeed as the mUsedHandle does not have this handle
mHandleMap.emplace(handle, Entry(si, isForDebug, isVirtual));
return true;
// handle exist already or handle mismatch
return false;
3. HardwareSensor相关
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
Sensor::Sensor(struct sensor_t const& hwSensor, const uuid_t& uuid, int halVersion) :
Sensor("")
mName = hwSensor.name;
mVendor = hwSensor.vendor;
mVersion = hwSensor.version;
mHandle = hwSensor.handle;
mType = hwSensor.type;
mMinValue = 0; // FIXME: minValue
mMaxValue = hwSensor.maxRange; // FIXME: maxValue
mResolution = hwSensor.resolution;
mPower = hwSensor.power;
mMinDelay = hwSensor.minDelay;
mFlags = 0;
mUuid = uuid;
// ... ...
3.1 HAL 硬件接口 sensor_t
# 常用设备HAL标准接口的头文件
hardware/libhardware/include/hardware
# 常用设备HAL标准接口实现
hardware/libhardware/modules/
hardware/libhardware/include/hardware/sensors.h
struct sensor_t
// ... ...
/* this sensor's type. */
int type;
// ... ...
;
4. VirtualSensor相关
frameworks/native/services/sensorservice/SensorInterface.h
frameworks/native/services/sensorservice/SensorInterface.cpp
frameworks/native/services/sensorservice/OrientationSensor.h
frameworks/native/services/sensorservice/OrientationSensor.cpp
例如SENSOR_TYPE_ORIENTATION
OrientationSensor::OrientationSensor()
const sensor_t sensor =
.name = "Orientation Sensor",
.vendor = "AOSP",
.version = 1,
.handle = '_ypr',
.type = SENSOR_TYPE_ORIENTATION,
.maxRange = 360.0f,
.resolution = 1.0f/256.0f, // FIXME: real value here
.power = mSensorFusion.getPowerUsage(),
.minDelay = mSensorFusion.getMinDelay(),
;
mSensor = Sensor(&sensor);
时序图
以上是关于APP获取Sensor对象-Android12的主要内容,如果未能解决你的问题,请参考以下文章
获取Android设备的方向,Sensor和SensorManager实现手机旋转角度