Pablo Garcia
Pablo Garcia

Reputation: 381

Receive dataItem in handheld from wear Android Kotlin

I have an app with two modules, app and wearable.

I need to send data from handheld to wear, and from wear to handheld.

This is my code:

Wear MainActivity

class MainActivity : Activity(), DataClient.OnDataChangedListener {

private lateinit var binding: ActivityMainBinding
private val dataClient by lazy { Wearable.getDataClient(this) }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

}

override fun onResume() {
    super.onResume()
    dataClient.addListener(this)
    addClickListener()
}

override fun onPause() {
    super.onPause()
    dataClient.removeListener(this)
}

override fun onDataChanged(dataEvent: DataEventBuffer) {
    Toast.makeText(this, "Hello World!", Toast.LENGTH_LONG).show()
}

private fun addClickListener() {
    binding.buttonWear.setOnClickListener {
        sendDataItem(System.currentTimeMillis())
    }
}

private fun sendDataItem(item: Long) {
    try {
        val request = PutDataMapRequest.create("/wearable_path").apply {
            dataMap.putString("wearable_key", item.toString())
        }
            .asPutDataRequest()
            .setUrgent()

        dataClient.putDataItem(request).addOnSuccessListener {
            android.util.Log.d(":::","Success")
        }.addOnCanceledListener {
            android.util.Log.d(":::","Cancel")
        }.addOnFailureListener {
            android.util.Log.d(":::","Failure")
        }

    } catch (cancellationException: CancellationException) {
        cancellationException.printStackTrace()
    } catch (exception: Exception) {
        exception.printStackTrace()
    }
}

}

Handheld MainFragment.

class MainFragment: Fragment(), DataClient.OnDataChangedListener {

private val dataClient by lazy { Wearable.getDataClient(requireContext()) }
private val button by lazy { requireActivity().findViewById<Button>(R.id.button)}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_main, container, false)
}

override fun onResume() {
    super.onResume()
    dataClient.addListener(this)
    addClickListener()
}

override fun onPause() {
    super.onPause()
    dataClient.removeListener(this)
}

override fun onDataChanged(p0: DataEventBuffer) {
    Toast.makeText(requireContext(), "Hello World!", Toast.LENGTH_LONG).show()

}

private fun addClickListener() {
    button?.setOnClickListener {
        sendDataItem(System.currentTimeMillis())
    }
}

private fun sendDataItem(item: Long) {
    try {
        val request = PutDataMapRequest.create("/wearable_path").apply {
            dataMap.putString("wearable_key", item.toString())
        }.asPutDataRequest().setUrgent()

        dataClient.putDataItem(request).addOnSuccessListener {
            android.util.Log.d(":::","Success")
        }.addOnCanceledListener {
            android.util.Log.d(":::","Cancel")
        }.addOnFailureListener {
            android.util.Log.d(":::","Failure")
        }
    } catch (cancellationException: CancellationException) {
        cancellationException.printStackTrace()
    } catch (exception: Exception) {
        exception.printStackTrace()
    }
}
}

The problem is, when I click the button in handheld, the data is received in both devices and show the toast, but when I click the button in the wearable device, onDataChanged in handlend (fragment) is not called, and the toast is shown in the wear but not in the handheld.

Any suggest?

Upvotes: 0

Views: 442

Answers (2)

Mollykobinson
Mollykobinson

Reputation: 1

I also had trouble for the android code, then waited for help but it didn't work, I had to pay for a professional.

Upvotes: -1

Pablo Garcia
Pablo Garcia

Reputation: 381

I found the solution. I was signing the app but not singning the wear module.

I must sign in the same way the app and the wear module.

Be careful with the applicationId, it must be the same too.

Upvotes: 0

Related Questions