Build Wear OS Apps and Connect with Android
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 and open your project in Android Studio (open the android
folder if using cross-platform frameworks).
Phone App Setup
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 permission.
Adding Dependencies
Add the following to your app level gradle file (app/build.gradle
):
Don't forget to press Sync Now
Permissions Setup
Add the following code in the manifest
block of AndroidManifest.xml
:
Creating Message Receiver
Now, we'll write a service that will be android:exported
. Create a new file WearMessageReceiver.kt
in the same folder as MainActivity.kt
:
The above code listens for the /open
message and when received, it opens the app.
Registering the Service
We need to tell our phone about this service. Add the following code in the application
block of AndroidManifest.xml
:
Message Sending Function
This function sends messages from our phone to the watch. Add it to MainActivity.kt
:
Read more on suspend functions here.
Watch App Setup
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.
Creating the Watch Project
- Go to
Android Studio > File > New > New Project > Wear OS > Empty Wear App
- Keep the bundle identifier same as your phone app
- Connect a Wear OS device or create an emulator
Adding Permissions
Just like the phone app, we need to bind a listener and add permissions. Add to the manifest
block in AndroidManifest.xml
:
Creating Message Listener
Create MessageListenerService.kt
:
Registering Watch Service
Add to the application
block in AndroidManifest.xml
:
Message Sending Function
Add to MainActivity.kt
:
Final Notes
- Messages between devices are transferred in
ByteArray
format - remember to encode data in sender device and decode the data in receiver device - For cross-platform frameworks, you can call
sendMessageToWatch
using MethodChannel or equivalent - Name of services should match the name specified in
AndroidManifest.xml