本节由介绍3种数据的发送接收:
1.Data Items : 比特类型数据,限制100KB以内
2.Assets : 资源类型数据,大小无上限
3.Message : 发送消息,触发指令
http://developer.android.com/training/wearables/data-layer/data-items.html
DataItem定义了同步手机和手表的数据接口.一个DataItem对象一般包括下面2个部分:
一般来说是不需要自己实现DataItem接口的,而是通过以下方式:
以上的方式并非最佳的选择,还可以通过DataMap的方式来实现,这类似于Bundle.
因为DataMap的方式是用Bundle实现的,所以它帮我们解决了序列化问题,并且可以使用键值对的方式来操控数据.
注意:此时手表和手机若未连接,则会缓存在发送端,待到两端连接时再进行同步.
代码样例
private void syncData() {
PutDataMapRequest dataMap = PutDataMapRequest.create("/count");
dataMap.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
.putDataItem(mGoogleApiClient, request);
}
当任意一端的数据发生变化时,这时候如果想通知到另外一端,可以通过监听数据改变接口来实现:
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
}
}
}
http://developer.android.com/training/wearables/data-layer/assets.html
若想通过蓝牙发送较大的二进制数据块,例如图片,则需要将数据绑定到Asset对象上.
Assets自动地处理了数据缓存的阻止重发和保存蓝牙数据带宽.一个通用模式来处理手机应用下载图片,再处理成适当的尺寸,最后转换成asset发送到手表.
注意:数据单元Data Item被限制在100KB以内,而Assets则是没有限制的.但是传输大数据量的Assets会影响到用户体验,所以若是迫不得已必须传较大的Assets,要多测试.
构建好了Assets,则可以通过PutDataRequest或PutDataMapRequest的方式传递数据
private void transferAsset{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Asset asset = createAssetFromBitmap(bitmap);
PutDataRequest request = PutDataRequest.create("/image");
request.putAsset("profileImage", asset);
Wearable.DataApi.putDataItem(mGoogleApiClient, request);
}
private void transferAsset{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Asset asset = createAssetFromBitmap(bitmap);
PutDataMapRequest dataMap = PutDataMapRequest.create("/image");
dataMap.getDataMap().putAsset("profileImage", asset)
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
.putDataItem(mGoogleApiClient, request);
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED &&
event.getDataItem().getUri().getPath().equals("/image")) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage");
Bitmap bitmap = loadBitmapFromAsset(profileAsset);
// Do something with the bitmap
}
}
}
public Bitmap loadBitmapFromAsset(Asset asset) {
if (asset == null) {
throw new IllegalArgumentException("Asset must be non-null");
}
ConnectionResult result =
mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
if (!result.isSuccess()) {
return null;
}
// convert asset into a file descriptor and block until it's ready
InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
mGoogleApiClient, asset).await().getInputStream();
mGoogleApiClient.disconnect();
if (assetInputStream == null) {
Log.w(TAG, "Requested an unknown Asset.");
return null;
}
// decode the stream into a bitmap
return BitmapFactory.decodeStream(assetInputStream);
}
http://developer.android.com/training/wearables/data-layer/messages.html
可以通过MessageApi来发送以下两种数据:
与DataItem的双向同步不同,message是单向的.Message通过单向的连接发送到另一端,这有利于远程调用(RPC - remote procedure calls).
以下是一个发送消息启动Activity的样例,在发送消息时,会阻塞线程,直到消息送达或者超时.
Node node; // the connected device to send the message to
GoogleApiClient mGoogleApiClient;
public static final START_ACTIVITY_PATH = "/start/MainActivity";
...
SendMessageResult result = Wearable.MessageApi.sendMessage(
mGoogleApiClient, node, START_ACTIVITY_PATH, null).await();
if (!result.getStatus().isSuccess()) {
Log.e(TAG, "ERROR: failed to send Message: " + result.getStatus());
}
以下是获取所有的和Android Wear应用相连接的节点.
private Collection<String> getNodes() {
HashSet <String>results = new HashSet<String>();
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for (Node node : nodes.getNodes()) {
results.add(node.getId());
}
return results;
}
以下是接收消息的例子,与上一个发送消息的例子对应.在处理接收到的消息之前,需要使用MessageApi.addListener()注册监听器.具体在其他章节中有讲述.
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
Intent startIntent = new Intent(this, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);
}
}