---
title: "Build Wear OS Apps and Connect with Android"
summary: "Create Wear OS app and integrate them with Android, covering setup, permissions, and data sync."
---

## Initial Setup

Start by creating an android app using your preferred framework:

- Flutter <span className="text-gray-400">(the supreme)</span>
- React Native <span className="text-gray-400">(soydevs)</span>
- Kotlin <span className="text-gray-400">(chad)</span>
- Android Views <span className="text-gray-400">(mentally ill)</span>
- Any other <span className="text-gray-400">(idk)</span>

Open your android project in Android Studio (open the `android` folder if using cross-platform frameworks).

## Phone App Setup

### Adding Dependencies

Add the following to your app level gradle file (`app/build.gradle`):

```kotlin
implementation("com.google.android.gms:play-services-wearable:19.0.0")
```

### Permissions Setup

Add the following code in the `manifest` block of `AndroidManifest.xml`:

```xml
<uses-permission android:name="com.google.android.wearable.permission.BIND_WEARABLE_LISTENER" />
```

### Creating Message Receiver

Create a new file `WearMessageReceiver.kt` in the same folder as `MainActivity.kt`:

```kotlin
package com.your.package

import android.content.Intent
import com.google.android.gms.wearable.MessageEvent
import com.google.android.gms.wearable.WearableListenerService

class WearMessageReceiver : WearableListenerService() {
    override fun onMessageReceived(event: MessageEvent) {
        when (event.path) {
            "/open" -> {
                val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                }
                startActivity(intent)
            }
        }
    }
}
```

### Registering the Service

Add the following code in the `application` block of `AndroidManifest.xml`:

```xml
<service
    android:name="[package name].WearMessageReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="*"
            android:pathPattern=".*"
            android:scheme="wear" />
    </intent-filter>
</service>
```

### Message Sending Function

Add this function to `MainActivity.kt`:

```kotlin
suspend fun sendMessageToWatch(
        context: Context,
        path: String,
        data: ByteArray = ByteArray(0)
    ) {
        withContext(Dispatchers.IO) {
            try {
                val nodes = Tasks.await(Wearable.getNodeClient(context).connectedNodes)
                nodes.forEach { node ->
                    Tasks.await(
                        Wearable.getMessageClient(context).sendMessage(
                            node.id,
                            path,
                            data,
                        )
                    )
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
```

## Watch App Setup

### Creating the Watch Project

1. Go to `Android Studio > File > New > New Project > Wear OS > Empty Wear App`
2. Keep the bundle identifier same as your phone app
3. Connect a Wear OS device or create an emulator

### Adding Permissions

Add to the `manifest` block in `AndroidManifest.xml`:

```xml
<uses-permission android:name="com.google.android.wearable.permission.BIND_WEARABLE_LISTENER" />
```

### Registering Watch Service

Add to the `application` block in `AndroidManifest.xml`:

```xml
<service
    android:name=".MessageListenerService"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data
                    android:host="*"
                    android:pathPattern=".*"
                    android:scheme="wear" />
        </intent-filter>
</service>
```

### Creating Message Listener

Create `MessageListenerService.kt`:

```kotlin
class MessageListenerService : WearableListenerService() {
    override fun onMessageReceived(event: MessageEvent) {
        super.onMessageReceived(event)
        when (event.path) {
            "/open" -> {
                val intent = Intent(this, MainActivity::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                startActivity(intent)
            }
        }
    }
}
```

### Message Sending Function

Add to `MainActivity.kt`:

```kotlin
private suspend fun sendMessageToPhone(path: String, data: ByteArray) {
    val connectedNodes = Wearable.getNodeClient(this).connectedNodes.await()
    println("Connected nodes: $connectedNodes")
    for (node in connectedNodes) {
        try {
            messageClient.sendMessage(node.id, path, data).await()
        } catch (e: Exception) {
            println("Failed to send message to node ${node.id}: ${e.message}")
        }
    }
}
```

## Final Notes

- Messages are transferred in `ByteArray` format - remember to decode the data in target device
- For cross-platform frameworks, call `sendMessageToWatch` using MethodChannel or equivalent
- Name of services should match the name specified in `AndroidManifest.xml`