| 1 | --- |
| 2 | title: "Build Wear OS Apps and Connect with Android" |
| 3 | summary: "Create Wear OS app and integrate them with Android, covering setup, permissions, and data sync." |
| 4 | date: 2025-11-06 |
| 5 | --- |
| 6 | |
| 7 | ## Initial Setup |
| 8 | |
| 9 | Start by creating an android app using your preferred framework and open your project in Android Studio (open the `android` folder if using cross-platform frameworks). |
| 10 | |
| 11 | ## Phone App Setup |
| 12 | |
| 13 | First, we need to bind a listener so we can listen to messages sent by our watch even when our app is closed/killed. This requires adding permissions and implementing the package. Don't worry about prompting users for permissions - its not needed as it is a [normal](https://developer.android.com/guide/topics/permissions/overview#normal) permission. |
| 14 | |
| 15 | ### Adding Dependencies |
| 16 | |
| 17 | Add the following to your app level gradle file (`app/build.gradle`): |
| 18 | |
| 19 | ```kotlin |
| 20 | implementation("com.google.android.gms:play-services-wearable:19.0.0") |
| 21 | ``` |
| 22 | |
| 23 | Don't forget to press `Sync Now` |
| 24 | |
| 25 | ### Permissions Setup |
| 26 | |
| 27 | Add the following code in the `manifest` block of `AndroidManifest.xml`: |
| 28 | |
| 29 | ```xml |
| 30 | <uses-permission android:name="com.google.android.wearable.permission.BIND_WEARABLE_LISTENER" /> |
| 31 | ``` |
| 32 | |
| 33 | ### Creating Message Receiver |
| 34 | |
| 35 | Now, we'll write a service that will be `android:exported`. Create a new file `WearMessageReceiver.kt` in the same folder as `MainActivity.kt`: |
| 36 | |
| 37 | ```kotlin |
| 38 | package com.your.package |
| 39 | |
| 40 | import android.content.Intent |
| 41 | import com.google.android.gms.wearable.MessageEvent |
| 42 | import com.google.android.gms.wearable.WearableListenerService |
| 43 | |
| 44 | class WearMessageReceiver : WearableListenerService() { |
| 45 | override fun onMessageReceived(event: MessageEvent) { |
| 46 | when (event.path) { |
| 47 | "/open" -> { |
| 48 | val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply { |
| 49 | addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 50 | addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) |
| 51 | } |
| 52 | startActivity(intent) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | ``` |
| 58 | |
| 59 | The above code listens for the `/open` message and when received, it opens the app. |
| 60 | |
| 61 | ### Registering the Service |
| 62 | |
| 63 | We need to tell our phone about this service. Add the following code in the `application` block of `AndroidManifest.xml`: |
| 64 | |
| 65 | ```xml |
| 66 | <service |
| 67 | android:name="[package name].WearMessageReceiver" |
| 68 | android:exported="true"> |
| 69 | <intent-filter> |
| 70 | <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" /> |
| 71 | <category android:name="android.intent.category.DEFAULT" /> |
| 72 | <data |
| 73 | android:host="*" |
| 74 | android:pathPattern=".*" |
| 75 | android:scheme="wear" /> |
| 76 | </intent-filter> |
| 77 | </service> |
| 78 | ``` |
| 79 | |
| 80 | ### Message Sending Function |
| 81 | |
| 82 | This function sends messages from our phone to the watch. Add it to `MainActivity.kt`: |
| 83 | |
| 84 | ```kotlin |
| 85 | suspend fun sendMessageToWatch( |
| 86 | context: Context, |
| 87 | path: String, |
| 88 | data: ByteArray = ByteArray(0) |
| 89 | ) { |
| 90 | withContext(Dispatchers.IO) { |
| 91 | try { |
| 92 | val nodes = Tasks.await(Wearable.getNodeClient(context).connectedNodes) |
| 93 | // it is recommended to filter out nodes with `isNearby = false` |
| 94 | nodes.forEach { node -> |
| 95 | Tasks.await( |
| 96 | Wearable.getMessageClient(context).sendMessage( |
| 97 | node.id, |
| 98 | path, |
| 99 | data, |
| 100 | ) |
| 101 | ) |
| 102 | } |
| 103 | } catch (e: Exception) { |
| 104 | e.printStackTrace() |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | ``` |
| 109 | |
| 110 | Read more on suspend functions [here](https://kotlinlang.org/docs/composing-suspending-functions.html). |
| 111 | |
| 112 | ## Watch App Setup |
| 113 | |
| 114 | Here, you'll need a Wear OS device connected to your phone and connected to your PC via wireless debugging. Don't have a Wear OS device? No problem! Create a new emulator and run it following the setup instructions. Unlike Apple Watch simulator, it will connect to your physical phone or emulator. |
| 115 | |
| 116 | ### Creating the Watch Project |
| 117 | |
| 118 | 1. Go to `Android Studio > File > New > New Project > Wear OS > Empty Wear App` |
| 119 | 2. Keep the bundle identifier same as your phone app |
| 120 | 3. Connect a Wear OS device or create an emulator |
| 121 | |
| 122 | ### Adding Permissions |
| 123 | |
| 124 | Just like the phone app, we need to bind a listener and add permissions. Add to the `manifest` block in `AndroidManifest.xml`: |
| 125 | |
| 126 | ```xml |
| 127 | <uses-permission android:name="com.google.android.wearable.permission.BIND_WEARABLE_LISTENER" /> |
| 128 | ``` |
| 129 | |
| 130 | ### Creating Message Listener |
| 131 | |
| 132 | Create `MessageListenerService.kt`: |
| 133 | |
| 134 | ```kotlin |
| 135 | // make sure the name of this class matches the one in AndroidManifest.xml |
| 136 | class MessageListenerService : WearableListenerService() { |
| 137 | override fun onMessageReceived(event: MessageEvent) { |
| 138 | super.onMessageReceived(event) |
| 139 | when (event.path) { |
| 140 | "/boot" -> { |
| 141 | val intent = Intent(this, MainActivity::class.java).apply { |
| 142 | addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 143 | } |
| 144 | startActivity(intent) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | ``` |
| 150 | |
| 151 | ### Registering Watch Service |
| 152 | |
| 153 | Add to the `application` block in `AndroidManifest.xml`: |
| 154 | |
| 155 | ```xml |
| 156 | <service |
| 157 | android:name=".MessageListenerService" |
| 158 | android:exported="true"> |
| 159 | <intent-filter> |
| 160 | <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" /> |
| 161 | <data |
| 162 | android:host="*" |
| 163 | android:pathPattern=".*" |
| 164 | android:scheme="wear" /> |
| 165 | </intent-filter> |
| 166 | </service> |
| 167 | ``` |
| 168 | |
| 169 | ### Message Sending Function |
| 170 | |
| 171 | Add to `MainActivity.kt`: |
| 172 | |
| 173 | ```kotlin |
| 174 | private suspend fun sendMessageToPhone(path: String, data: ByteArray) { |
| 175 | val connectedNodes = Wearable.getNodeClient(this).connectedNodes.await() |
| 176 | println("Connected nodes: $connectedNodes") |
| 177 | for (node in connectedNodes) { |
| 178 | try { |
| 179 | messageClient.sendMessage(node.id, path, data).await() |
| 180 | } catch (e: Exception) { |
| 181 | println("Failed to send message to node ${node.id}: ${e.message}") |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | ``` |
| 186 | |
| 187 | ## Final Notes |
| 188 | |
| 189 | - Messages between devices are transferred in `ByteArray` format - remember to encode data in sender device and decode the data in receiver device |
| 190 | - For cross-platform frameworks, you can call `sendMessageToWatch` using [MethodChannel](https://api.flutter.dev/flutter/services/MethodChannel-class.html) or equivalent |
| 191 | - Name of services should match the name specified in `AndroidManifest.xml` |