我开发了一款适用于Android的应用程序。下面的代码解释了问题
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,但结果是相同的
答案 0 :(得分:3)
我相信每次GoogleApiClient连接只能调用一次getConnectedNodes。您希望在第一次获得结果时缓存节点ID,然后使用onPeerConnected / Disconnected()回调来跟踪节点ID是否仍然相关。
答案 1 :(得分:1)
如果你看看google的样品,那就是一个名为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>
答案 2 :(得分: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();
}
答案 3 :(得分: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;
}