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.
The Java files of interest are in
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.