Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Things Projects

You're reading from   Android Things Projects Efficiently build IoT projects with Android Things

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781787289246
Length 232 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Francesco Azzola Francesco Azzola
Author Profile Icon Francesco Azzola
Francesco Azzola
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Getting Started with Android Things 2. Creating an Alarm System Using Android Things FREE CHAPTER 3. How to Make an Environmental Monitoring System 4. Integrate Android Things with IoT Cloud Platforms 5. Create a Smart System to Control Ambient Light 6. Remote Weather Station 7. Build a Spying Eye 8. Android with Android Things

How to close the connection with a GPIO pin

In this last step, we will learn how to close the connection with a GPIO pin. This is an important step because in this way, we free the resources and remove all the listeners we added to the GPIO pins.

An Android Things app has a life cycle very similar to an Android app. The place where we implement these actions is the Activity onDestroy method. In this method we have to:

  • Remove all the listeners attached to the GPIO pins
  • Close the connection to the GPIO pins

So, open MainActivity.java again and look for the onDestroy method and modify it:

@Override
protected void onDestroy()
{ super.onDestroy(); Log.d(TAG, "onDestroy");
if (gpioPin != null) {
gpioPin.unregisterGpioCallback(sensorCallback);
try {
gpioPin.close(); gpioPin = null;
}
catch(Exception e) {}
}
}
...
You have been reading a chapter from
Android Things Projects
Published in: Jun 2017
Publisher: Packt
ISBN-13: 9781787289246
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 $19.99/month. Cancel anytime