VISUAL STUDIO   Windows Mac

Getting Started

PDF for offline use
Sample Code:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-06

Create your first Android Wear app and run it on a Wear emulator or device. This walkthrough provides step-by-step instructions for creating a small Android Wear project that handles button clicks and displays a click counter on the Wear device. It explains how to debug the app using a Wear emulator or a Wear device that is connected via Bluetooth to an Android phone. It also provides a set of debugging tips for Android Wear.

Screenshot of the Wear app to be completed in this tutorial

Your first Wear app

Follow these steps to create your first Xamarin.Android Wear app:

1. Create a new Android project

Create a new Android Wear Application:

Creating a new Android Wear Application in the New Project dialog

Creating a new Android Wear Application in the New Solution dialog

This template automatically includes the Xamarin Android Wearable Library NuGet (and dependencies) so you'll have access to Wear-specific widgets. If you don't see the Wear template, review the Installation and Setup guide to double-check that you have installed a supported Android SDK.

2. Choose the correct Target Framework

Ensure that Minimum Android to target is set to Android 5.0 (Lollipop) or later:

Setting the Target Framework to Android 5.0 in Visual Studio

Ensure the target framework is set to Android 5.0 (Lollipop) or later:

Setting the Target Framework to Android 5.0 in Visual Studio for Mac

For more information on setting the target framework, see Understanding Android API Levels.

3. Edit the Main.axml layout

Configure the layout to contain a TextView and a Button for the sample:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
  <ScrollView
    android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:fillViewport="true">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:text="Main Activity"
        android:textSize="36sp"
        android:textColor="#006600" />
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:textColor="#cccccc"
        android:id="@+id/result" />
      <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showNotification"
        android:text="Click Me!"
        android:id="@+id/click_button" />
    </LinearLayout>
  </ScrollView>
</FrameLayout>

4. Edit the MainActivity.cs source

Add the code to increment a counter and display it whenever the button is clicked:

[Activity (Label = "WearTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
  int count = 1;

  protected override void OnCreate (Bundle bundle)
  {
    base.OnCreate (bundle);

    SetContentView (Resource.Layout.Main);

    Button button = FindViewById<Button> (Resource.Id.click_button);
    TextView text = FindViewById<TextView> (Resource.Id.result);

    button.Click += delegate {
      text.Text = string.Format ("{0} clicks!", count++);
    };
  }
}

5. Run the Android Wear app

The next step is to deploy and run the app. If you are not yet familiar with the process of deploying and running Xamarin.Android apps, see the Hello, Android Quickstart.

Using an Emulator

If you are using an emulator, be sure to choose the correct Wear Android Wear Device (AVD) before you start debugging:

Choosing a Wear AVD in Visual Studio device menu

Choosing a Wear AVD in Visual Studio for Mac device menu

Don't be surprised if you see this (or another interstitial screen) at first. The watch emulator can take a while to start up:

Watch emulator displays Just a minute ...

Eventually you'll be rewarded with your first Xamarin.Android Wear app:

Screenshot of first app screen Screenshot of screen after 1 click Screenshot of screen after 7 clicks

Using an Android Wear Device

If you have an Android Wear device such as an Android Wear Smartwatch, You can run the app on the device instead of using an emulator. If your Wear device has a USB port, you can connect the Wear device to your computer, deploy to it, and run/debug the app as you would on an Android phone.

If your Wear device does not have a USB port, you can deploy the app to the Wear device over Bluetooth by routing the app's debug output to an Android phone that is connected to your computer. Use the following steps to make this connection:

  1. Prepare your Android Wear device by following the steps in Installation and Setup.

  2. On the phone that will act as Bluetooth intermediary, start the Android Wear app.

  3. Tap the Settings icon.

  4. Enable Debugging over Bluetooth. You should see the following status displayed on the screen of the Android Wear app:

    Host: disconnected
    Target: connected
  5. Connect the phone to your computer over USB and run the following commands:

    adb forward tcp:4444 localabstract:/adb-hub
    adb connect 127.0.0.1:4444

    If port 4444 is not available, you can use any other available port to which you have access.

    ℹ️

    Note: If you restart Visual Studio for Mac or Visual Studio, you must run these commands again to setup a connection to the Wear device.

  6. When the Wear device prompts you, confirm that you are allowing ADB Debugging. In the Android Wear app, you should see the status change to:

    Host: connected
    Target: connected
  7. After you complete the above steps, running adb devices shows the status of both the phone and the Android Wear device:

    List of devices attached
    127.0.0.1:4444    device
    019ad61df0a69399  device

Deploy the app to the device. The Android Wear device should appear in the device pulldown menu. For example:

LGE G Watch appears in the device pulldown menu

If you are using Bluetooth, it will take more time to deploy the app than it would over USB. For example, it takes about 5 minutes to deploy this app to an LG G Watch that is Bluetooth-connected to a Nexus 5 phone.

After the app successfully deploys, the screen of the Wear device should display the following screen:

Initial screen of Wear app

Tap the CLICK ME! button on the face of the Wear device and see the count increment with each tap:

Screenshot of Wear app after 3 clicks

6. Debugging Tips

The following tips can be useful if you are debugging your app via a Bluetooth connection:

Taking screenshots

You can take a screenshot of the Wear device by entering the following command:

adb -s 127.0.0.1:4444 shell screencap -p /sdcard/DCIM/screencap.png

Copy the screenshot to your computer by entering the following command:

adb -s 127.0.0.1:4444 pull /sdcard/DCIM/screencap.png

Delete the screenshot on the device by entering the following command:

adb -s 127.0.0.1:4444 shell rm /sdcard/DCIM/screencap.png

Uninstalling an app

You can uninstall an app from the wear device by entering the following command:

adb -s 127.0.0.1:4444 uninstall <package name>

For example, to remove the app with the package name com.xamarin.weartest, enter the following command:

adb -s 127.0.0.1:4444 uninstall com.xamarin.weartest

For more information about debugging Android Wear devices over Bluetooth, see Debugging over Bluetooth.

Debugging a Wear app with a companion phone app

Android Wear apps are packaged with a companion Android phone app for distribution on Google Play (for more information, see Working with Packaging). However, you still develop the Wear app and its companion app separately. When you release your app through the Google Play Store, the Wear app will be packaged with the companion app and automatically installed if possible.

To debug the Wear app with a companion app:

  1. Build and deploy the companion app to the phone.

  2. Right-click the Wear project and set it as the default start project.

  3. Deploy the Wear project to the wearable device.

  4. Run and debug the Wear app on the device.

Next Steps

Check out the Wear samples including Android Wear apps with companion Phone apps.

When you are ready to distribute your app, see Working with Packaging.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.