
Android Wear Docs, Release 1.1
object that contains a golf course hole number and the distances to the front, middle, and back pin locations.
The receiving side can use the path to identify the origin of the data.
// Connect to the data layer when the Activity starts
@Override
protected void onStart() {
super.onStart();
googleClient.connect();
}
// Send a data object when the data layer connection is successful.
@Override
public void onConnected(Bundle connectionHint) {
String WEARABLE_DATA_PATH = "/wearable_data";
// Create a DataMap object and send it to the data layer
DataMap dataMap = new DataMap();
dataMap.putLong("time", new Date().getTime());
dataMap.putString("hole", "1");
dataMap.putString("front", "250");
dataMap.putString("middle", "260");
dataMap.putString("back", "270");
//Requires a new thread to avoid blocking the UI
new SendToDataLayerThread(WEARABLE_DATA_PATH, dataMap).start();
}
// Disconnect from the data layer when the Activity stops
@Override
protected void onStop() {
if (null != googleClient && googleClient.isConnected()) {
googleClient.disconnect();
}
super.onStop();
}
// Placeholders for required connection callbacks
@Override
public void onConnectionSuspended(int cause) { }
@Override
public void onConnectionFailed(ConnectionResult connectionResult) { }
3. Define a class that extends the Thread class and implements a method that sends your data object to all nodes
currently connected to the data layer. This task can block the main UI thread, so it must run in a new thread.
This can be an inner class.
class SendToDataLayerThread extends Thread {
String path;
DataMap dataMap;
// Constructor for sending data objects to the data layer
SendToDataLayerThread(String p, DataMap data) {
path = p;
dataMap = data;
}
public void run() {
7.1. First Wearable Data 33
Comentarios a estos manuales