.NET MAUI - Android Watch Application Showcase (Part 1)

.NET MAUI - Android Watch Application Showcase (Part 1)
December 20, 2023
As software developers, we are facing demands from a new generation of users, who are not just sitting in front of computer monitors but are using our apps on mobile devices and increasingly also are wearing other devices. Like smart watches, headphones and in future, there may exist even other device types, like smart glasses, personal sensors, etc. This article covers our experience in one of such projects. We wrote a multiplatform dotnet MAUI application for both Android and iOS and decided to extend it to another interface – Android watches.

There are plenty of documentation and discussions across the internet about writing applications for Wear OS in general. So, I would leave general information about it to official Google and Microsoft sites and focus instead only on our experience in this article – how to extend .NET MAUI mobile application for Wear OS.

Step 0 – Analysis and preparation

Even though we are going to write a WearOS supplement for a .NET MAUI application, we will use the same approach, which Microsoft developed for Xamarin. Follow this excellent documentation for more: https://learn.microsoft.com/en-us/xamarin/android/wear/get-started/intro-to-wear.

Since Android Wear 2.0, developers prepare the Wear OS application always as a standalone .APK package with defined hardware in its AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
  <uses-feature android:name="android.hardware.type.watch" />
 <application android:allowBackup="true" android:icon="@mipmap/appicon" ... >
   ...
 </application>
</manifest>

There are no requirements for AndroidManifest.xml file for our .NET MAUI mobile application. So, we can directly move to the next step – add Wear OS app to our solution.

Step 1 – Add .NET Android project to .NET MAUI solution

Open your .NET MAUI solution in Visual Studio, right-click on solution and add the new Android Wear Application. See picture 1.  

When you are done, the project structure should look like ours in picture 2. In our example, Ias.Sabot.Mobile project is the main MAUI application, Base projects are pure .NET project without dependency on underlaying platform, Core project is .NET MAUI library with platform or MAUI dependent code and finally, Ias.Sabot.Mobile.Watch.Android project is the .NET Android app for watches.  

Like in Xamarin.Android, there is one MainActivity.cs in it, Resources with layers, drawables and values folder.

Obsah obrázku text, snímek obrazovky, software, čísloPopis byl vytvořen automaticky
Picture 1 - Creating a new Android Wear application
Obsah obrázku text, snímek obrazovky, Písmo, čísloPopis byl vytvořen automaticky
Picture 2 – Solution structure of .NET MAUI app with Android Wear OS supplement

There is one Wear OS specific in resources. There are folders “values” and “values-round”. In the first one, you can have default strings, colors, dimensions for UI defined and in the second folder, you can override some of them for rounded – circle-like UIs. Android Wear OS will take care of the decision, which values should be used based on what format has the watches – whether square or round (see Picture 3).

Obsah obrázku hodiny, hodinky, text, ZnačkaPopis byl vytvořen automaticky
Picture 3 – Round and Square forms of Android based watches

Step 2 – Setup development environment

How to run application on Wear OS emulator

Once we have an Android Wear application in the solution, we usually want to run it on a real device or emulator. Luckily, Android Device Manager offers us a template for emulator for watches. Let’s create an emulator and run the application for the first time.

Obsah obrázku text, snímek obrazovky, software, Počítačová ikonaPopis byl vytvořen automaticky
Picture 4 – Creating a Watch emulator with Android Device Manager
Obsah obrázku text, přístroj, Elektronické zařízení, snímek obrazovkyPopis byl vytvořen automaticky
Picture 5 – Android Wear OS emulator running

How to connect the Wear OS emulator to a real Android device

Running the Wear OS application on emulator is the easy part of setup development environment. But in real scenarios, you will need the app on emulator to communicate with mobile application running on your Android device.

It is also possible under two conditions.

  1. The Wear OS emulator must run on PC / Mac connected to the same local network as mobile device
  1. The mobile device must be connected to the PC / Mac over USB

Follow the pairing process described here:
https://developer.android.com/training/wearables/get-started/connect-phone#pair-phone-with-avd

There is a tool inside the Visual Studio, which you’ll need for that – ADB (Android Debug Bridge). The forwarding command  (adb -d forward tcp:5601 tcp:5601) must be run within it – not in system console (see Picture 6).

Obsah obrázku text, software, Počítačová ikona, Multimediální softwarePopis byl vytvořen automaticky
Picture 6 – Android Debug Bridge console

How to deploy Wear OS app to real watches

Not everything can be tested / simulated on a WearOS emulator and if you’re serious with Android Wear development, you must have real Android Watches. You can use the Android Debug Bridge for deployment to real watch devices as well.

The process of connecting Visual Studio to a real watch may slightly vary depending on the watch vendor or type. But there are a few conditions, which must be setup on any watches:

  1. It must run on Android Wear operating system (no Tizen, no Garmin, …)
  1. It must be connected over WiFi to the same local network as development PC / Mac
  1. “Developer options” must be enabled
  1. Under Developer options, “ADB debugging” must be enabled
  1. Under Developer options “Wireless debugging" must be enabled

How to enable Developer options on watches

Go to System -> About watch -> Software information and tap the “Software version” section 7 times.

The exact process may vary depending on the Android version and your language setup. But when finished, you should see it in menu under Settings:

Obsah obrázku hodiny, hodinky, popruh, Analogové hodinkyPopis byl vytvořen automaticky
Picture 7 – Developer options enabled in Android Wear

Once Developer options are enabled on watches, and ADB debugging and Wireless debugging is enabled as well, you can add the watches among target devices in Visual Studio using ADB tool.

How to add Watch among target devices in Visual Studio

There are two possible ways to do it. On Android 11 running on Samsung Watch 4, it was

  1. Open ADB console in Visual Studio
  1. Type adb connect [ip address of watches without port number]
  1. Type adb devices – there should be visible watches in list of connected Android device
  1. Then watches appears in Android debug devices

On Samsung Watch 4 with Android 13, the manual requires pairing the device with developer PC / Mac:

  1. On watches, go to “Developer options” -> “Wireless debugging”
  1. Click to “Pair new device”
  1. A screen on watches like on Picture 8 should appear
  1. Open ADB console in Visual Studio
  1. Type command adb pair [ip address]:[port number] with values from watches
  1. Enter pairing code displayed on watches to the prompt
  1. Visual Studio should add your watches as new Device to debug on
Obsah obrázku text, snímek obrazovkyPopis byl vytvořen automaticky
Picture 8 – Pairing watches with developer PC / Mac for debugging over WiFi

Once your watches are visible as local Android device, select Watches app project in Visual Studio as Startup project and run Debug. Your app will be installed on the watches.

How to deal with watches disconnecting from WiFi (due to battery saving setup)

If you try to enable Wireless debugging on watches and they keep disconnecting, go to Settings – Connection and disable Bluetooth. Then Android Wear will keep connected to WiFi to keeps connection with paired mobile phone over WiFi.

Part 1 Conclusion

Hopefully, this intro was easy to follow! In Part 2, we will take a look at specific tips regarding the codebase, so stay tuned!

Share:
Luboš is an experienced .NET developer (web and mobile apps) very familiar with SQL and DevOps. He developed a lot of web applications used worldwide by industry and the academical world. He holds a bachelor’s degree in applied physics and astrophysics. Likes bowling, rides e-mountainbike, skis and plays the violin.

Article collaborators

SABO Newsletter icon

SABO NEWSLETTER

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

About SABO Mobile IT

We focus on developing specialized software for our customers in the automotive, supplier, medical and high-tech industries in Germany and other European countries. We connect systems, data and users and generate added value for our customers with products that are intuitive to use.
Learn more about sabo