From Wrist to Hand: Develop a Companion App for Your Wearable Application
galaxy watch applications can support many features and services on the watch hardware, but the watch's smaller size compared to a mobile device carries limitations, such as fewer hardware resources and smaller screen size. to take advantage of mobile hardware, you can develop a companion mobile application for the wearable application. the companion application handles complex and resource-intensive tasks while the wearable application provides a seamless experience. this tutorial uses android studio and kotlin to demonstrate how to create a multi-module project and communicate between a companion mobile application and a wearable application using the wearable data layer api. to test the application, you need a galaxy watch running wear os powered by samsung and a connected galaxy mobile device. prerequisites to develop a wearable application and its companion mobile application, create a multi-module project in android studio: create a new mobile application project. select file > new > new project > phone & tablet > empty activity and define an appropriate name and application id (package name) for the project. in the android studio environment, switch to project view. to create the wearable module, right-click the project name and select new > module > wear os > blank activity. make sure the application id for this module is identical to the mobile application. switch the android studio environment back to android view. to use the wearable data layer api to create a communication channel between the wearable and mobile applications, add the following dependency to the build.gradle file for both modules: dependencies { ... implementation 'com.google.android.gms:play-services-wearable:xx.x.x' } launching the companion application launching the companion mobile application from the wearable application includes the following steps, which can be implemented using the messageclient api: send a message from the wearable application to the mobile application. receive the message in the mobile application and launch an intent. send a message to the companion application a coroutine is needed to send messages from the wearable application to the mobile application, because this task cannot be carried out on the main thread. the coroutine creates a separate background thread on which to complete the work. to implement coroutines in the wearable application: add the following dependencies to its build.gradle file:implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:x.x.x" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:x.x.x" implementation "androidx.lifecycle:lifecycle-extensions:x.x.x" implementation "androidx.lifecycle:lifecycle-runtime-ktx:x.x.x" implementation "androidx.appcompat:appcompat:x.x.x" in the application code, modify the mainactivity class to inherit appcompatactivity() instead of activity(). in the androidmanifest.xml file, in the <application> element, change the android:theme attribute value to "@style/theme.appcompat.noactionbar" or another custom theme. now that you've implemented coroutines, let's develop a way to send a message to the companion application. in the xml file for the wearable module, define a ui with a button. while a mobile device can be connected to multiple wearables, a watch is typically connected to only a single device. therefore, send the message to the first node (connected device) on the watch using the messageclient api. to retrieve a list of the connected nodes, implement the following function: private fun getnodes(): collection<string> { return tasks.await(wearable.getnodeclient(context).connectednodes).map { it.id } } in the mainactivity class of the wearable module, add a listener to the ui button that uses the sendmessage() function of the messageclient api to send a message to the first node retrieved from the getnodes() function. the sendmessage() function requires three parameters: id of the receiving node. message path (specific address) of the receiving node. byte array of the information to be sent to the node. the size must be less than 100 kb. the message path is initialized in the companion object and is needed to receive the message on the mobile application. class mainactivity : appcompatactivity() { private lateinit var binding: activitymainbinding private var transcriptionnodeid: string? = null override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) binding = activitymainbinding.inflate(layoutinflater) setcontentview(binding.root) binding.deploybutton.setonclicklistener { lifecyclescope.launch(dispatchers.io){ transcriptionnodeid = getnodes().first()?.also { nodeid-> val sendtask: task<*> = wearable.getmessageclient(applicationcontext).sendmessage( nodeid, message_path, "deploy".tobytearray() //send your desired information here ).apply { addonsuccesslistener { log.d(tag, "onsuccess") } addonfailurelistener { log.d(tag, "onfailure") } } } } } } } companion object{ private const val tag = "mainwearactivity" private const val message_path = "/deploy" } listen for messages in the companion application a companion mobile application can be notified of incoming messages from the wearable application in two ways: extending the wearablelistenerservice class to create a service that listens for incoming messages even when the application is in the background implementing the messageclient.onmessagereceivedlistener interface in the mainactivity class of the mobile application since we want to react to the message by launching the companion application even when it is not running in the foreground, create a separate class that extends the wearablelistenerservice class. to receive messages from the wearable application, implement the following onmessagereceived() function: class phonelistenerservice: wearablelistenerservice() { override fun onmessagereceived(messageevent: messageevent) { log.d(tag, "onmessagereceived(): $messageevent") log.d(tag, string(messageevent.data)) if (messageevent.path == message_path) { val startintent = intent(this, mainactivity::class.java).apply { addflags(intent.flag_activity_new_task) putextra("messagedata", messageevent.data) } startactivity(startintent) } } companion object{ private const val tag = "phonelistenerservice" private const val message_path = "/deploy" } } in the androidmanifest.xml file of the mobile application, add the service and an intent filter for the listener service: <service android:name=".phonelistenerservice" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.wearable.message_received" /> <data android:host="*" android:pathprefix="/" android:scheme="wear" /> </intent-filter> </service> to test the project, build both applications and run them on your galaxy watch and mobile device. when you tap the ui button on the watch, the companion application launches on the mobile device. conclusion this tutorial has demonstrated how the wearable data layer api enables you to implement seamless communication between a galaxy watch running wear os powered by samsung and its connected mobile device. in addition to launching a companion application, the functions of the messageclient api can also be used to delegate complex tasks to the mobile device, creating more seamless user experiences for wearable applications. for more information about implementing communication between watch and mobile devices, see send and receive messages on wear. if you have questions about or need help with the information in this tutorial, you can share your queries on the samsung developers forum. for more specialized support, you can contact us through samsung developer support. stay tuned for the next blog on wearable and mobile communication.