Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision.

Similar presentations


Presentation on theme: "Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision."— Presentation transcript:

1 Sensors Nasrullah Khan Niazi

2 Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision and features, vary from device to device. Some of the sensors that applications can interact with include the magnetic sensor, which can be used as a compass, and the accelerometer sensor that can detect motion. You can access the device sensors through the SensorManager object (android.hardware.SensorManager).The SensorManager object listens for data from the sensors. It is a system service, and you can retrieve an instance retrieved with the getSystemService() method, as shown here: SensorManager sensors =(SensorManager) getSystemService(Context.SENSOR_SERVICE);

3 Working with different Snsors The Sensor class (android.hardware.Sensor) defines a number of identifiers for the various sensors that you might find on a device. Not all sensors are available on each device. Some interesting sensors are listed on next slide

4 TYPE_ACCELEROMETER: Measures acceleration in three directions; values are in SI units (m/s2). TYPE_GYROSCOPE: Measures angular orientation in three directions; values are angles in degrees. TYPE_LIGHT: Measures ambient light; values are in SI lux units. TYPE_MAGNETIC_FIELD: Measures magnetism in three directions; the compass values are in micro-Tesla (uT). TYPE_PRESSURE: Measures barometric pressure. TYPE_PROXIMITY: Measures the distance to an object; values are in centimeters, or “near” versus “far.” TYPE_RELATIVE_HUMIDITY: Measures the relative humidity. TYPE_AMBIENT_TEMPERATURE: Measures temperature

5 Configuring Androidmainfest.xml for sensors The tag in the Android manifest file is used to indicate which sensors are required by your application. For example, to declare that your application requires the barometer, but can optionally use the gyroscope, you add the following to your application’s manifest file: <uses-feature android:name="android.hardware.sensor.gyroscope" android:required="false" />

6 Acquiring a reference to a sensor You can acquire a reference to a specific sensor using the SensorManager class method called getDefaultSensor().This method takes a sensor type parameter. For example,you can acquire the default accelerometer sensor as follows Sensor accelSensor= sensors.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

7 Reading Sensors Data After you have a valid Sensor object, you can read the sensor data periodically. Sensor values are sent back to an application using a SensorEventListener object that the application must implement and register using the registerListener() method. boolean isAvailable = sensors.registerListener(SensorsActivity.this, accelSensor, SensorManager.SENSOR_DELAY_NORMAL);


Download ppt "Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision."

Similar presentations


Ads by Google