<android.support.wearable.view.WatchViewStub
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:id="@+id/watch_view_stub"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	app:rectLayout="@layout/rect_activity_main"
	app:roundLayout="@layout/round_activity_main"
	tools:context=".MainActivity"
	tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
	stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
   	@Override
   	public void onLayoutInflated(WatchViewStub stub) {
       	heartRateTxtView = (TextView) stub.findViewById(R.id.text);
   	}
	});
}

@Override
protected void onResume() {
	super.onResume();
	if (heartRateSensor == null) {
   	heartRateTxtView.setText(R.string.no_sensor_available);
	} else {
   	sensorManager.registerListener(this, heartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
	}
}

@Override
protected void onPause() {
	super.onPause();
	if (heartRateSensor != null) {
   	sensorManager.unregisterListener(this, heartRateSensor);
	}
}
@Override
public void onSensorChanged(SensorEvent event) {
	if (event.accuracy != SensorManager.SENSOR_STATUS_UNRELIABLE) {
   	heartRateTxtView.setText(getString(R.string.heart_rate_frequency, event.values[0]));
	} else {
   	heartRateTxtView.setText(getString(R.string.unreliable_heart_rate));
	}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
	[...]
	googleApiClient = new GoogleApiClient.Builder(this)
       	.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
           	@Override
           	public void onConnected(Bundle connectionHint) {
           	}

           	@Override
           	public void onConnectionSuspended(int cause) {
           	}
       	})
       	.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
           	@Override
           	public void onConnectionFailed(ConnectionResult result) {
           	}
       	})
       	.addApi(Wearable.API)
       	.build();
}

@Override
protected void onStart() {
	super.onStart();
	googleApiClient.connect();
}

@Override
protected void onStop() {
	googleApiClient.disconnect();
	super.onStop();
}

@Override
public void onSensorChanged(SensorEvent event) {
  	[...]
	if (googleApiClient.isConnected()) {
   	PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/heartrate");
   	DataMap dataMap = dataMapRequest.getDataMap();
   	dataMap.putFloat("HeartRate", event.values[0]);
   	Wearable.DataApi.putDataItem(googleApiClient, dataMapRequest.asPutDataRequest());
	} else {
   	[...]
   	}
}

@Override
public void onConnected(Bundle connectionHint) {
	Wearable.DataApi.addListener(googleApiClient, MainActivity.this);
}

@Override
protected void onStop() {
	if (googleApiClient != null && googleApiClient.isConnected()) {
   	Wearable.DataApi.removeListener(googleApiClient, this);
   	googleApiClient.disconnect();
	}
	super.onStop();
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
	for (DataEvent event : dataEvents) {
   	Uri uri = event.getDataItem().getUri();
   	if (event.getType() == DataEvent.TYPE_DELETED) {
            	Log.d(TAG, "DataItem deleted: " + uri);
   	} else if (event.getType() == DataEvent.TYPE_CHANGED) {
            	Log.d(TAG, "DataItem changed: " + uri);
	if("/heartrate".equals(uri.getPath())){
    	DataMapItem dataMapItem =  DataMapItem.fromDataItem(event.getDataItem());
                	Float heartRate = dataMapItem.getDataMap().getFloat("HeartRate");
                	[...]
            	}
}
	}
}