无法连接到可穿戴数据层

时间:2015-10-30 18:41:59

标签: android android-asynctask timeout wear-os android-wear-data-api

我正在尝试使用实现Activity的{​​{1}}连接到可穿戴设备上的数据层。在DataApi.DataListener设备中,设备会连接到Google API客户端,并会调用onStart()。从onConnected()开始,我调用另一种方法向手持设备发送消息以更新数据,并启动新的onConnected()以从数据层获取数据。这是我的AsyncTask

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()); } } }

FetchDataTask

连接结果始终以error code 14返回,这是一个连接超时。所以我尝试将已经连接的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; } } GoogleApiClient作为参数传递给DataLayerActivity,并将其用于进程。但是,当我在FetchDataTask内拨打Wearable.DataApi.getDataItem().await()Werable.NodeApi.getLocalNode().await()时,doInBackground()永远不会结束,我的应用程序会停止。但我从来没有收到来自await()onConnectionSuspended()的任何错误消息。我无法弄清楚为什么与数据层的连接在我的onConnectionFailed()中超时。我知道这可以做到,因为它是在XYZTouristAttractions示例应用程序的AttractionsActivity中以这种方式完成的。

1 个答案:

答案 0 :(得分:0)

快速查看代码,我可以看到一些问题:

  • 在FetchDataTask中,在doInBackground中,您构建了一个googleApiClient,但您从未调用connect()来建立连接
  • 在requestUpdate()中,似乎有不匹配的大括号等。另外,对于你在那里构建的线程,我看不到你曾经在它上面调用“start()”。
  • 每当您在任何异步调用上调用await()时,请确保指定超时以使其永远不会被卡住,然后在使用结果之前检查以确保调用成功。