public interface WearableBridge

Internal service-provider interface implemented by each platform port to carry the com.codename1.wearable API onto the native phone-to-watch transport (Apple’s WCSession or Google’s Wearable Data Layer).

Application code never touches this interface – it is obtained by the com.codename1.wearable framework from com.codename1.ui.Display#getWearableBridge() and driven through the public com.codename1.wearable.WearableConnection API. The base implementation returns null, which is why the public API degrades to a harmless no-op on the simulator and on ports with no paired device (so application code needs no platform if statements).

Payloads cross this interface as the opaque bytes produced by com.codename1.wearable.WearableMessage#toByteArray(), so a port only has to move bytes and never has to understand the value model. Incoming traffic is pushed back the other way by calling the static entry points on com.codename1.wearable.WearableConnection (deliverMessage, deliverReply, deliverDataChanged, deliverDataRemoved, notifyStateChanged), which take care of EDT dispatch and of queueing across a cold start.

Methods

public abstract boolean isSupported()Returns true when the PLATFORM provides the transport and this app may use it – not whether a counterpart device exists.
public abstract boolean isPaired()Returns true when a counterpart device is paired with this one, whether or not it is currently switched on or in range.
public abstract boolean isReachable()Returns true when the peer app can receive a live message right now.
public abstract boolean isCompanionAppInstalled()Returns true when the counterpart app is actually installed on the paired device.
public abstract String[] getConnectedNodes()Returns the currently connected counterpart devices, one entry per device, each formatted as id \t displayName \t 1|0 where the trailing flag is whether the device is nearby.
public abstract void sendMessage(String path, byte[] payload, int replyToken)Sends a live message to the peer app, delivered only if it is reachable.
public abstract void sendReply(int replyToken, byte[] payload)Answers a message the peer sent with a reply token.
public abstract void putData(String path, byte[] payload)Publishes or replaces the replicated value at a path.
public abstract byte[] getData(String path)Returns the replicated value at a path, as published by either side.
public abstract void removeData(String path)Removes the replicated value at a path.
public abstract String[] getDataPaths()Returns every path that currently holds a replicated value.
public abstract void transferFile(String path, String name, byte[] contents)Transfers a file to the peer in the background.

Method details

isSupported

public abstract boolean isSupported()

Returns true when the PLATFORM provides the transport and this app may use it – not whether a counterpart device exists.

An implementation should answer for the API, not for the pairing: Apple’s bridge reports WCSession availability whether or not a watch is paired, and an implementation that folded pairing into this answer would make the whole public API inert on a phone whose watch merely happens to be unpaired right now. Pairing is isPaired()’s question and reachability is isReachable()’s.

False makes the whole public API inert, so return false only when there is no transport.

Returns

true if the platform’s wearable transport is available to this app

isPaired

public abstract boolean isPaired()
Returns true when a counterpart device is paired with this one, whether or not it is currently switched on or in range.

Returns

true if a counterpart device is paired

isReachable

public abstract boolean isReachable()
Returns true when the peer app can receive a live message right now. This is the condition sendMessage needs; replicated data does not.

Returns

true if the peer app is reachable

isCompanionAppInstalled

public abstract boolean isCompanionAppInstalled()
Returns true when the counterpart app is actually installed on the paired device. A paired watch with no watch app installed is the common case worth telling the user about.

Returns

true if the peer app is installed

getConnectedNodes

public abstract String[] getConnectedNodes()
Returns the currently connected counterpart devices, one entry per device, each formatted as id \t displayName \t 1|0 where the trailing flag is whether the device is nearby. The flat string form keeps the interface to primitives so native ports do not have to construct Java objects.

Returns

the connected nodes, never null; an empty array when nothing is connected

sendMessage

public abstract void sendMessage(String path, byte[] payload, int replyToken)
Sends a live message to the peer app, delivered only if it is reachable.

Parameters

path String
the path the peer matches on
payload byte[]
the encoded payload
replyToken int
a positive token to answer with WearableConnection.deliverReply when the sender wants a reply, or 0 when it does not

sendReply

public abstract void sendReply(int replyToken, byte[] payload)
Answers a message the peer sent with a reply token.

Parameters

replyToken int
the token that arrived with the request
payload byte[]
the encoded reply payload

putData

public abstract void putData(String path, byte[] payload)
Publishes or replaces the replicated value at a path. The value must survive this app being killed and must reach the peer whenever it next runs.

Parameters

path String
the path to publish under
payload byte[]
the encoded payload

getData

public abstract byte[] getData(String path)
Returns the replicated value at a path, as published by either side.

Parameters

path String
the path to read

Returns

the encoded payload, or null when nothing is published at that path

removeData

public abstract void removeData(String path)
Removes the replicated value at a path.

Parameters

path String
the path to clear

getDataPaths

public abstract String[] getDataPaths()
Returns every path that currently holds a replicated value.

Returns

the published paths, never null

transferFile

public abstract void transferFile(String path, String name, byte[] contents)
Transfers a file to the peer in the background. Delivery may happen long after this returns, including after this app has exited.

Parameters

path String
the path the peer matches on
name String
the file name to present to the peer
contents byte[]
the file bytes