| 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 | Start by creating an android app, doesn't matter what you use to make it be it Flutter<span className="text-gray-400 |
| 7 | |
| 8 | |
| 9 | Open your android project in Android Studio(open the `android` folder if you are using cross platform frameworks). |
| 10 | |
| 11 | First, we need to bind a listener so we can listen to messages sent by our watch even when our app is closed/killed. So, we first need to add permissions for it and then package to actually implement it. You don't need to prompt user for allowing the permission, it will be done automatically. |
| 12 | |
| 13 | Add the following code in your app level gradle file(`app/build.gradle`): |
| 14 | |
| 15 | ```kotlin |
| 16 | implementation("com.google.android.gms:play-services-wearable:19.0.0") |
| 17 | ``` |
| 18 | |
| 19 | Now, we need to add permission. Open `AndroidManifest.xml` and add the following code in `manifest` block: |
| 20 | |
| 21 | ```xml |
| 22 | <uses-permission android:name |
| 23 | ``` |
| 24 | |
| 25 | Now, time for some kotlin code. We will write a [service](https://developer.android.com/guide/topics/manifest/service-element) which will be ` |
| 26 | |
| 27 | Create a new file `WearMessageReceiver.kt` in same folder as `MainActivity.kt` and add the following code: |
| 28 | |
| 29 | ```kotlin |
| 30 | package com.your.package |
| 31 | |
| 32 | |
| 33 | import android.content.Intent |
| 34 | import com.google.android.gms.wearable.MessageEvent |
| 35 | import com.google.android.gms.wearable.WearableListenerService |
| 36 | |
| 37 | class WearMessageReceiver : WearableListenerService |
| 38 | override fun onMessageReceived(event: |
| 39 | when (event.path) { |
| 40 | "/open" -> { |
| 41 | val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply |
| 42 | addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 43 | addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) |
| 44 | } |
| 45 | startActivity(intent) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | The above code listens for `/open` message and when it receives it, it will open the app. |
| 53 | |
| 54 | Now, we need to tell the android about this service. Open `AndroidManifest.xml` and add the following code in `application` block: |
| 55 | |
| 56 | ```xml |
| 57 | <service |
| 58 | android:name=" |
| 59 | android:exported=" |
| 60 | <intent-filter> |
| 61 | <action android:name |
| 62 | <category android:name |
| 63 | <data |
| 64 | android:host=" |
| 65 | android:pathPattern=" |
| 66 | android:scheme=" |
| 67 | </intent-filter> |
| 68 | </service> |
| 69 | ``` |
| 70 | |
| 71 | Now, we need to write a function which can send messages from our phone to watch. |
| 72 | |
| 73 | Open `MainActivity.kt` and inside the `MainActivity` class, paste the following code: |
| 74 | |
| 75 | ```kotlin |
| 76 | suspend fun sendMessageToWatch( |
| 77 | context: Context, |
| 78 | path: String, |
| 79 | data: ByteArray |
| 80 | ) { |
| 81 | withContext(Dispatchers.IO) { |
| 82 | try { |
| 83 | val nodes = Tasks.await(Wearable.getNodeClient |
| 84 | // it is recommended to filter out nodes with `isNearby = false` |
| 85 | nodes.forEach { node -> |
| 86 | Tasks.await( |
| 87 | Wearable.getMessageClient(context).sendMessage( |
| 88 | node.id, |
| 89 | path, |
| 90 | data, |
| 91 | ) |
| 92 | ) |
| 93 | } |
| 94 | } catch (e: Exception) { |
| 95 | e.printStackTrace() |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | ``` |
| 100 | |
| 101 | Messages between the devices are transferred in `ByteArray` format so keep that in mind that you will have to decode the data in watch. |
| 102 | |
| 103 | Phone setup is now done, now time for Watch. |
| 104 | |
| 105 | Here, you will need a Wear OS device connected to your phone and connected to your PC via wireless debugging. |
| 106 | |
| 107 | Don't have a Wear OS device? Don't worry, create a new emulator and run it and follow the setup instructions. It will connect to your phone(unlike apple watch sim). |
| 108 | |
| 109 | Create a new Wear OS app and name it anything. |
| 110 | |
| 111 | `Android Studio > File > New > New Project > Wear OS > Empty Wear App`. Keep the bundle identifier same as your phone app. |
| 112 | |
| 113 | Now, same as phone, we need to bind a listener and add permission. Open `AndroidManifest.xml` and add the following code in `manifest` block: |
| 114 | |
| 115 | ```xml |
| 116 | <uses-permission android:name |
| 117 | ``` |
| 118 | |
| 119 | and following code in `application` block: |
| 120 | |
| 121 | ```xml |
| 122 | <service |
| 123 | android:name=" |
| 124 | android:exported=" |
| 125 | <intent-filter> |
| 126 | <action android:name |
| 127 | <data |
| 128 | android:host=" |
| 129 | android:pathPattern= |
| 130 | android:scheme=" |
| 131 | </intent-filter> |
| 132 | </service> |
| 133 | ``` |
| 134 | |
| 135 | Now, create a new kotlin file `MessageListenerService.kt` and add the following code: |
| 136 | |
| 137 | ```kotlin |
| 138 | // make sure the name of this class matches the one in AndroidManifest.xml |
| 139 | class MessageListenerService : WearableListenerService |
| 140 | override fun onMessageReceived(event: |
| 141 | super.onMessageReceived(event) |
| 142 | when (event.path) { |
| 143 | "/boot" -> { |
| 144 | val intent = Intent(this |
| 145 | addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 146 | } |
| 147 | startActivity(intent) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | } |
| 153 | ``` |
| 154 | |
| 155 | and now, in `MainActivity.kt` add the following code to send messages from watch to phone: |
| 156 | |
| 157 | ```kotlin |
| 158 | private suspend fun |
| 159 | val connectedNodes = Wearable.getNodeClient(this |
| 160 | println("Connected nodes: $connectedNodes") |
| 161 | for (node in connectedNodes) { |
| 162 | try { |
| 163 | messageClient.sendMessage(node.id, path, data).await |
| 164 | } catch (e: Exception) { |
| 165 | println("Failed to send message to node ${node.id}: |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | ``` |
| 170 | |
| 171 | |
| 172 | Now, your setup is ready to send messages back and forth between the devices. |
| 173 | |
| 174 | You will need to use Jetpack Compose to create wear os app but you can call the `sendMessageToWatch` function using MethodChannel or equivalent in your cross platform framework. |
| 175 | |