public class WearableMessage

  1. Object
  2. 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

public String getPath()Returns the path this message is addressed to.
public List<String> getKeys()Returns the keys carried by this message, in insertion order.
public boolean contains(String key)Returns true if the payload carries a value under the supplied key.
public WearableMessage put(String key, String value)Adds a string value.
public WearableMessage put(String key, int value)Adds an int value.
public WearableMessage put(String key, long value)Adds a long value.
public WearableMessage put(String key, double value)Adds a double value.
public WearableMessage put(String key, boolean value)Adds a boolean value.
public WearableMessage put(String key, byte[] value)Adds a raw byte payload.
public String getString(String key, String defaultValue)Reads a string value.
public int getInt(String key, int defaultValue)Reads an int value.
public long getLong(String key, long defaultValue)Reads a long value.
public double getDouble(String key, double defaultValue)Reads a double value.
public boolean getBoolean(String key, boolean defaultValue)Reads a boolean value.
public byte[] getBytes(String key, byte[] defaultValue)Reads a raw byte payload.
public byte[] toByteArray()
public static WearableMessage fromByteArray(String path, byte[] data)Reconstructs a payload received from the peer.
public String toString()Returns a string representation of the object.

Inherited methods

Constructor details

WearableMessage

public WearableMessage(String path)
Creates an empty message addressed to a path.

Parameters

path String
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

key String
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

key String
the key
value String
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

key String
the key
value int
the value

Returns

this message, for chaining

put

public WearableMessage put(String key, long value)
Adds a long value.

Parameters

key String
the key
value long
the value

Returns

this message, for chaining

put

public WearableMessage put(String key, double value)
Adds a double value.

Parameters

key String
the key
value double
the value

Returns

this message, for chaining

put

public WearableMessage put(String key, boolean value)
Adds a boolean value.

Parameters

key String
the key
value boolean
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

key String
the key
value byte[]
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

key String
the key
defaultValue String
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

key String
the key
defaultValue int
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

key String
the key
defaultValue long
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

key String
the key
defaultValue double
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

key String
the key
defaultValue boolean
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

key String
the key
defaultValue byte[]
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

path String
the path the payload arrived on
data byte[]
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())