Companion apps and communication

One of the most typical setups for IoT involves two apps: one for the thing and another one for mobile. It just makes sense to control your IoT device from your phone. With Android Things and Android Studio we can create both apps under the same project, each one being its own module. This setup also allows us to have another common library module that is used by both including areas such as a communication data model.

With Android Studio, we can have a mobile app and a Things app as modules of the same project

The big question about having a companion app is about how it should communicate with the IoT device. There are multiple ways to do so. The following three are the most common:

  • Hosting a REST API server on the things app and a client on the mobile one
  • Firebase Realtime Database integration on both sides
  • Nearby communication

Each option has advantages and disadvantages. In this section we will be exploring the broad setup for each of them.

We will extend the previous example of temperature and red LED and we will split it into two parts: the control of the peripherals inside the things module and the UI on the mobile one.

In all the examples, the things apps will have the same block of initialization and cleanup for the peripherals, which is the same as for the previous example, and the mobile apps will have the same layout, which will also be the same as for the previous example.

With that settled, let's make a REST API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.74.54