How could we pass a list of object between android phone and wear device?

35 Views Asked by At

In phone code, I use Serializable to transfer to ByteArray to be passed in an asset, wear(watch) is able to receive it but in the following code, payload doesn't contain any data that can be transferred back to original data.

var b = ListToPass(listOf("mission 1", "mission 2"))
this.dataMap.putAsset("KEY", Asset.createFromBytes(b.toByteArray()))

fun Serializable.toByteArray(): ByteArray {
    val byteArrayOutputStream = ByteArrayOutputStream()
    val objectOutputStream = ObjectOutputStream(byteArrayOutputStream)
    objectOutputStream.writeObject(this)
    objectOutputStream.flush()
    val result = byteArrayOutputStream.toByteArray()
    byteArrayOutputStream.close()
    objectOutputStream.close()
    return result
  }

  data class ListToPass(
    var test: List<String>
  ) : Serializable

wear code to handle the data:

val payload = DataMapItem.fromDataItem(dataEvent.dataItem)
              .dataMap.getAsset(WearViewModel.MISSION_KEY) 
0

There are 0 best solutions below