可穿戴设备.Wearable.NodeApi.getConnectedNodes方法的结果从未被调用。

11

我正在为Android Wear开发一个应用程序。以下是代码和问题的解释:

 if(mGoogleApiClient.isConnected()){
            K.i("Always called!");
            Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                @Override
                public void onResult(NodeApi.GetConnectedNodesResult nodes) {
                    K.i("Never called :( ");
                    for (Node node : nodes.getNodes()) {
                        Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), message, null);
                    }
                }
            });      
        }

更新:我通过关闭并重新打开我的手机(Nexus 5)来解决问题。也许还有更简单的方法来解决这个问题吗?

尝试添加.await()和AsyncTask,但结果是一样的。


我使用了这段代码来检测可穿戴设备是否已连接:http://davistechyinfo.blogspot.com/2014/07/android-determining-if-wearable-device.html - brwngrldev
@adavis 请仔细阅读问题,onResult() 没有被调用(线程从未完成)。 - dooplaye
@dooplaye 你是否调用了 connect() 方法(并等待 onConnected() 回调)来连接 mGoogleApiClient? - matiash
@matiash 看代码,mGoogleApiClient.isConnected() 返回 true; - dooplaye
在AsyncTask中使用await()时,我遇到了一个问题,但并不总是出现。有时它可以正常工作。所以我也在等待答案。看起来像是一个错误 - 但一个解决方法会很好。 - barkside
4个回答

3

我相信每次GoogleApiClient连接只能调用一次getConnectedNodes。您需要在第一次获得结果时缓存节点ID,然后使用onPeerConnected/Disconnected()回调来跟踪节点ID是否仍然相关。


好的建议,但还有另一个问题... OnPeedConnected/Disconnected() 没有被调用 - https://plus.google.com/+NathanSchwermann/posts/1Rs9etY5qte - dooplaye

1
如果您查看Google Wear示例,会发现一个名为FindMyPhone的项目。我认为他们解决问题的方式更加简洁。他们通过后台服务检查设备是否连接或断开。
package com.example.android.wearable.findphone;

import android.app.Notification;
import android.app.NotificationManager;

import com.google.android.gms.wearable.WearableListenerService;

/**
 * Listens for disconnection from home device.
 */
public class DisconnectListenerService extends WearableListenerService {

    private static final String TAG = "ExampleFindPhoneApp";

    private static final int FORGOT_PHONE_NOTIFICATION_ID = 1;

    @Override
    public void onPeerDisconnected(com.google.android.gms.wearable.Node peer) {
        // Create a "forgot phone" notification when phone connection is broken.
        Notification.Builder notificationBuilder = new Notification.Builder(this)
                .setContentTitle(getString(R.string.left_phone_title))
                .setContentText(getString(R.string.left_phone_content))
                .setVibrate(new long[] {0, 200})  // Vibrate for 200 milliseconds.
                .setSmallIcon(R.drawable.ic_launcher)
                .setLocalOnly(true)
                .setPriority(Notification.PRIORITY_MAX);
        Notification card = notificationBuilder.build();
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                .notify(FORGOT_PHONE_NOTIFICATION_ID, card);
    }

    @Override
    public void onPeerConnected(com.google.android.gms.wearable.Node peer) {
        // Remove the "forgot phone" notification when connection is restored.
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                .cancel(FORGOT_PHONE_NOTIFICATION_ID);
    }

}

他们还将这个添加到AndroidManifest.xml文件中。
<service android:name=".DisconnectListenerService" >
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>

"com.google.android.gms.wearable.BIND_LISTENER"现已过时。 - loshkin

0

我搞定了:

初始化Google API客户端:

private void initGoogleApiClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.d(TAG, "ConnectionCallback onConnected");
                    if (servicesAvailable()) {
                        // new CheckWearableConnected().execute();
                        resolveNodes();
                    }
                }

                @Override
                public void onConnectionSuspended(int i) {
                    Log.d(TAG, "ConnectionCallback onConnectionSuspended");
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    Log.d(TAG, "ConnectionCallback onConnectionFailed");
                    //TODO do something on connection failed
                }
            })
            .build();

}

然后在你的onStart方法中连接API客户端:

@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

最后,在您的onStop方法中进行清理:

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop");
    if (mGoogleApiClient != null)
        if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect();
}

0
这是一个完整的代码,用于检查它:
将此添加到gradle文件中:
compile 'com.google.android.gms:play-services-wearable:9.4.0'

使用此函数检查可穿戴设备是否已连接:

@WorkerThread
public boolean isWearableAvailable(Context context) {
    NotificationManagerCompat.from(context).cancel(NotificationType.WEARABLE_IN_CALL.getId());
    final GoogleApiClient googleApiClient = new Builder(context).addApi(Wearable.API).build();
    final ConnectionResult connectionResult = googleApiClient.blockingConnect();
    if (connectionResult.isSuccess()) {
        NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
        for (Node node : nodes.getNodes()) {
            if (node.isNearby()) 
                return true;
        }
    }
    return false;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接