Wear - getting IndexOutOfBounds exception from DataMap.fromByteArray(messageEvent.getData());

谁都会走 提交于 2019-12-12 01:56:08

问题


In my app, I want to use the MessageAPI to communicate between my phone and my wear device. To send data, I use DataMap:

Sending a message from my phone:

MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                googleApiClient, nodes.get(0), SharedConstants.Paths.PATH_WEAR_NOTIFICATION, dataMap.toByteArray()).await();

Reading the message on the wear device (WearableListenerService, onMessageReceived()):

    byte[] data = messageEvent.getData();
    DataMap dataMap = DataMap.fromByteArray(data); <--- crashes 

I get the exception:

 FATAL EXCEPTION: WearableListenerService
    Process: com.aaa.bbb, PID: 20375
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at com.google.android.gms.internal.pb.a(Unknown Source)
            at com.google.android.gms.internal.pb.a(Unknown Source)
            at com.google.android.gms.wearable.DataMap.fromByteArray(Unknown Source)
            at com.aaa.bbb.services.NotificationWearableListenerService.onMessageReceived(NotificationWearableListenerService.java:74)
            at com.google.android.gms.wearable.WearableListenerService$a$2.run(Unknown Source)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)

However, the byte array is not empty.

Does anyone know why can this happen?

Edit:

After testing this a bit more, I noticed that once I remove an image from the payload, the crash stopped. Using the same payload is working when using the DataAPI. Is the limitation for the MessageAPI specified in the documentation 100 kilobit or kilobyte?

来源:https://stackoverflow.com/questions/27669318/wear-getting-indexoutofbounds-exception-from-datamap-frombytearraymessageeven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!