【问题标题】:Trouble Connecting To Wearable Data Layer无法连接到可穿戴数据层
【发布时间】:2016-01-31 06:50:40
【问题描述】:

我正在尝试使用实现DataApi.DataListenerActivity 连接到可穿戴设备上的数据层。在onStart() 中,设备连接到Google API 客户端,并调用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

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

连接结果总是返回error code 14,即连接超时。所以我尝试将已经连接的GoogleApiClientDataLayerActivity 作为参数传递给FetchDataTask,并将其用于进程。但是当我在doInBackground() 内拨打Wearable.DataApi.getDataItem().await()Werable.NodeApi.getLocalNode().await() 时,await() 永远不会结束,我的应用程序就会停止。但我从未收到来自onConnectionSuspended()onConnectionFailed() 的任何错误消息。我无法弄清楚为什么在我的AsyncTask 中与数据层的连接会超时。我知道这是可以做到的,因为它是在 XYZTouristAttractions 示例应用程序的 AttractionsActivity 中完成的。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我没有注意到我从未打电话给connect(),这似乎会导致问题。但是我在doInBackground() 中添加了googleApiClient.connect(),我仍然得到相同的超时错误代码。 AsyncTask 没有 start() 方法,我相信 execute() 功能类似。至于await(),其实只是为了测试,我打算在完成的代码中加入超时回调。
    • 另外,我只是进一步研究了它,看起来googleApiClient.blockingConnect(30, TimeUnit.SECONDS) 充当connect() 调用。记录在这里:developers.google.com/android/reference/com/google/android/gms/…
    • 您对不匹配的大括号和start() 调用是正确的,我以为您的意思是AsyncTask。当我复制代码时它有点混乱,当我试图修复它时我一定错过了。在原始代码中是正确的,我只是在问题中修复了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多