GetSensorList () vs getDefaultSensor () in Android SensorManager

I am writing an Android game and want to use the accelerometer for input.

I see two ways to get the sensor, one way is to use the first element SensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER) and the other SensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) .

getDefaultSensor doc says that it can return a "composite" sensor, so if I need a "raw" sensor, I should use getSensorList .

Any idea what's the difference between a composite or raw sensor? Does this even apply to an accelerometer? Does anyone have experience working with devices that contain multiple or multiple accelerometers? (Or some other sensor?)

+4
source share
2 answers

The Google documentation is far ahead of their implementation here. I looked at the code repository (which seems to be the source of 2.3.1-ish) and found:

 public Sensor getDefaultSensor(int type) { // TODO: need to be smarter, for now, just return the 1st sensor List<Sensor> l = getSensorList(type); return l.isEmpty() ? null : l.get(0); } 

So there is no real difference (and I don't think they can add it later) between the sensors returned with getDefaultSensor() and getSensorList() .

+3
source

Update: They updated the getDefaultSensor method in Lollipop, and now there is a difference:

 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; // 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) { wakeUpSensor = true; } for (Sensor sensor : l) { if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor; } return null; } 

Therefore, if several sensors are available for the specified type, getDefaultSensor will return the version without waking up (unless the default type is one of the 6 that are actually defined as the waking sensor)

By the way, Sensor.TYPE_TILT_DETECTOR, Sensor.TYPE_WAKE_GESTURE, Sensor.TYPE_GLANCE_GESTURE and Sensor.TYPE_PICK_UP_GESTURE are hidden in the SDK, because they are intended to be used only for the system user interface. There is more detailed information about them in the source Sensor.java

+3
source

All Articles