0

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?

Pablo Garcia
  • 361
  • 6
  • 23

2 Answers2

0

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.

Pablo Garcia
  • 361
  • 6
  • 23
-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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 28 '22 at 10:41