Reputation: 2334
I have a watch face which I am looking to send a few strings using the Data Layer. I have followed the guide by adding the service to the manifest and creating the DataLayerListenerService
class.
What am I supposed to do to send data to the wearable from the service? I've done this before using PutDataRequest
in my config activity which works. I now want to periodically send battery stats, weather info etc. to the wearable. How do I do this?
Here is my Class so far:
public class DataLayerListenerService extends WearableListenerService {
private static final String TAG = DataLayerListenerService.class.getSimpleName();
public static final String EXTRAS_PATH = "/extras";
private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
private GoogleApiClient mGoogleApiClient;
public static void LOGD(final String tag, String message) {
if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, message);
}
}
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
LOGD(TAG, "onDataChanged: " + dataEvents);
if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting()) {
ConnectionResult connectionResult = mGoogleApiClient
.blockingConnect(30, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient, "
+ "error code: " + connectionResult.getErrorCode());
return;
}
}
// Loop through the events and send a message back to the node that created the data item.
for (DataEvent event : dataEvents) {
Uri uri = event.getDataItem().getUri();
String path = uri.getPath();
if (EXTRAS_PATH.equals(path)) {
// Get the node id of the node that created the data item from the host portion of
// the uri.
String nodeId = uri.getHost();
// Set the data of the message to be the bytes of the Uri.
byte[] payload = uri.toString().getBytes();
// Send the rpc
Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, DATA_ITEM_RECEIVED_PATH,
payload);
}
}
}
Upvotes: 0
Views: 924
Reputation: 4950
Firstly, create an instance of GoogleApiClient, when you want to make a connection to one of the Google APIs provided in the Google Play services library. You need to create an instance of GoogleApiClient ("Google API Client"). The Google API Client provides a common entry point to all the Google Play services and manages the network connection between the user's device and each Google service.
Define a WearableListenerService
which receives the message. It receives events from other nodes, such as data changes, messages or connectivity events.
Send a message thru MessageApi
, messages are delivered to connected network nodes. Multiple wearable devices can be connected to a user’s handheld device. Each connected device in the network is considered a node. With multiple connected devices, you must consider which nodes receive the messages.
Then implement MessageApi.MessageListener
used with addListener(GoogleApiClient, MessageApi.MessageListener)
to receive message events. Callers wishing to be notified of events in the background should use WearableListenerService
. Then, receive the message and use a LocalBroadcastManager to elaborate the message and display the value on the wear.
Here's a related SO ticket: Send message from wearable to phone and then immediately reply
Upvotes: 1