Juan Vazquez
Juan Vazquez

Reputation: 71

WearableListenerService doesn't receive data from mobile app

im trying to receive a token in my wear app from my mobile app, the dataR request y sent successfully but it never arrives on my service onDataChange method. I checked that the node is connected correctly.

Update:

Now I send my data and is catched but in Mobile App Service instead Wear App service

Sen Data Method in my App:

private fun sendData(data: String, dataClient: DataClient){
    viewLifecycleOwner.lifecycleScope.launch {
        try {
            val request = PutDataMapRequest.create("/data-path").apply {
                dataMap.putString("DATA_KEY", data)
            }
                .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")
        }
    }
}

My Service in Wear App

class WearOsWearableListenerService : WearableListenerService() {
override fun onDataChanged(dataEvents: DataEventBuffer) {
    for (event in dataEvents) {
        if (event.type == DataEvent.TYPE_CHANGED) {
            val item = event.dataItem
            if (item.uri.path?.compareTo("/data-path") == 0) {
                val dataMap = DataMapItem.fromDataItem(item).dataMap
                dataMap.getInt("DATA_KEY")

                val intent = Intent("com.pkg.wearable.DATA_CHANGED")
                intent.putExtra("TOKEN_DATA",  dataMap.getInt("DATA_KEY"))
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent)

            }
        }
    }
}

}

And this is my service conf in wear module Manifest

<service
        android:name="com.pkg.wearable.services.WearOsWearableListenerService"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data
                android:host="*"
                android:pathPrefix="/start-activity"
                android:scheme="wear" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <data
                android:host="*"
                android:pathPrefix="/data-path"
                android:scheme="wear" />
        </intent-filter>
    </service>

Is the same service config and class for mobile app just I changed the class name

As Additional info my

Wear device is an emulator Wear Os Square API 33

Phone device is a physical device - Nubia Red Magic 8s pro API 33

Upvotes: 0

Views: 84

Answers (0)

Related Questions