| 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 | --- |
| 5 | |
| 6 | ## Initial Setup |
| 7 | |
| 8 | Start by creating an android app using your preferred framework: |
| 9 | |
| 10 | - Flutter <span className= |
| 11 | - React Native <span className= |
| 12 | - Kotlin <span className= |
| 13 | - Android Views <span className= |
| 14 | - Any other <span className= |
| 15 | |
| 16 | Open your android project in Android Studio (open the `android` folder if using cross-platform frameworks). |
| 17 | |
| 18 | ## Phone App Setup |
| 19 | |
| 20 | First we need to create a service which will listen to messages from the watch and then we need to tell our phone about the service. |
| 21 | |
| 22 | ### Adding Dependencies |
| 23 | |
| 24 | Add the following to your app level gradle file (`app/build.gradle`): |
| 25 | |
| 26 | ```kotlin |
| 27 | implementation("com.google.android.gms:play-services-wearable:19.0.0") |
| 28 | ``` |
| 29 | |
| 30 | The dependency is needed to create the service. |
| 31 | |
| 32 | ### Permissions Setup |
| 33 | |
| 34 | Add the following code in the `manifest` block of `AndroidManifest.xml`: |
| 35 | |
| 36 | ```xml |
| 37 | <uses-permission android:name |
| 38 | ``` |
| 39 | |
| 40 | We need `BIND_WEARABLE_LISTENER` permission to bind a service. We don't need to prompt user to allow this permission. |
| 41 | |
| 42 | ### Creating Message Receiver |
| 43 | |
| 44 | Create a new file `WearMessageReceiver.kt`: |
| 45 | |
| 46 | ```kotlin |
| 47 | package com.your.package |
| 48 | |
| 49 | import android.content.Intent |
| 50 | import com.google.android.gms.wearable.MessageEvent |
| 51 | import com.google.android.gms.wearable.WearableListenerService |
| 52 | |
| 53 | class WearMessageReceiver : WearableListenerService |
| 54 | override fun onMessageReceived(event: |
| 55 | when (event.path) { |
| 56 | "/open" -> { |
| 57 | val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply |
| 58 | addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 59 | addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) |
| 60 | } |
| 61 | startActivity(intent) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | This service will run in background(even when the app is killed) and listen to `/open` from your watch. When it receives the message, it will open the app. |
| 69 | |
| 70 | ### Registering the Service |
| 71 | |
| 72 | Now we need to tell android about this service. Add the following code in the `application` block of `AndroidManifest.xml`: |
| 73 | |
| 74 | ```xml |
| 75 | <service |
| 76 | android:name=" |
| 77 | android:exported=" |
| 78 | <intent-filter> |
| 79 | <action android:name |
| 80 | <category android:name |
| 81 | <data |
| 82 | android:host=" |
| 83 | android:pathPattern=" |
| 84 | android:scheme=" |
| 85 | </intent-filter> |
| 86 | </service> |
| 87 | ``` |
| 88 | |
| 89 | This code tells android when to invoke the `WearMessageReceiver` service. Make sure `[package name]` is replaced with your package name. |
| 90 | |
| 91 | ### Message Sending Function |
| 92 | |
| 93 | Now we need a function to send messages from the phone to the watch. |
| 94 | |
| 95 | Add this function to `MainActivity.kt` and in `MainActivity` class: |
| 96 | |
| 97 | ```kotlin |
| 98 | suspend fun sendMessageToWatch( |
| 99 | context: Context, |
| 100 | path: String, |
| 101 | data: ByteArray |
| 102 | ) { |
| 103 | withContext(Dispatchers.IO) { |
| 104 | try { |
| 105 | val nodes = Tasks.await(Wearable.getNodeClient |
| 106 | nodes.forEach { node -> |
| 107 | Tasks.await( |
| 108 | Wearable.getMessageClient(context).sendMessage( |
| 109 | node.id, |
| 110 | path, |
| 111 | data, |
| 112 | ) |
| 113 | ) |
| 114 | } |
| 115 | } catch (e: Exception) { |
| 116 | e.printStackTrace() |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | this function is a suspend function which means you need to invoke it inside a coroutine to avoid blocking the main thread. Read more about suspend functions [here](https://kotlinlang.org/docs/composing-suspending-functions.html). |
| 123 | |
| 124 | ## Watch App Setup |
| 125 | |
| 126 | ### Creating the Watch Project |
| 127 | |
| 128 | 1. Go to `Android Studio > File > New > New Project > Wear OS > Empty Wear App` |
| 129 | 2. Keep the bundle identifier same as your phone app |
| 130 | 3. Connect a Wear OS device or create an emulator |
| 131 | |
| 132 | ### Adding Permissions |
| 133 | |
| 134 | Add to the `manifest` block in `AndroidManifest.xml`: |
| 135 | |
| 136 | ```xml |
| 137 | <uses-permission android:name |
| 138 | ``` |
| 139 | |
| 140 | This permission is also a [`normal` permission](https://developer.android.com/guide/topics/permissions/overview#normal) |
| 141 | |
| 142 | ### Creating Message Listener |
| 143 | |
| 144 | Create `MessageListenerService.kt`: |
| 145 | |
| 146 | ```kotlin |
| 147 | class MessageListenerService : WearableListenerService |
| 148 | override fun onMessageReceived(event: |
| 149 | super.onMessageReceived(event) |
| 150 | when (event.path) { |
| 151 | "/open" -> { |
| 152 | val intent = Intent(this |
| 153 | addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 154 | } |
| 155 | startActivity(intent) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | ``` |
| 161 | |
| 162 | This service will run in background(even when the app is killed) and listen to `/open` from your phone. When it receives the message, it will open the app. |
| 163 | |
| 164 | ### Registering Watch Service |
| 165 | |
| 166 | Add to the `application` block in `AndroidManifest.xml`: |
| 167 | |
| 168 | ```xml |
| 169 | <service |
| 170 | android:name=" |
| 171 | android:exported=" |
| 172 | <intent-filter> |
| 173 | <action android:name |
| 174 | <data |
| 175 | android:host=" |
| 176 | android:pathPattern= |
| 177 | android:scheme=" |
| 178 | </intent-filter> |
| 179 | </service> |
| 180 | ``` |
| 181 | |
| 182 | Similar to the phone app, we need to tell android about this service. |
| 183 | |
| 184 | ### Message Sending Function |
| 185 | |
| 186 | Add to `MainActivity.kt`: |
| 187 | |
| 188 | ```kotlin |
| 189 | private suspend fun sendMessageToPhone |
| 190 | val connectedNodes = Wearable.getNodeClient(this |
| 191 | println("Connected nodes: $connectedNodes") |
| 192 | for (node in connectedNodes) { |
| 193 | try { |
| 194 | messageClient.sendMessage(node.id, path, data).await |
| 195 | } catch (e: Exception) { |
| 196 | println("Failed to send message to node ${node.id}: |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | ``` |
| 201 | |
| 202 | This function is a suspend function which means you need to invoke it inside a coroutine to avoid blocking the main thread. Read more about suspend functions [here](https://kotlinlang.org/docs/composing-suspending-functions.html). |
| 203 | |
| 204 | Now, the setup is done. You can now send messages from the watch to the phone and vice versa. |
| 205 | |
| 206 | This is very minimal setup and you can extend it using [`JSONObject`](https://developer.android.com/reference/org/json/JSONObject) or [` |
| 207 | |
| 208 | ## Final Notes |
| 209 | |
| 210 | - Messages are transferred in `ByteArray` format - remember to encode the data in sender device and decode the data in target device |
| 211 | - For cross-platform frameworks, call `sendMessageToWatch` using [MethodChannel |
| 212 | - Name of services should match the name specified in `AndroidManifest.xml` |