Search code examples
androidandroid-serviceandroid-manifestlistenerwear-os

WearableListenerService onCreate and onDataChanged not getting called


I'm trying to send data using the DataClient from a phone to a watch.

Things I looked out for:

  • same package name
  • no build flavors on both modules
  • added service to the wear modules manifest
  • same path prefix
  • same signing config

I tried this sample project and copied parts over to my project. I just can't find any issues with it.

The sample project ran fine on my hardware, interestingly enough it wasn't working in the emulator. Therefore I tested my app also only with my hardware. (Pixel 6 Pro & Pixel Watch)

The sending data part seems to be working, as it behaves the same way as the sample project does.

How I send data from the phone:

class WearDataManager(val context: Context) {

private val dataClient by lazy { Wearable.getDataClient(context) }

companion object {
    private const val CLIENTS_PATH = "/clients"
    private const val CLIENT_LIST_KEY = "clientlist"
}

fun sendClientList(clientList: MutableList<String>) {
    GlobalScope.launch {
        try {
            val request = PutDataMapRequest.create(CLIENTS_PATH).apply {
                dataMap.putStringArray(CLIENT_LIST_KEY, arrayOf("clientList, test"))
            }
                .asPutDataRequest()
                .setUrgent()

            val result = dataClient.putDataItem(request).await()

            Log.d("TAG", "DataItem saved: $result")
        } catch (cancellationException: CancellationException) {
            throw cancellationException
        } catch (exception: Exception) {
            Log.d("TAG", "Saving DataItem failed: $exception")
        }
    }
}
}

This is how I'm receiving data on the watch:

class WearableListenerService: WearableListenerService() {

companion object {
    const val CLIENTS_PATH = "/clients"
}

override fun onCreate() {
    super.onCreate()
    Log.d("testing", "STARTED SERVICE")
}

override fun onDataChanged(dataEvents: DataEventBuffer) {
    super.onDataChanged(dataEvents)

    Log.d("testing", "RECEIVED $dataEvents")
   
}
}

Surprisingly "STARTED SERVICE" does not appear in the log when I start the app on the watch. For my understanding that means that the system isn't aware of the listeners existance and didn't register it. So something must be wrong with the manifest below.

This is the service inside the manifest on the watch:

<service android:name=".wear.communication.WearableListenerService"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
        <data
            android:host="*"
            android:pathPrefix="/clients"
            android:scheme="wear" />
    </intent-filter>
</service>

What am I missing here?


Solution

  • Turns out the sending part was the culprit after all. Be careful what scope you use or if you even want to use one at all. This function is being called inside of a worker in my code so it isn't an issue.

    I completely modified the demo project above and with the help of this I found out why it wasn't working.

    This is the working solution:

     fun sendClientList(clientList: MutableList<String>) {
        val request = PutDataMapRequest.create(CLIENTS_PATH).apply {
            dataMap.putStringArray(CLIENT_LIST_KEY, arrayOf(clientList.joinToString()))
        }
            .asPutDataRequest()
            .setUrgent()
    
        val result = dataClient.putDataItem(request)
        Log.d("TAG", "DataItem saved: $result")
    }