Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Things Projects

You're reading from  Android Things Projects

Product type Book
Published in Jun 2017
Publisher Packt
ISBN-13 9781787289246
Pages 232 pages
Edition 1st Edition
Languages
Author (1):
Francesco Azzola Francesco Azzola
Profile icon Francesco Azzola

Table of Contents (15) Chapters

Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Getting Started with Android Things Creating an Alarm System Using Android Things How to Make an Environmental Monitoring System Integrate Android Things with IoT Cloud Platforms Create a Smart System to Control Ambient Light Remote Weather Station Build a Spying Eye Android with Android Things

How to close the sensor connection


When the Android Things app is destroyed, it is important to release all the connections we have used to exchange data with the sensor and remove all listeners. In this way, we free all the resources used by the app. In more details, we have to release the SDA pin used to communicate to the sensor so that other apps can reuse it. In order to destroy the app gracefully, we have to execute the following steps:

  1. Unregister the sensor listener we have used to listen to value changes.
  2. Unregister the sensor listener we have used to know when the sensor is connected to the Android Things board.
  3. Close the sensor connection.

The best approach is executing the previous steps in the onDestroy method of our monitoring app. The following code describes how to implement these steps:

@Override
protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy");
sensorManager.unregisterListener(tempCallback); sensorManager.unregisterListener(pressCallback); mySensorDriver...
lock icon The rest of the chapter is locked
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 $15.99/month. Cancel anytime}