Accessing sensors
Devices worn on the wrist are ideal for fitness apps and the inclusion of a heart rate monitor in many models makes them perfect for such tasks. The way that the SDK manages all sensors is almost identical, so seeing how one works applies to the others.
The following exercise demonstrates how to read the heart rate sensor on a wearable device:
- Open an Android Wear project with both mobile and wear modules.
- Create a layout of your choosing, ensuring you include a
TextView
to display the output.
- Open the
Manifest
file in the wear module and add the following permission:
<uses-permission android:name="android.permission.BODY_SENSORS" />
- Open the
MainActivity.java
file in the wear module and add the following fields:
private TextView textView; private SensorManager sensorManager; private Sensor sensor;
- Have the
Activity
implement a sensor event listener, like so:
public class MainActivity extends Activity implements SensorEventListener {
- Implement the methods required...