Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Sensor Programming By Example

You're reading from  Android Sensor Programming By Example

Product type Book
Published in Apr 2016
Publisher Packt
ISBN-13 9781785285509
Pages 194 pages
Edition 1st Edition
Languages
Author (1):
Varun Nagpal Varun Nagpal
Profile icon Varun Nagpal

Table of Contents (13) Chapters

Android Sensor Programming By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Sensor Fundamentals 2. Playing with Sensors 3. The Environmental Sensors – The Weather Utility App 4. The Light and Proximity Sensors 5. The Motion, Position, and Fingerprint Sensors 6. The Step Counter and Detector Sensors – The Pedometer App 7. The Google Fit Platform and APIs – The Fitness Tracker App

Best practice for accessing sensors


Android devices are manufactured by different OEMs (Original Equipment Manufactures) and come with various configurations. Each OEM is free to support its own set of sensors, which again come from different vendors. This creates the problem of device fragmentation. This problem is further complicated by addition and deprecation of sensors with different Android API levels. The following are some best practices that will help you deal with this device fragmentation problem and avoid common pitfalls and mistakes:

  • Before using the sensor coordinate system, confirm the default orientation mode of the device and check for the orientation of the x and y axes.
  • Check the availability, range, minimum delay, reporting modes, and resolution of the sensor before using it.
  • Before selecting the sampling period of any sensor, check for its power consumption. Also, keep your application precision and accuracy needs in mind before deciding the sampling period. It's recommended that you select one of the constants given by the operating system.
  • Do not block or do heavy processing on the OnSensorChanged() method. Your app might miss callbacks or go into ANR (Application Not Responding) mode. The app might even crash in the worst cases if this callback is blocked.
  • Every registration of the event listener should be paired with the un-registration of the same listener. This should be done at the right time and place. (More on this, in the next chapter).
  • Avoid using deprecated sensors and any of the deprecated APIs.
  • Never write any kind of application logic based on the delay between the sensor events. Always use the timestamp from the sensor event to do your time-related calculations.
  • If some sensors are mandatory for your application to function, then use the uses-feature filter in the Manifest.xml file and change the required value to true.
  • Check your application and its sensor behavior on more than one device, as the sensor values and range may vary with different devices.
You have been reading a chapter from
Android Sensor Programming By Example
Published in: Apr 2016 Publisher: Packt ISBN-13: 9781785285509
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}