mardi 10 mars 2015

Check what sensors an Android device has?



So I want to create a list with all available sensor types on an Android device, so in a timer I can get the current value from all available sensors.


I have this currently:



List<Sensor> availableSensors;

public void getAllSensors() {
PackageManager manager = getPackageManager();
SensorManager sensorMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sensorMngr.getSensorList(Sensor.TYPE_ALL);

for (Sensor sensor : sensors) {
if (manager.hasSystemFeature(/*Not sure what to do here*/ sensor)) {
availableSensors.add(sensor);
}
}
}


I added a comment on the bit I'm unsure on. hasSystemFeature seems to be expecting a string, but when I try parsing it sensor.getName() or even sensor, I'm getting no results.


How do you check if a device has a sensor type, when that type is stored in a variable? I'm sure the answer is really simple, but I just can't work it out. I need to future proof this code, so I can't use a switch to work it out.




Aucun commentaire:

Enregistrer un commentaire