MobileAndroidBuilding Your First Wearable Android App

Building Your First Wearable Android App

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Wearable devices are gaining more momentum, especially after Google released its Android Wear platform for developers. Smart watches powered by this platform are among the most affordable for consumers and thus we are seeing new models in the market more often than ever. The low-cost sensors, versatile connectivity, and even the fashionable designs are making this technology very accessible and appealing. Even Apple also has its own smart watches in this wearable device competition and Google is pushing more new features for Android Wear; for example, Wi-Fi connectivity, wrist gestures for navigation, and so on. Now is definitely a great time to dive into this technology. As mentioned in a previous article, the Android Wear platform is only made available for its own IDE in Android Studio, so if you need the related info, please refer to “Using Android Studio“.

This tutorial will go through the necessary setup on mobile and wearable devices as well as configurations on Android Studio. A complete example will follow to illustrate how the development process works.

Setup on Android Studio

First, check is whether you have the required Android SDK. Although Android 4.4W (API 20) works as a minimum level for you to start using Android Wear platform, I would still recommend you get the latest and greatest APIs. In Android Studio, under the system menu “Tools -> Android -> SDK Manager”, you can check if you have API 20 and above installed, as shown in Figure 1.

Next, if you do not have a Android Wear powered smart device, we need to create an Android virtual device (AVD) for our testing purpose. Under the same menu as described previously, select “AVD Manager” and then select “Create Virtual Device…” with all the default values for now. The example is in Figure 2.

WAD1

Figure 1: SDK Manager

WAD2
Figure 2: Android Wear AVD

Setup on Mobile Devices

The only required companion app for your mobile phone is Android Wear. It is free and downloadable through the Google Play store. Start the app and you can connect with wearable devices guided by on-screen step-by-setp instructions. It also provides direct control for your device settings as well as links to more wearable apps. Now, you can establish the debugging connection with the virtual device we created in the last section through the DOS command line “adb -d forward tcp:5601 tcp:5601”.

WAD3
Figure 3: Android Wear App

Setup on Wearable Devices

On your Android Wear powered device, go to “Settings -> About” and tap on “Build Number” several times till your device tells you it is about to switch on the developer option. Then, go to your “Developer Options” and enable “ADB debugging” as well as “Debug over Bluetooth”, as in Figure 4. Connect your wearable device with your mobile phone through the Android Wear app. Also, enable “Debug over Bluetooth” in the app settings.

Finally, from the DOS command line, type “adb forward tcp:4444 localabstract:/adb-hub” and then “adb connect localhost:4444”, as in Figure 5. You will see your wearable device listed as “localhost:4444”. For your information, the other listed device “05e30f613444cc5e” is my mobile phone, a Nexus 5.

WAD4
Figure 4: Wearable Device Settings

WAD5
Figure 5: Wearable Device Debugging Connection

A Working Example

After all the initial setups, we now are ready to start building our first wearable project. Click on “Start a new Android Studio project” on Android Studio and name our project “Scientist”. Check the target devices for “Phone and Tablet” and “Wear”. Simply use the default mobile and wear blank activities as our templates. The main layout file, activity_scientist_wear.xml, for the wear module is as follows. You can see it contains links to both round and square layouts.

<?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_scientist_wear"
   app_roundLayout="@layout/round_activity_scientist_wear"
   tools_context=".ScientistWearActivity"
   tools_deviceIds="wear">
   </android.support.wearable.view.WatchViewStub>

For example, the round layout file, round_activity_scientist_wear.xml, for the wear module should be similar to this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns_android="http://schemas.
      android.com/apk/res/android"
   xmlns_tools="http://schemas.android.com/tools"
      android_layout_width="match_parent"
   android_layout_height="match_parent" tools_context=
      ".ScientistWearActivity"
   tools_deviceIds="wear_round">

   <TextView android_id="@+id/text"
         android_layout_width="wrap_content"
      android_layout_height="wrap_content"
         android_layout_centerHorizontal="true"
      android_layout_centerVertical="true"
         android_text="@string/hello_round" />
</RelativeLayout>

The main activity is named ScientistWearActivity. It uses one of the most powerful APIs for the wearable interface experience by adding speech recognition capability and trying to interpret the free-form speech result. Of course, the Android Wear platform already provides a list of voice intents in the system that are ready for use. Developers can define their own app-provided voice actions as well. Starting the speech recognizer is simple and straightforward as other handy intents. In “startSpeechRecognizer()”, it prompts a question about “Who is the key scientist in the movie The Imitation Game?” and waits for free-form voice input. The result returned by the speech recognizer is captured by “onActivityResult()”. It then responds with a message depending on whether the recognized speech text contains the scientist’s name in “Alan Turing”. Figures 6 and 7 show the screen shots while the app is in action.

public class ScientistWearActivity extends Activity {

   private static final int SPEECH_RECOGNIZER
      _REQUEST_CODE = 3000;
   private TextView mTextView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_scientist_wear);
      final WatchViewStub stub = (WatchViewStub)
         findViewById(R.id.watch_view_stub);
      tub.setOnLayoutInflatedListener(new
            WatchViewStub.OnLayoutInflatedListener() {
         @Override
         public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView)
               stub.findViewById(R.id.text);
            startSpeechRecognizer();
         }
      });
   }

   private void startSpeechRecognizer() {
      Intent intent = new Intent
         (RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
         RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
         "Imitation GamenScientist");
      startActivityForResult(intent,
         SPEECH_RECOGNIZER_REQUEST_CODE);
   }

   @Override
   protected void onActivityResult(int requestCode,
         int resultCode, Intent data) {
      if (requestCode == SPEECH_RECOGNIZER_REQUEST_CODE) {
         if (resultCode == RESULT_OK) {
            List<String> results =
               data.getStringArrayListExtra
               (RecognizerIntent.EXTRA_RESULTS);
            String theText = results.get(0);
            if (theText.toUpperCase().indexOf("ALAN TURING") > -1)
               mTextView.setText("Correct!nIt is Alan Turing.");
            else
               mTextView.setText("Incorrect!nIt is Alan Turing.");
         }
      }
      super.onActivityResult(requestCode, resultCode, data);
   }
}

WAD6
Figure 6: App Start

WAD7
Figure 7: App Result

Conclusion

The Android Wear platform is by far the most popular and widely used development tool for wearable devices among developers. There are already millions of smart devices powered by it out there in the market: smart watches, glass, fitness trackers, and so forth. With Apple stepping into the competition, we are only going to see this wearable technology continue to grow at a fierce speed. Well, it is not too late for the game yet. Get ready to be creative and productive with the new upcoming devices.

References

About the Author

Chunyen Liu has been a software professional in Taiwan and the United States. He is a published author of 30+ articles and 100+ tiny apps, software patentee, technical reviewer, and programming contest winner by ACM/IBM/SUN. He holds advanced Computer Science degrees and trained in 20+ graduate-level courses. On the non-technical side, he is a certified coach, certified umpire, and rated player of USA Table Tennis, having won categorized events at State Championships and US Open.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories