在我的手机中,此代码将返回所有连接的节点:
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
Log.d(TAG, "Nodes: " + getConnectedNodesResult.getNodes());
}
});
这是为每个断开连接的节点调用的:
@Override
public void onPeerDisconnected(Node peer) {
// Which peer is this?
}
onPeerDisconnected:手册
Notification that a peer has been disconnected from this node or is no
longer reachable by this node.
Since multiple nodes can be connected to a network at the same time,
peer connected and disconnected events can come in any order.
我怎么知道我的Wear在手机中断开了连接?而不是任何其他连接的设备?
通常,我们不喜欢用节点的形状因子"mobile/watch/…"来标记节点,而是提供一种方式,每个节点可以广播其"能力",其他节点可以查询连接设备的网络,以查看哪些节点提供了所需的"能力"。这是通过使用CapabilityApis来完成的。我们在一些官方示例中使用了这些API,因此您可以看到它们的实际应用。例如,你的手机应用程序可以广播它支持"转录功能",你的穿戴应用程序可以找到那些提供该功能的节点,以决定将语音流发送到哪里进行转录。这提供了一个灵活和可扩展的框架,当生态系统增长时,该框架将继续工作。
Ali说的很对,但如果你真的需要设备的节点,那么NodeApi.GetLocalNodeResult
也有API。
以下部分向您展示了如何宣传可以处理活动请求的设备节点,发现能够满足请求需求的节点,并向这些节点发送消息,
广告功能
要从可穿戴设备在手持设备上启动活动,请使用MessageApi类发送请求。由于多个可穿戴设备可以连接到手持设备,因此可穿戴应用程序需要确定连接的节点是否能够启动活动。在手持应用程序中,宣传其运行的节点提供特定功能。
要宣传您的手持应用程序的功能:
在项目的res/values/目录中创建一个XML配置文件,并将其命名为wear.XML。将名为android_wear_capabilities的资源添加到wear.xml中。定义设备提供的功能。
<resources>
<string-array name="android_wear_capabilities">
<item>android_wear</item>
</string-array>
</resources>
检索具有所需功能的节点
最初,您可以通过调用CapabilityApi.getCapability((方法来检测有能力的节点。以下示例显示了如何使用android_wear功能手动检索可达节点的结果。在磨损模块中使用以下代码:
public abstract class BaseActivity extends Activity implements MessageApi.MessageListener, NodeApi.NodeListener,
DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String
SMART_WEAR_CAPABILITY_NAME = "android_wear";
protected GoogleApiClient mGoogleApiClient;
protected ArrayList<String> results;
private String TAG = "BaseActivity::Wear";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
private Collection<String> getNodes() {
results = new ArrayList<>();
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for (Node node : nodes.getNodes()) {
Log.d(TAG, node.getId());
results.add(node.getId());
}
return results;
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
Wearable.MessageApi.removeListener(mGoogleApiClient, this);
Wearable.NodeApi.removeListener(mGoogleApiClient, this);
Wearable.DataApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected(): Successfully connected to Google API client");
Wearable.MessageApi.addListener(mGoogleApiClient, this);
Wearable.DataApi.addListener(mGoogleApiClient, this);
Wearable.NodeApi.addListener(mGoogleApiClient, this);
results = new ArrayList<>();
getNodeIdOfHandheldDevice();
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "onConnectionSuspended(): Connection to Google API client was suspended");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed(): Failed to connect, with result: " + connectionResult);
}
@Override
public void onPeerConnected(Node node) {
Log.e(TAG, "onPeerConnected():");
}
@Override
public void onPeerDisconnected(Node node) {
Log.e(TAG, "onPeerDisconnected():");
}
private void getNodeIdOfHandheldDevice() {
Wearable.CapabilityApi.getCapability(
mGoogleApiClient, SMART_WEAR_CAPABILITY_NAME,
CapabilityApi.FILTER_REACHABLE).setResultCallback(
new ResultCallback<CapabilityApi.GetCapabilityResult>() {
@Override
public void onResult(CapabilityApi.GetCapabilityResult result) {
if (result.getStatus().isSuccess()) {
updateFindMeCapability(result.getCapability());
} else {
Log.e(TAG,
"setOrUpdateNotification() Failed to get capabilities, "
+ "status: "
+ result.getStatus().getStatusMessage());
}
}
});
}
private void updateFindMeCapability(CapabilityInfo capabilityInfo) {
Set<Node> connectedNodes = capabilityInfo.getNodes();
if (connectedNodes.isEmpty()) {
results.clear();
} else {
for (Node node : connectedNodes) {
// we are only considering those nodes that are directly connected
if (node.isNearby()) {
results.add(node.getId());
}
}
}
}
}
有关更多详细信息,请查看Android Dev