Using the accelerometer and gyroscope on an Android Wear device
Switch branches/tags
Nothing to show
Clone or download
drejkim Merge pull request #2 from kubark42/android_studio_2.0
Update project to work with Android Studio 2.0
Latest commit 3f29663 Apr 12, 2016

README.md

Android Wear Motion Sensors

This project demonstrates the use of the accelerometer and gyroscope sensors on an Android Wear device. It displays the raw values for each sensor on its own page. It also performs simple shake and rotation detection using the accelerometer and gyroscope, respectively; when a shake or a rotation is detected, the background color changes to green.

See it in action here using the Moto 360.

Code Overview

The Java files of interest are in wear/src/main/java:

As the name suggests, MainActivity.java contains the code for the main activity. The 2D Picker pattern for Android Wear is used to page through the accelerometer and gyroscope fragments. This is accomplished by adding a GridViewPager element to the activity and setting an adapter that provides the pages by extending FragmentGridPagerAdapter (see SensorFragmentPagerAdapter.java). It also contains a DotsPageIndicator to identify which page is active.

SensorFragment.java provides the UI behavior for a sensor by displaying the raw data (available through the standard Android API) and performing simple shake / rotation detection. When instantiating a sensor fragment, the newInstance() function is used to specify the desired sensor (i.e. accelerometer or gyroscope). See SensorFragmentPagerAdapter.java to see how this is accomplished.