Sensor开发很容易,因为sensor的值都是底层上报的,不需要上层做其他操作。
1,获取SensorManager,并且调用其getDefaultSensor方法获取对应的sensor。
2,调用SensorManager的registerListener方法进行注册,监听sensor值的变化。
3,继承SensorEventListener接口,并且实现onSensorChanged和onAccuracyChanged方法。
加速度传感器开发如下,
mSensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorMgr.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_NORMAL);
•••
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) { }
@Override
public void onSensorChanged(SensorEvent event) {
mGX = event.values[SensorManager.DATA_X];
mGY = event.values[SensorManager.DATA_Y];
mGZ = event.values[SensorManager.DATA_Z];
•••
}
几乎所有sensor开发流程都一样,只是最后获取SensorEvent中的values 变量不一样而已, values是一个float型的数组。
Values值是SystemSensorManager根据不同的sensor通过JNI机制调用底层C/C++ 方法来赋值的。
从开发过程可以看到,最主要的就是SensorManager的getDefaultSensor和registerListener方法。数据上报的
onAccuracyChanged方法后面再分析。
实际上, Context.SENSOR_SERVICE对应的是SystemSensorManager,只是SystemSensorManager
是SensorManager的子类而已。
SystemServiceRegistry.java对应的代码如下,
registerService(Context.SENSOR_SERVICE, SensorManager.class,
new CachedServiceFetcher() {
@Override
public SensorManager createService(ContextImpl ctx) {
return new SystemSensorManager(ctx.getOuterContext(),
ctx.mMainThread.getHandler().getLooper());
}});
SystemSensorManager的构造方法逻辑如下,
1,JNI初始化
if (!sSensorModuleInitialized) {
sSensorModuleInitialized = true;
nativeClassInit();
}
2,将所有sensor对象保存在mFullSensorsList中。
for (int index = 0;;++index) {
Sensor sensor = new Sensor();
if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
mFullSensorsList.add(sensor);
mHandleToSensor.append(sensor.getHandle(), sensor);
}
SystemSensorManager对应的JNI层为android_hardware_SensorManager.cpp。
nativeClassInit方法主要是进行一些初始化操作,代码如下,
static void
nativeClassInit (JNIEnv *_env, jclass _this)
{
jclass sensorClass = _env->FindClass("android/hardware/Sensor");
SensorOffsets& sensorOffsets = gSensorOffsets;
sensorOffsets.name = _env->GetFieldID(sensorClass, "mName", "Ljava/lang/String;");
sensorOffsets.vendor = _env->GetFieldID(sensorClass, "mVendor", "Ljava/lang/String;");
sensorOffsets.version = _env->GetFieldID(sensorClass, "mVersion", "I");
sensorOffsets.handle = _env->GetFieldID(sensorClass, "mHandle", "I");
sensorOffsets.range = _env->GetFieldID(sensorClass, "mMaxRange", "F");
sensorOffsets.resolution = _env->GetFieldID(sensorClass, "mResolution","F");
sensorOffsets.power = _env->GetFieldID(sensorClass, "mPower", "F");
sensorOffsets.minDelay = _env->GetFieldID(sensorClass, "mMinDelay", "I");
sensorOffsets.fifoReservedEventCount =
_env->GetFieldID(sensorClass, "mFifoReservedEventCount", "I");
sensorOffsets.fifoMaxEventCount = _env->GetFieldID(sensorClass, "mFifoMaxEventCount", "I");
sensorOffsets.stringType = _env->GetFieldID(sensorClass, "mStringType", "Ljava/lang/String;");
sensorOffsets.requiredPermission = _env->GetFieldID(sensorClass, "mRequiredPermission",
"Ljava/lang/String;");
sensorOffsets.maxDelay = _env->GetFieldID(sensorClass, "mMaxDelay", "I");
sensorOffsets.flags = _env->GetFieldID(sensorClass, "mFlags", "I");
sensorOffsets.setType = _env->GetMethodID(sensorClass, "setType", "(I)Z");
}
将SensorOffsets结构体中的变量和Sensor.java中对应的变量一一对应起来,其实Sensor.java中的这些变量
都在在nativeGetSensorAtIndex方法中赋值的。
nativeGetSensorAtIndex方法如下,
SensorManager* mgr = reinterpret_cast(sensorManager);
Sensor const* const* sensorList;
size_t count = mgr->getSensorList(&sensorList);
if (size_t(index) >= count) {
return false;
}
Sensor const* const list = sensorList[index];
const SensorOffsets& sensorOffsets(gSensorOffsets);
jstring name = getInternedString(env, &list->getName());
jstring vendor = getInternedString(env, &list->getVendor());
jstring requiredPermission = getInternedString(env, &list->getRequiredPermission());
env->SetObjectField(sensor, sensorOffsets.name, name);
env->SetObjectField(sensor, sensorOffsets.vendor, vendor);
env->SetIntField(sensor, sensorOffsets.version, list->getVersion());
env->SetIntField(sensor, sensorOffsets.handle, list->getHandle());
env->SetFloatField(sensor, sensorOffsets.range, list->getMaxValue());
•••
首先调用SensorManager.cpp的getSensorList方法将sensor保存在sensorList中,然后为每一个sensor的变量赋值,
也就是Sensor.java中对应的变量赋值。
SensorManager.cpp的getSensorList方法如下,
ssize_t SensorManager::getSensorList(Sensor const* const** list) const
{
Mutex::Autolock _l(mLock);
status_t err = assertStateLocked();
if (err < 0) {
return static_cast(err);
}
*list = mSensorList;
return static_cast(mSensors.size());
}
SensorManager.cpp的mSensorList变量保存了sensor的相关信息,是如何得到的呢?
SensorManager的构造方法中会调用assertStateLocked方法对mSensorList赋值。
该方法主要逻辑如下,
1,首先获取sensorservice服务,
const String16 name("sensorservice");
for (int i = 0; i < 60; i++) {
status_t err = getService(name, &mSensorServer);
•••
2,然后监听服务端进程的状态,
mDeathObserver = new DeathObserver(*const_cast(this));
IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
3,调用服务的getSensorList方法获取sensor列表以及信息。
mSensors = mSensorServer->getSensorList(mOpPackageName);
•••
for (size_t i=0 ; i
经过SystemSensorManager的构造方法, mFullSensorsList就保存了sensor的信息。
SensorManager的getDefaultSensor方法如下,
public Sensor getDefaultSensor(int type) {
// TODO: need to be smarter, for now, just return the 1st sensor
List l = getSensorList(type);
boolean wakeUpSensor = false;
// For the following sensor types, return a wake-up sensor. These types are by default
// defined as wake-up sensors. For the rest of the SDK defined sensor types return a
// non_wake-up version.
if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION ||
type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE ||
type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE ||
type == Sensor.TYPE_WRIST_TILT_GESTURE) {
wakeUpSensor = true;
}
for (Sensor sensor : l) {
if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
}
return null;
}
该方法比较简单,从mFullSensorsList找打对应的sensor,并且是已经激活的sensor。
例如accelerometer 加速度传感器,本文都是以加速度传感器为例,其他的传感器流程几乎完全相同。
获取对应的加速度传感器传感器之后,只有调用registerListener方法注册了才可以获取底层传输过来的数据。
registerListener调用的流程图如下,
registerListenerImpl方法如下,
synchronized (mSensorListeners) {
SensorEventQueue queue = mSensorListeners.get(listener);
if (queue == null) {
Looper looper = (handler != null) ? handler.getLooper() : mMainLooper;
final String fullClassName = listener.getClass().getEnclosingClass() != null ?
listener.getClass().getEnclosingClass().getName() :
listener.getClass().getName();
queue = new SensorEventQueue(listener, looper, this, fullClassName);
if (!queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs)) {
queue.dispose();
return false;
}
mSensorListeners.put(listener, queue);
return true;
} else {
return queue.addSensor(sensor, delayUs, maxBatchReportLatencyUs);
}
}
为每一个SensorListener构造一个SensorEventQueue,然后将对应的sensor(加速度)以及相关信息添加到SensorEventQueue中。
SensorEventQueue是SystemSensorManager的内部类。
当然,不同的sensor是可以共用一个SensorListener的。
SensorEventQueue的构造方法如下,
public SensorEventQueue(SensorEventListener listener, Looper looper,
SystemSensorManager manager, String packageName) {
super(looper, manager, OPERATING_MODE_NORMAL, packageName);
mListener = listener;
}
将注册时的SensorEventListener 变量保存在mListener中,
private final SensorEventListener mListener;
当HAL有数据传输过来时, android_hardware_SensorManager.cpp就会回调dispatchSensorEvent方法,
然后调用注册的onSensorChanged吐出数据。
final int accuracy = mSensorAccuracies.get(handle);
if ((t.accuracy >= 0) && (accuracy != t.accuracy)) {
mSensorAccuracies.put(handle, t.accuracy);
mListener.onAccuracyChanged(t.sensor, t.accuracy);
}
mListener.onSensorChanged(t);
其实,调用registerListener方法后,已经可以从onSensorChanged方法获取数据了,那么数据是怎么从Driver到HAL,
到sensor服务端最后到客户端的呢?