# Class WearableMessage

java.lang.Object
com.codename1.wearable.WearableMessage

public class WearableMessage extends Object

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
  Constructor
  Description
  `WearableMessage(String path)`
  Creates an empty message addressed to a path.

- ## Method Summary

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

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### WearableMessage

    public WearableMessage(String path)

    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

    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`: 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`: the key
    - `value`: 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`: the key
    - `value`: the value

    ###### Returns

    this message, for chaining

  - ### put

    public WearableMessage put(String key, long value)

    Adds a long value.

    ###### Parameters

    - `key`: the key
    - `value`: the value

    ###### Returns

    this message, for chaining

  - ### put

    public WearableMessage put(String key, double value)

    Adds a double value.

    ###### Parameters

    - `key`: the key
    - `value`: the value

    ###### Returns

    this message, for chaining

  - ### put

    public WearableMessage put(String key, boolean value)

    Adds a boolean value.

    ###### Parameters

    - `key`: the key
    - `value`: 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`: the key
    - `value`: 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`: the key
    - `defaultValue`: 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`: the key
    - `defaultValue`: 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`: the key
    - `defaultValue`: 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`: the key
    - `defaultValue`: 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`: the key
    - `defaultValue`: 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`: the key
    - `defaultValue`: 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; `WearableConnection` does it on the way out.

    ###### Returns

    the encoded payload, never null

  - ### 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`: the path the payload arrived on
    - `data`: 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()
    Description copied from class: `Object`
    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())
    Overrides:
      `toString` in class `Object`