在 Wear 上传输资源

要通过蓝牙传输来发送图片等庞大的二进制数据 blob,您可以将 Asset 附加到数据项,然后将数据项放入重复数据存储。

注意:尽管 Wear 应用可以使用下文引用的 Data Layer API 与手机应用通信,但不建议使用此 API 连接到网络

资源自动处理数据的缓存,以防止重新传输并节省蓝牙带宽。一种常见模式是,手持式设备应用下载图片,将其缩小到适合在穿戴式设备上显示的尺寸,然后将其作为资源传输到穿戴式设备应用。以下示例演示了这一模式。

注意:尽管数据项的大小上限为 100KB,但资源想要多大都行。不过,在许多情况下,传输大型资源都会影响用户体验,因此请测试您的应用,以确保应用在您传输大型资源时能够保持良好的性能。

请参阅以下相关资源:

传输资源

您可以使用 Asset 类中的某个 create...() 方法来创建资源。在本例中,我们将一个位图转换成字节流,然后调用 createFromBytes() 来创建资源。

Kotlin

    private fun createAssetFromBitmap(bitmap: Bitmap): Asset =
        ByteArrayOutputStream().let { byteStream ->
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream)
            Asset.createFromBytes(byteStream.toByteArray())
        }
    

Java

    private static Asset createAssetFromBitmap(Bitmap bitmap) {
        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
        return Asset.createFromBytes(byteStream.toByteArray());
    }
    

有了资源后,使用 DataMapPutDataRequest 中的 putAsset() 方法将其附加到数据项,然后使用 putDataItem() 将数据项放入数据存储:

使用 PutDataRequest

Kotlin

    val asset: Asset = BitmapFactory.decodeResource(resources, R.drawable.image).let { bitmap ->
        createAssetFromBitmap(bitmap)
    }
    val request: PutDataRequest = PutDataRequest.create("/image").apply {
        putAsset("profileImage", asset)
    }
    val putTask: Task<DataItem> = Wearable.getDataClient(context).putDataItem(request)
    

Java

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    Asset asset = createAssetFromBitmap(bitmap);
    PutDataRequest request = PutDataRequest.create("/image");
    request.putAsset("profileImage", asset);
    Task<DataItem> putTask = Wearable.getDataClient(context).putDataItem(request);
    

使用 PutDataMapRequest

Kotlin

    val asset: Asset = BitmapFactory.decodeResource(resources, R.drawable.image).let { bitmap ->
        createAssetFromBitmap(bitmap)
    }
    val request: PutDataRequest = PutDataMapRequest.create("/image").run {
        dataMap.putAsset("profileImage", asset)
        asPutDataRequest()
    }
    val putTask: Task<DataItem> = Wearable.getDataClient(context).putDataItem(request)
    

Java

    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();
    Task<DataItem> putTask = Wearable.getDataClient(context).putDataItem(request);
    

接收资源

创建资源后,您可能希望在连接的另一端读取并提取该资源。以下示例说明了如何实现回调来检测资源变化并提取资源:

Kotlin

    override fun onDataChanged(dataEvents: DataEventBuffer) {
        dataEvents
                .filter { it.type == DataEvent.TYPE_CHANGED && it.dataItem.uri.path == "/image" }
                .forEach { event ->
                    val bitmap: Bitmap? = DataMapItem.fromDataItem(event.dataItem)
                            .dataMap.getAsset("profileImage")
                            .let { asset -> loadBitmapFromAsset(asset) }
                    // Do something with the bitmap
                }
    }

    fun loadBitmapFromAsset(asset: Asset): Bitmap? {
        // convert asset into a file descriptor and block until it's ready
        val assetInputStream: InputStream? =
                Tasks.await(Wearable.getDataClient(context).getFdForAsset(asset))
                ?.inputStream

        return assetInputStream?.let { inputStream ->
            // decode the stream into a bitmap
            BitmapFactory.decodeStream(inputStream)
        } ?: run {
            Log.w(TAG, "Requested an unknown Asset.")
            null
        }
    }
    

Java

    @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");
        }
        // convert asset into a file descriptor and block until it's ready
        InputStream assetInputStream =
            Tasks.await(Wearable.getDataClient(context).getFdForAsset(asset))
                .getInputStream();
        if (assetInputStream == null) {
            Log.w(TAG, "Requested an unknown Asset.");
            return null;
        }
        // decode the stream into a bitmap
        return BitmapFactory.decodeStream(assetInputStream);
    }