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.

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:
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:
Ensure the target framework is set to Android 5.0 (Lollipop) or later:
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:
Don't be surprised if you see this (or another interstitial screen) at first. The watch emulator can take a while to start up:

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

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:
Prepare your Android Wear device by following the steps in Installation and Setup.
On the phone that will act as Bluetooth intermediary, start the Android Wear app.
Tap the Settings icon.
Enable Debugging over Bluetooth. You should see the following status displayed on the screen of the Android Wear app:
Host: disconnected Target: connectedConnect 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:4444If port 4444 is not available, you can use any other available port to which you have access.
ℹ️
Note: If you restart Xamarin Studio or Visual Studio, you must run these commands again to setup a connection to the Wear device.
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: connectedAfter you complete the above steps, running
adb devicesshows 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:
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:
Tap the CLICK ME! button on the face of the Wear device and see the count increment with each tap:
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:
Build and deploy the companion app to the phone.
Right-click the Wear project and set it as the default start project.
Deploy the Wear project to the wearable device.
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.
Let us know how you feel about this
Translation Quality
0/250
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.










