Class WearableMessage
A payload addressed to a path, used both for live messages and for replicated data.
The path is what the receiving side matches on -- "/steps", "/workout/start" -- and works
like a URL path, so give related payloads a common prefix. Values are the primitive types every
wearable transport can carry natively on both platforms: string, int, long, double, boolean and
raw bytes.
WearableMessage m = new WearableMessage("/steps")
.put("count", 8412)
.put("goalReached", true);
WearableConnection.putData(m);
Reads name a default, so a peer running an older version of your app that never sent a key gets a sane value rather than an exception. That matters more than usual here: the two apps are updated independently and can be different versions of each other for a long time.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if the payload carries a value under the supplied key.static WearableMessagefromByteArray(String path, byte[] data) Reconstructs a payload received from the peer.booleangetBoolean(String key, boolean defaultValue) Reads a boolean value.byte[]Reads a raw byte payload.doubleReads a double value.intReads an int value.getKeys()Returns the keys carried by this message, in insertion order.longReads a long value.getPath()Returns the path this message is addressed to.Reads a string value.Adds a boolean value.Adds a raw byte payload.Adds a double value.Adds an int value.Adds a long value.Adds a string value.byte[]Serializes the payload to the compact form the platform bridges carry.toString()Returns a string representation of the object.
-
Constructor Details
-
WearableMessage
Creates an empty message addressed to a path.
Parameters
path: the path the peer matches on, conventionally starting with/
Throws
IllegalArgumentException: if the path is null or empty
-
-
Method Details
-
getPath
Returns the path this message is addressed to.
Returns
the path
-
getKeys
-
contains
Returns true if the payload carries a value under the supplied key.
Parameters
key: the key to look for
Returns
true if the key is present
-
put
Adds a string value.
Parameters
key: the keyvalue: the value; a null value removes the key
Returns
this message, for chaining
-
put
Adds an int value.
Parameters
key: the keyvalue: the value
Returns
this message, for chaining
-
put
Adds a long value.
Parameters
key: the keyvalue: the value
Returns
this message, for chaining
-
put
Adds a double value.
Parameters
key: the keyvalue: the value
Returns
this message, for chaining
-
put
Adds a boolean value.
Parameters
key: the keyvalue: the value
Returns
this message, for chaining
-
put
Adds a raw byte payload. Keep it small: a message is delivered over a low-bandwidth link and the platforms reject oversized payloads outright. Use [WearableConnection#transferFile(String,String,byte[])] for anything substantial.
Parameters
key: the keyvalue: the bytes; a null value removes the key
Returns
this message, for chaining
-
getString
-
getInt
Reads an int value. Accepts any numeric value, so a peer that sent a long or a double still reads back sensibly.
Parameters
key: the keydefaultValue: returned when the key is absent or holds a non-numeric type
Returns
the value, or the default
-
getLong
Reads a long value. Accepts any numeric value.
Parameters
key: the keydefaultValue: returned when the key is absent or holds a non-numeric type
Returns
the value, or the default
-
getDouble
Reads a double value. Accepts any numeric value.
Parameters
key: the keydefaultValue: returned when the key is absent or holds a non-numeric type
Returns
the value, or the default
-
getBoolean
Reads a boolean value.
Parameters
key: the keydefaultValue: returned when the key is absent or holds another type
Returns
the value, or the default
-
getBytes
Reads a raw byte payload.
Parameters
key: the keydefaultValue: returned when the key is absent or holds another type
Returns
the value, or the default
-
toByteArray
public byte[] toByteArray()Serializes the payload to the compact form the platform bridges carry. Application code does not normally call this;
WearableConnectiondoes it on the way out.Returns
the encoded payload, never null
-
fromByteArray
Reconstructs a payload received from the peer. Application code does not normally call this;
WearableConnectiondoes it on the way in.Parameters
path: the path the payload arrived ondata: the encoded payload, may be null or empty for a payload with no values
Returns
the decoded message, never null; a payload this build cannot parse decodes to an empty message on the same path rather than throwing
-
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
-