I am trying to connect to the data layer on the wearable device using an Activity
that implements DataApi.DataListener
. In onStart()
the device connects to the Google API Client, and onConnected()
is called. From onConnected()
I call another method that sends a message to the handheld to update the data, and starts a new AsyncTask
to get the data from the data layer. This is my Activity
:
public class DataLayerActivity extends Activity implements DataApi.Listener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private ArrayList<myObject> mObjects = new ArrayList<>();
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
if(null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
Wearable.DataApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
} super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
requestUpdate();
}
@Override
public void onConnectionSuspended(int cause) {
Timber.d("Connection Suspended");
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Timber.d("Connection Failed with result: " + result);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
}
private void requestUpdate() {
new Thread(new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for(Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(getGoogleApiClient(), node.getId(), "/path", null).setResultCallback(onMessageResult());
}
}
}).start();
try {
Uri uri = Uri.parse("/path");
mObjects = new FetchDataTask(this).execute(uri).get();
} catch(InterruptedException | ExecutionException exception) {
Timber.e(exception, "Fetch Data Failed");
}
}
protected ResultCallback<MessageApi.SendMessageResult> onMessageResult() {
if(!result.getStatus().isSuccess()) {
Timber.d("Failed to Connect with status " + result.getStatus());
}
}
}
And this is the FetchDataTask
:
public class FetchDataTask extends AsyncTask<Uri, Void, ArrayList<myObject>> {
private Context mContext;
private ArrayList<myObject> mObjects = new ArrayList<>();
public FetchDataTask(Context context) {
mContext = context;
}
@Override
protected ArrayList<myObject> doInBackground(Uri... params) {
GoogleApiClient googleApiClient = new GoogleApiClient
.Builder(mContext).addApi(WearableAPI).build();
ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
if(!connectionResult.isSuccess() || !googleApiClient.isConnected()) {
Timber.e("Failed to Connect with error code: " + connectionResult.getErrorCode());
return null;
}
...
return mObjects;
}
}
The connection result always returns with error code 14, which is a connection timeout. So I tried passing the already connected GoogleApiClient
from DataLayerActivity
as a parameter to FetchDataTask
, and using that for the processes. But when I make a call to Wearable.DataApi.getDataItem().await()
or Werable.NodeApi.getLocalNode().await()
within doInBackground()
, await()
never finishes, and my app stalls. But I never get any error messages from onConnectionSuspended()
or onConnectionFailed()
. I cannot figure out why the connection to the data layer times out in my AsyncTask
. I know this can be done, as it is done this way in the AttractionsActivity in the XYZTouristAttractions sample application.