Android Sensor系统剖析(2.3.5)(上)
本文希望通过对androidsensor系统的介绍,使大家在了解android sensor系统架构的同时,会对大家阅读和分析其他同类型代码框架有所帮助。1:概览
首先看下应用层如何获取sensor数据
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
public SensorActivity() {
//获取对应服务
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
//获取指定sensor对象
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
//注册listener用于数据回调
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
}
从代码上看,应用首先要使用sensor service名来获取SensorManager对象实例,然后调用其成员函数registerListener并传入listener来得到回调数据。
Sensor service在后台和driver交互获取数据,各个应用连上service获取想要的sensor数据,从如上代码看,没有任何和service交互的代码,这一切都被封装到SensorManager里了。
2:Sensor service
Android轻量级的系统服务,一般都会运行于systemserver内,sensor service够轻量,当然不能例外。
System server起来时,会创建sensorserivice:
//frameworks/base/cmds/system_server/library/system_init.cpp
extern "C" status_t system_init()
{
LOGI("Entered system_init()");
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
LOGI("ServiceManager: %p\n", sm.get());
sp<GrimReaper> grim = new GrimReaper();
sm->asBinder()->linkToDeath(grim, grim.get(), 0);
char propBuf[PROPERTY_VALUE_MAX];
property_get("system_init.startsu易做图ceflinger", propBuf, "1");
if (strcmp(propBuf, "1") == 0) {
// Start the Su易做图ceFlinger
Su易做图ceFlinger::instantiate();
}
// Start the sensor service
SensorService::instantiate();
…..
}
通过调用SensorService的静态成员函数instantiate()来初始化并创建sensor service,在详细介绍这个函数的内部行为之前,先来看下SensorService类的声明。
//frameworks/base/services/sensorservice/Sensorservice.h
class SensorService :
public BinderService<SensorService>,
public BnSensorServer,
protected Thread
{
friend class BinderService<SensorService>;
static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
SensorService();
virtual ~SensorService();
virtual void onFirstRef();
// Thread inte易做图ce
virtual bool threadLoop();
// ISensorServer inte易做图ce
virtual Vector<Sensor> getSensorList();
virtual sp<ISensorEventConnection> createSensorEventConnection();
virtual status_t dump(int fd, const Vector<String16>& args);
class SensorEventConnection : public BnSensorEventConnection {
virtual ~SensorEventConnection();
virtual void onFirstRef();
virtual sp<SensorChannel> getSensorChannel() const;
virtual status_t enableDisable(int handle, bool enabled);
virtual status_t setEventRate(int handle, nsecs_t ns);
sp<SensorService> const mService;
sp<SensorChannel> const mChannel;
mutable Mutex mConnectionLock;
// protected by SensorService::mLock
SortedVector<int> mSensorInfo;
public:
SensorEventConnection(const sp<SensorService>& service);
status_t sendEvents(sensors_event_t const* buffer, size_t count,
sensors_event_t* scratch = NULL);
bool hasSensor(int32_t handle) const;
bool hasAnySensor() const;
bool addSensor(int32_t handle);
bool removeSensor(int32_t handle);
};
class SensorRecord {
SortedVector< wp<SensorEventConnection> > mConnections;
public:
SensorRecord(const sp<SensorEventConnection>& connection);
bool addConnection(const sp<SensorEventConnection>& connection);
bool removeConnection(const wp<SensorEventConnection>& connection);
size_t getNumConnections() const { return mConnections.size(); }
};
SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
DefaultKeyedVector<int, SensorInte易做图ce*> getActiveVirtualSensors() const;
String8 getSensorName(int handle) const;
void recordLastValue(sensors_event_t const * buffer, size_t count);
static void sortEventBuffer(sensors_event_t* buffer, size_t count);
void registerSensor(SensorInte易做图ce* sensor);
void registerVirtualSensor(SensorInte易做图ce* sensor);
// constants
Vector<Sensor> mSensorList;
DefaultKeyedVector<int, SensorInte易做图ce*> mSensorMap;
Vector<SensorInte易做图ce *> mVirtualSensorList;
Permission mDump;
status_t mInitCheck;
// protected by mLock
mutable Mutex mLock;
DefaultKeyedVector<int, SensorRecord*> mActiveSens
补充:移动开发 , Android ,
上一个:Android2.3 Sip简单分析
下一个:P76v 调试