Wear でアセットを転送する

別のデバイスからの音声録音など、バイナリ ラージ オブジェクトを Bluetooth 経由で送信するために、Asset をデータアイテムにアタッチしてから、複製したデータストアにそのデータアイテムを格納できます。

アセットは、再送信を防止し、Bluetooth の帯域幅を節約するために、データのキャッシュ保存を自動的に処理します。よくあるパターンとして、ハンドヘルド アプリで画像をダウンロードし、それをウェアラブルで表示するために適切なサイズに縮小して、ウェアラブル アプリにアセットとして送信するというものがあります。以下の例で、このパターンについて説明します。

注: データアイテムのサイズは、理論的には 100 KB に制限されていますが、実際には、より大きなデータアイテムを使用できます。大規模なデータアイテムの場合、デベロッパーは一意のパスでデータを分離し、すべてのデータに単一のパスを使用しないようにします。大規模なアセットを転送すると、多くの場合、ユーザー エクスペリエンスに影響します。そのため、アプリをテストして、大規模なアセットを転送しても良好なパフォーマンスが得られることを確認してください。

アセットを転送する

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());
}

次に、DataMap または PutDataRequestputAsset() メソッドを使用して、アセットをデータアイテムにアタッチします。その後、以下のサンプルに示すように、 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);
}

詳しくは、GitHub の DataLayer サンプル プロジェクトをご覧ください。