Channels ▼
RSS

Embedded Systems

The Wearable Computing Revolution Begins: Building Android Wear Apps


You can leverage your existing experience with Android APIs to create wearable apps; that is, apps that run on Android Wear devices. While these apps share some APIs with those that target smartphones or tablets (known as handheld apps), they are actually completely different applications and they require you to shift your — dare I say it? — pardigm. In this article, I explain the necessary steps to code and build a sample Android Wear app that uses speech recognition with Android Studio and highlight some of the differences of handheld apps.

Setting up the Development Environment

It is possible to develop wearable apps with Eclipse ADT, but Google has removed the templates from the latest versions and is favoring Android Studio to create these kinds of apps. I've tested my sample app with Android Studio 0.8.14 and Android 4.4W.2 SDK. You can use Eclipse ADT to test the sample app, but I will not be going into detail on the necessary steps to build the app with this IDE (this article will stick to Android Studio). I'll assume you have previous experience with the Android SDK and Android development practices.

To test your wearable app, you will need an Android device. There is an emulator for Android Wear, but it requires an Android device connected to the USB port of your computer to provide a really useful emulation. At the time of writing, it wasn't possible to run two emulator instances (one for the Android Wear device and the other for a handheld). So you need an Android device with the Android Wear Companion app installed on it to run an Android Wear emulator. In fact, if you want to publish your wearable app to the Google Play store, you need a handheld app that contains the wearable app. When the user downloads the handheld app, it automatically pushes the wearable app to the paired Android Wear device.

A common mistake is to downsize an existing handheld app to create a smaller or simpler wearable app. Android Wear devices have very limited user interaction capabilities. They are not Android devices with a smaller screen. For example, Android Wear doesn't provide a keyboard to allow the user to enter data.

You can develop wearable apps that run in standalone mode on the Android Wear device without being paired with a handheld device. However, if the wearable app requires either voice recognition or Internet access, it will need to work in conjunction with a handheld app that provides these services. It is also possible to use the handheld app to run some tasks that provide results to the wearable app or to allow the user to either control or configure the wearable app.

Getting Started

Launch the Android SDK Manager. Download and install the latest Android 4.4W SDK (see Figure 1). At the time of writing, the latest version was Android 4.4W.2 (API 20 Revision 2). Make sure you check the SDK platform and the system images to use the emulator: "Android Wear ARM EABI v7a System Image" and "Android Wear Intel x86 Atom System Image." In addition, install the latest version of the Google Repository located within the Extras folder.

Android Wear
Figure 1: The components of Android 4.4W (API 20) in the Android SDK Manager.

If you meet the necessary hardware requirements, it is usually a good idea to install the Intel x86 Emulator Accelerator (HAXM), which speeds up Android emulation and is also compatible with Android Wear. If you use HAXM, just make sure you always install the latest available version from the official website. The version available through the Android SDK Manager might not be the latest, and so it can cause problems that may already be solved in the updated version on the official site. For example, the Intel x86 Emulator Accelerator (HAXM installer) version 5 package listed within the Extras in the Android Manager fails to install on Mac OS X Yosemite, but the newer version downloadable from the official website works OK with this operating system version.

Once you have the Android 4.4W SDK and images, you must create an Android Wear virtual device with the Android Virtual Device Manager, also known as AVD Manager. Launch the AVD Manager from Android Studio and click Create Virtual Device…. The newest version includes many changes in the AVD Manager because the number of device categories has grown. Select Wear in Category  "Android 4.4W - API Level 20 in Target." You will have two Android Wear device choices (see Figure 2):

  • Android Wear Square: Size: 1.65", Resolution: 280x280, Density: hdpi
  • Android Wear Round: Size: 1.65", Resolution: 320x320, Density: hdpi

Android Wear
Figure 2: The Android Wear device choices in the new AVD Manager interface.

Select one of the two devices and click Next. If you are using Intel HAXM, you should select the x86 ABI to take advantage of hardware acceleration. Use the default values and finish the creation of the Android Wear virtual device. Sadly, Google removed the hardware keyboard support. The keyboard is useful for typing words to test the voice recognition(and your code) without using a real device. After you create the device, go to the avd folder where the AVD Manager creates a new subfolder for each virtual device you make. Here, you can edit the config.ini within the recently created virtual device subfolder. AVD Manager uses underscores (_) to replace spaces in the AVD name, and adds the .avd extension. Thus, the config.ini file for a virtual device named "Android Wear Square" will be in the Android_Wear_Square.avd subfolder.

Replace the following line:

hw.keyboard=no

With this:

hw.keyboard=yes

The setting is equivalent to activating the Hardware keyboard present in previous AVD Manager versions. Android Wear devices do not have keyboards, but you can use your keyboard in the emulator to type words instead of using the speech recognizer for voice input.

Launch the emulator. When it finishes the boot process, you will see the time and a disconnected cloud icon. Next, enable USB debugging in your Android handheld device (you need to access the Developer options in your device). Then, install the Android Wear Companion app on this device and connect it with a USB cable to your computer.

Open a command prompt or a terminal window (according to your operating system) and go to the platform-tools or Platform Tools subfolder within the Android SDK installation folder. Make sure that the Android handheld device is connected to your computer by running adb with devices as a parameter (adb devices or ./adb devices according to your operating system). You should see the handheld device and the emulator device listed within the List of devices attached list without any error messages.

Run the following adb command: adb -d forward tcp:5601 tcp:5601 or ./adb -d forward tcp:5601 tcp:5601, according to your operating system. This way, adb directs command to the only connected USB device and forwards socket connections from local TCP port 5601 to remote TCP port 5601.

Then, launch the Android Wear Companion app on the handheld device and establish a connection to pair it with the emulator. The action bar will display a watch icon and an Emulator Connected label. The disconnected cloud icon will disappear in the Android Wear emulator and you will see weather information displayed.

Creating a Wearable App in Android Studio

Create a new project in Android Studio. I'll use SpeechWear as the application name and the default values for all the other fields in the wizard. The project will include both handheld and wearable app modules. Check both Phone and Tablet and Wear. The Minimum SDK for Phone and Tablet is API 9: Android 2.3 (Gingerbread), but you can select a higher SDK for the handheld app. The Minimum SDK for Wear is API 20: Android 4.4 (KitKat Wear).

Select Blank Activity in Add an Activity to Mobile. Then, select Blank Wear Activity in Add an Activity to Wear. As you might guess, Android Studio uses Mobile to refer to the handheld.

The Blank Wear Activity creates a different layout for each of the Android Wear screens: Round layout (Android Wear Round) and Rectangular (Android Wear Square) layout. The layout for the blank activity in the wear module specify each layout in app:rectLayout and app:roundLayout. The following lines show the code for wear/src/main/res/layout/activity_my.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/watch_view_stub"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rectLayout="@layout/rect_activity_my"
    app:roundLayout="@layout/round_activity_my"
    tools:context=".MyActivity"
    tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>

This way, the app loads the appropriate layout for the Android Wear device. There is a big difference between the information your app can display in Wear Round and Wear Square screens, and in some cases, it is difficult to have a single layout appropriate for both screens. In some cases, however, you just need to center the contents to make sure that the Wear Round screen doesn't hide the information that you expect to be visible and there is no need to maintain two separate layouts.

Each layout displays a different welcome message. If you build and run the wear module on a Wear Round emulator or device, the round_activity_my.xml layout will display a Hello Round World! message. If you run the wear module on a Wear Square emulator, the rect_activity_my.xml layout will display a Hello Square World! message.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video