androidkotlinwear-osandroid-wear-data-api

How to send String via DataItem


I can't connect my watchface with a handheld app. I want to send a simple text (on click button in the handheld) for understanding how it works. Unfortunately, I'm missing something because I have not any message in the watchface.

My onClickListener in the handheld MainActivity

sendDataItem.setOnClickListener {
            val putDataReq: PutDataRequest = PutDataMapRequest.create("/test").run {
                dataMap.putString("key", "TEST TEXT")
                asPutDataRequest()
            }
            val putDataTask: Task<DataItem> = dataClient.putDataItem(putDataReq)
        }

My override OnDataChanged method that (what I at least want to do) send data to my watchface

override fun onDataChanged(dataEvents: DataEventBuffer) {
            dataEvents.forEach { event ->
                // DataItem changed
                if (event.type == DataEvent.TYPE_CHANGED) {
                    event.dataItem.also { item ->
                        if (item.uri.path.compareTo("/test") == 0) {
                            DataMapItem.fromDataItem(item).dataMap.apply {
                                weatherTemp = getString("key")
                            }
                        }
                    }
                } else if (event.type == DataEvent.TYPE_DELETED) {
                    // DataItem deleted
                }
            }
        }

I want a simple result. As soon I receive data I want to save it to weatherTemp var.


Solution

  • I spend some time on this but finally, I get this working. In my watch face class I have onDataChange method that is receiving data item:

    override fun onDataChanged(dataEvents: DataEventBuffer) {
                dataEvents.forEach { event ->
                    if (event.type == DataEvent.TYPE_CHANGED) {
                        val path = event.dataItem.uri.path
                        event.dataItem.also { item ->
                            if (path.compareTo("/testPath") == 0) {
                                DataMapItem.fromDataItem(item).dataMap.apply {
                                   //I'm changing var that is printed on the watchface screen
                                    testVar = getInt("key").toString()
                                }
                            }
                        }
                    } else if (event.type == DataEvent.TYPE_DELETED) {
                        //DataItem deleted
                    } else {
                        //Unknown data type event
                    }
                }
            }
    

    You cannot forget about:

     Wearable.getDataClient(applicationContext).addListener(this)
    

    in your onCreate method. I'm sending data item from my handheld activity on button press.

    sendDataItem.setOnClickListener {
                putDataMapReq = PutDataMapRequest.create("/testPath")
                putDataMapReq.dataMap.putInt("key", testVar++)
                putDataReq = putDataMapReq.asPutDataRequest()
                putDataReq.setUrgent()
                putDataTask = Wearable.getDataClient(applicationContext).putDataItem(putDataReq)
            }
    

    It's not perfect but at least it's working and I'm able to go ahead. If anything is unclear feel free to write the comment.