首页 > 代码库 > 在手机与可穿戴设备Wearable通过蓝牙Bluetooth传递Assets(图片等)
在手机与可穿戴设备Wearable通过蓝牙Bluetooth传递Assets(图片等)
开发android wear程序时,经常需要将图片通过Assets 蓝牙传输。
1.创建asset,使用create...()方法,比如传递一个Bitmap传输如下
[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());
- }
创建了asset,需要将它指向一个data item,通过使用putAsset() 方法如下
使用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);
使用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);
使用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. 手表端接收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);
- }
在手机与可穿戴设备Wearable通过蓝牙Bluetooth传递Assets(图片等)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。