我遇到了api功能问题。我正试图从我的Android可穿戴设备向我的手机发送消息。 我检测到一个功能,但根本没有节点。
private static final String CAPABILITY_NAME = "mobile";
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "CONNECTED TO API");
setupNode();
Wearable.CapabilityApi.addCapabilityListener(mApiClient, capabilityListener, CAPABILITY_NAME);
}
private void setupNode() {
new Thread(new Runnable() {
@Override
public void run() {
Wearable.CapabilityApi.getCapability(
mApiClient, CAPABILITY_NAME,
CapabilityApi.FILTER_REACHABLE).setResultCallback(new ResultCallback<CapabilityApi.GetCapabilityResult>() {
@Override
public void onResult(CapabilityApi.GetCapabilityResult result) {
if (result.getCapability() == null) {
Log.d(TAG, "we detected no capability");
} else {
Log.d(TAG, "we detected a capability");
}
updateCapability(result.getCapability());
}
});
}
}).start();
}
private void updateCapability(CapabilityInfo capabilityInfo) {
Set<Node> connectedNodes = capabilityInfo.getNodes();
nodeId = pickBestNodeId(connectedNodes);
Log.d(TAG, "nodeId : " + nodeId);
if (nodeId != null) {
sendMessage(MESSAGE_PATH_READY, "connectionToMobile"); //try a config message to mobile
} else {
//if (mobileAppIsRunning) {
Intent intent = new Intent();
String status = "DISCONNECTED";
intent.putExtra("status", status);
intent.setAction("com.orange.myapp.watch.moto360.receiveDeviceEvent");
sendBroadcast(intent);
//} else {
//DO NOTHING
//}
}
}
private String pickBestNodeId(Set<Node> nodes) {
String bestNodeId = null;
// Find a nearby node or pick one arbitrarily
for (Node node : nodes) {
if (node.isNearby()) {
return node.getId();
}
bestNodeId = node.getId();
}
Log.d(TAG, "best node id :" + bestNodeId);
return bestNodeId;
}
CapabilityApi.CapabilityListener capabilityListener =
new CapabilityApi.CapabilityListener() {
@Override
public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
Log.d(TAG, "CAPABILITY CHANGED!");
updateCapability(capabilityInfo);
}
};
在移动端,我在wear.xml文件中添加了以下代码:
<resources>
<string-array name="android_wear_capabilities">
<item>mobile</item>
</string-array>
</resources>
这些是我打印的日志:
02-01 18:09:16.758 4907-4907/? D/WearMessageListenerService﹕ creating service
02-01 18:09:16.811 4907-4907/? D/WearMessageListenerService﹕ mApiClient : com.google.android.gms.common.api.zzg@7ba32
02-01 18:09:17.138 4907-4907/? D/WearMessageListenerService﹕ CONNECTED TO API
02-01 18:09:17.182 4907-4907/? D/WearMessageListenerService﹕ we detected a capability
02-01 18:09:17.214 4907-4907/? D/WearMessageListenerService﹕ best node id :null
02-01 18:09:17.215 4907-4907/? D/WearMessageListenerService﹕ nodeId : null
要将信息从手机发送到手表,我都不会使用此功能:
private void sendMessage(final String path, final String text) {
Wearable.NodeApi.getConnectedNodes(mApiClient)
.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
for (Node node : result.getNodes()) {
Wearable.MessageApi.sendMessage(mApiClient, node.getId(), path, text.getBytes());
}
}
});
}
这很好,我收到了手表上的消息。
答案 0 :(得分:8)
好吧,我在挖掘后发现了它。我在磨损方面的gradle中的包签名有问题。我可以给出一个建议,这样你就不会浪费太多时间,总是检查你的gradle文件(穿戴和移动)中的包名是否相同。