Sensors tutorial: How to use sensor in Android
Android Sensor
Sensor list
Barometer
SensorManager
SensorEventListener
The most common sensors are:
- GPS
- Proximity sensor
- Light sensor
- Temperature sensor
- Barometer sensor
- NFC
We want to create an app that show the current pressure:
Using sensor in android
When we develop an android app and we need a specific sensor so that our app can run we have two different choices:- Specify it the sensor in the AndroidManifest.xml
- Detect the sensor list and check if the one we are interested on is available
and once we have selected ‘User feature’, we have:
We can retrieve the sensor list too and we need a bit of code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the reference to the sensor manager
sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
// Get the list of sensor
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
List<Map<String, String>> sensorData = new ArrayList<Map<String,String>>();
for (Sensor sensor: sensorList) {
Map<String, String> data = new HashMap<String, String>();
data.put("name", sensor.getName());
data.put("vendor", sensor.getVendor());
sensorData.add(data);
}
}
At line 7 we get the reference to the SensorManager, used to handle sensor, then at line 10 we get the sensor list. In this case we want to have all the sensors present in our smartphone so we use Sensor.TYPE_ALL. If we wanted just one time we can filter the list passing the type of the sensor we are looking for. For example, if we want to have all the barometer sensor we can use:
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_PRESSURE);
Once we have the list we can simply show it using a ListView and SimpleAdapter. The result (in my smartphone) is
What's now? We can have several information from the Sensor class, for example Vendor sensor resolution, min and max range. You have to keep in mind that sensor range can vary among different sensors. Once we have the list we can check if the smartphone supports our sensor. Now we have our sensors we want to get information from them.
Sensor Events
To get information from a sensor there's a simple method: register a listener. First we have to select the sensor we are interested on and then register our listener. In our case, we are interesting on barometer sensor so we have:// Look for barometer sensor
SensorManager snsMgr = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
Sensor pS = snsMgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
snsMgr.registerListener(this, pS, SensorManager.SENSOR_DELAY_UI);
At line 4 we register our listener. Notice that the last parameter represents how fast we want to get notified when the value measured by the sensor changes. There are several values but notice that a too fast notification rate can have some side effects on your apps. To register a class as a listener we have simply implements an interface SensorEventListener for example:
public class PressActivity extends Activity implements SensorEventListener {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
pressView.setText("" + values[0]);
}
}
At line 3 we override a method that is called when the accuracy changes. This parameter represents the confidence level of the value we get from the sensor. The other method (the one more interesting) is onSensorChanged that is called when the value changes. In this case we simply get the first value e show it in a TextVIew. The result is shown below:
For example a typical application can show the pressure trend to know if the sun will shy or we will have clouds.
Source code available @ github.
0 comments:
Post a Comment