Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Sending and Syncing Data

Save for later
  • 4 min read
  • 10 Aug 2015

article-image

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:

    sending-and-syncing-data-img-0

  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:

    sending-and-syncing-data-img-1

  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:

    sending-and-syncing-data-img-2

    Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €18.99/month. Cancel anytime

    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:

    sending-and-syncing-data-img-3

    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:

    sending-and-syncing-data-img-4

    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:

     sending-and-syncing-data-img-5

  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: