我正在尝试使用实现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中以这种方式完成的。
答案 0 :(得分:0)
快速查看代码,我可以看到一些问题: