11

我为安卓穿戴开发了一个应用程序。下面的代码解释了问题

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

UPD:我通过关闭并再次打开手机来解决问题(Nexus 5)可能有更简单的方法来解决问题吗?

尝试添加 .await() 和 AsyncTask,但结果相同

4

4 回答 4

3

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

于 2014-08-16T19:22:21.617 回答
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>
于 2014-09-30T16:12:27.570 回答
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();
}
于 2015-02-09T22:05:35.400 回答
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;
}
于 2016-09-15T14:18:40.603 回答