问题
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