3 min read

This article, by Steven F. Daniel, author of the book, Android Wearable Programming, will provide you with the background and understanding of how you can effectively build applications that communicate between the Android handheld device and the Android wearable.

Android Wear comes with a number of APIs that will help to make communicating between the handheld and the wearable a breeze. We will be learning the differences between using MessageAPI, which is sometimes referred to as a “fire and forget” type of message, and DataLayerAPI that supports syncing of data between a handheld and a wearable, and NodeAPI that handles events related to each of the local and connected device nodes.

(For more resources related to this topic, see here.)

Creating a wearable send and receive application

In this section, we will take a look at how to create an Android wearable application that will send an image and a message, and display this on our wearable device. In the next sections, we will take a look at the steps required to send data to the Android wearable using DataAPI, NodeAPI, and MessageAPIs.

Firstly, create a new project in Android Studio by following these simple steps:

  1. Launch Android Studio, and then click on the File | New Project menu option.
  2. Next, enter SendReceiveData for the Application name field.
  3. Then, provide the name for the Company Domain field.
  4. Now, choose Project location and select where you would like to save your application code:

    Android Wearable Programming

  5. Click on the Next button to proceed to the next step.

    Next, we will need to specify the form factors for our phone/tablet and Android Wear devices using which our application will run. On this screen, we will need to choose the minimum SDK version for our phone/tablet and Android Wear.

  6. Click on the Phone and Tablet option and choose API 19: Android 4.4 (KitKat) for Minimum SDK.
  7. Click on the Wear option and choose API 21: Android 5.0 (Lollipop) for Minimum SDK:

    Android Wearable Programming

  8. Click on the Next button to proceed to the next step.

    In our next step, we will need to add Blank Activity to our application project for the mobile section of our app.

  9. From the Add an activity to Mobile screen, choose the Add Blank Activity option from the list of activities shown and click on the Next button to proceed to the next step:

    Android Wearable Programming

    Next, we need to customize the properties for Blank Activity so that it can be used by our application. Here we will need to specify the name of our activity, layout information, title, and menu resource file.

  10. From the Customize the Activity screen, enter MobileActivity for Activity Name shown and click on the Next button to proceed to the next step in the wizard:

    Android Wearable Programming

    In the next step, we will need to add Blank Activity to our application project for the Android wearable section of our app.

  11. From the Add an activity to Wear screen, choose the Blank Wear Activity option from the list of activities shown and click on the Next button to proceed to the next step:

    Android Wearable Programming

    Next, we need to customize the properties for Blank Wear Activity so that our Android wearable can use it. Here we will need to specify the name of our activity and the layout information.

  12. From the Customize the Activity screen, enter WearActivity for Activity Name shown and click on the Next button to proceed to the next step in the wizard:

     Android Wearable Programming

  13. Finally, click on the Finish button and the wizard will generate your project and after a few moments, the Android Studio window will appear with your project displayed.

Summary

In this article, we learned about three new APIs, DataAPI, NodeAPI, and MessageAPIs, and how we can use them and their associated methods to transmit information between the handheld mobile and the wearable.

If, for whatever reason, the connected wearable node gets disconnected from the paired handheld device, the DataApi class is smart enough to try sending again automatically once the connection is reestablished.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here