1 Android JumpingJack Sample 2 =================================== 3 4 A basic sample showing how to use the Gravity sensor on the wearable device 5 by counting how many jumping jacks you have performed. 6 7 Introduction 8 ------------ 9 10 [SensorEventListener][1] offers you methods used for receiving notifications from the 11 [SensorManager][2] when sensor values have changed. 12 13 This example counts how many times Jumping Jakcs are performed by detecting the value 14 of the Gravity sensor by the following code: 15 16 ```java 17 @Override 18 public void onSensorChanged(SensorEvent event) { 19 detectJump(event.values[0], event.timestamp); 20 } 21 22 private void detectJump(float xValue, long timestamp) { 23 if ((Math.abs(xValue) > GRAVITY_THRESHOLD)) { 24 if(timestamp - mLastTime < TIME_THRESHOLD_NS && mUp != (xValue > 0)) { 25 onJumpDetected(!mUp); 26 } 27 mUp = xValue > 0; 28 mLastTime = timestamp; 29 } 30 } 31 ``` 32 33 The detectJump method above assumes that when a person is wearing the watch, the x-component of gravity 34 as measured by the Gravity Sensor is +9.8 when the hand is downward and -9.8 when the hand 35 is upward (signs are reversed if the watch is worn on the right hand). Since the upward or 36 downward may not be completely accurate, we leave some room and instead of 9.8, we use 37 GRAVITY_THRESHOLD (7.0f). We also consider the up <-> down movement successful if it takes less than 38 TIME_THRESHOLD_NS (2000000000 nanoseconds). 39 40 [1]: http://developer.android.com/reference/android/hardware/SensorEventListener.html 41 [2]: http://developer.android.com/reference/android/hardware/SensorManager.html 42 43 Pre-requisites 44 -------------- 45 46 - Android SDK v21 47 - Android Build Tools v21.1.1 48 - Android Support Repository 49 50 Screenshots 51 ------------- 52 53 <img src="screenshots/jumping_jack.gif" height="400" alt="Screenshot"/> 54 55 Getting Started 56 --------------- 57 58 This sample uses the Gradle build system. To build this project, use the 59 "gradlew build" command or use "Import Project" in Android Studio. 60 61 Support 62 ------- 63 64 - Google+ Community: https://plus.google.com/communities/105153134372062985968 65 - Stack Overflow: http://stackoverflow.com/questions/tagged/android 66 67 If you've found an error in this sample, please file an issue: 68 https://github.com/googlesamples/android-JumpingJack 69 70 Patches are encouraged, and may be submitted by forking this project and 71 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 72 73 License 74 ------- 75 76 Copyright 2014 The Android Open Source Project, Inc. 77 78 Licensed to the Apache Software Foundation (ASF) under one or more contributor 79 license agreements. See the NOTICE file distributed with this work for 80 additional information regarding copyright ownership. The ASF licenses this 81 file to you under the Apache License, Version 2.0 (the "License"); you may not 82 use this file except in compliance with the License. You may obtain a copy of 83 the License at 84 85 http://www.apache.org/licenses/LICENSE-2.0 86 87 Unless required by applicable law or agreed to in writing, software 88 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 89 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 90 License for the specific language governing permissions and limitations under 91 the License. 92