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