In mobile phones and Wearable devices, Wearable transmits Assets (images, etc.) through Bluetooth ),
When developing the android wear program, you often need to transmit images through Assets Bluetooth.
1. create an asset and use the create... () method. For example, transfer a Bitmap file as follows:
[Java]View plaincopyprint?
- Private static Asset createAssetFromBitmap (Bitmap bitmap ){
- Final ByteArrayOutputStream byteStream = new ByteArrayOutputStream ();
- Bitmap. compress (Bitmap. CompressFormat. PNG, 100, byteStream );
- Return Asset. createFromBytes (byteStream. toByteArray ());
- }
When an asset is created, you need to point it to a data item by using the putAsset () method as follows:
Use PutDataRequest
[Java]View plaincopyprint?
- 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 );
Use PutDataMapRequest
[Java]View plaincopyprint?
- 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 );
Use PutDataMapRequest
[Java]View plaincopyprint?
- 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 );
2. The watch side receives asset
[Java]View plaincopyprint?
- @ 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 );
- }