public class WearableMessage
- Object
- 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.
Constructors
public WearableMessage(String path) | Creates an empty message addressed to a path. |
Methods
Inherited methods
Constructor details
WearableMessage
public WearableMessage(String path)Creates an empty message addressed to a path.
Parameters
pathString- the path the peer matches on, conventionally starting with
/
Throws
IllegalArgumentException- if the path is null or empty
Method details
getPath
public String getPath()Returns the path this message is addressed to.
Returns
the path
getKeys
public List<String> getKeys()Returns the keys carried by this message, in insertion order.
Returns
the keys present in the payload
contains
public boolean contains(String key)Returns true if the payload carries a value under the supplied key.
Parameters
keyString- the key to look for
Returns
true if the key is present
put
public WearableMessage put(String key, String value)Adds a string value.
Parameters
keyString- the key
valueString- the value; a null value removes the key
Returns
this message, for chaining
put
public WearableMessage put(String key, int value)Adds an int value.
Parameters
keyString- the key
valueint- the value
Returns
this message, for chaining
put
public WearableMessage put(String key, long value)Adds a long value.
Parameters
keyString- the key
valuelong- the value
Returns
this message, for chaining
put
public WearableMessage put(String key, double value)Adds a double value.
Parameters
keyString- the key
valuedouble- the value
Returns
this message, for chaining
put
public WearableMessage put(String key, boolean value)Adds a boolean value.
Parameters
keyString- the key
valueboolean- the value
Returns
this message, for chaining
put
public WearableMessage put(String key, byte[] value)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
keyString- the key
valuebyte[]- the bytes; a null value removes the key
Returns
this message, for chaining
getString
public String getString(String key, String defaultValue)Reads a string value.
Parameters
keyString- the key
defaultValueString- returned when the key is absent or holds another type
Returns
the value, or the default
getInt
public int getInt(String key, int defaultValue)Reads an int value. Accepts any numeric value, so a peer that sent a long or a double still
reads back sensibly.
Parameters
keyString- the key
defaultValueint- returned when the key is absent or holds a non-numeric type
Returns
the value, or the default
getLong
public long getLong(String key, long defaultValue)Reads a long value. Accepts any numeric value.
Parameters
keyString- the key
defaultValuelong- returned when the key is absent or holds a non-numeric type
Returns
the value, or the default
getDouble
public double getDouble(String key, double defaultValue)Reads a double value. Accepts any numeric value.
Parameters
keyString- the key
defaultValuedouble- returned when the key is absent or holds a non-numeric type
Returns
the value, or the default
getBoolean
public boolean getBoolean(String key, boolean defaultValue)Reads a boolean value.
Parameters
keyString- the key
defaultValueboolean- returned when the key is absent or holds another type
Returns
the value, or the default
getBytes
public byte[] getBytes(String key, byte[] defaultValue)Reads a raw byte payload.
Parameters
keyString- the key
defaultValuebyte[]- returned when the key is absent or holds another type
Returns
the value, or the default
toByteArray
public byte[] toByteArray()fromByteArray
public static WearableMessage fromByteArray(String path, byte[] data)Reconstructs a payload received from the peer. Application code does not normally call this;
WearableConnection does it on the way in.Parameters
pathString- the path the payload arrived on
databyte[]- 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
public String toString()Returns 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())