在手机与可穿戴设备Wearable通过蓝牙Bluetooth传递Assets(图片等)

开发android wear程序时,经常需要将图片通过Assets 蓝牙传输。

1.创建asset,使用create...()方法,比如传递一个Bitmap传输如下

[java] view
plain
copyprint?

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

创建了asset,需要将它指向一个data item,通过使用putAsset() 方法如下

使用PutDataRequest

[java] view
plain
copyprint?

  1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  2. Asset asset = createAssetFromBitmap(bitmap);
  3. PutDataRequest request = PutDataRequest.create("/image");
  4. request.putAsset("profileImage", asset);
  5. Wearable.DataApi.putDataItem(mGoogleApiClient, request);

使用PutDataMapRequest

[java] view
plain
copyprint?

  1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  2. Asset asset = createAssetFromBitmap(bitmap);
  3. PutDataRequest request = PutDataRequest.create("/image");
  4. request.putAsset("profileImage", asset);
  5. Wearable.DataApi.putDataItem(mGoogleApiClient, request);

使用PutDataMapRequest

[java] view
plain
copyprint?

  1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  2. Asset asset = createAssetFromBitmap(bitmap);
  3. PutDataMapRequest dataMap = PutDataMapRequest.create("/image");
  4. dataMap.getDataMap().putAsset("profileImage", asset)
  5. PutDataRequest request = dataMap.asPutDataRequest();
  6. PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
  7. .putDataItem(mGoogleApiClient, request);

2. 手表端接收asset

[java] view
plain
copyprint?

  1. @Override
  2. public void onDataChanged(DataEventBuffer dataEvents) {
  3. for (DataEvent event : dataEvents) {
  4. if (event.getType() == DataEvent.TYPE_CHANGED &&
  5. event.getDataItem().getUri().getPath().equals("/image")) {
  6. DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
  7. Asset profileAsset = dataMapItem.getDataMap().getAsset("profileImage");
  8. Bitmap bitmap = loadBitmapFromAsset(profileAsset);
  9. // Do something with the bitmap
  10. }
  11. }
  12. }
  13. public Bitmap loadBitmapFromAsset(Asset asset) {
  14. if (asset == null) {
  15. throw new IllegalArgumentException("Asset must be non-null");
  16. }
  17. ConnectionResult result =
  18. mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
  19. if (!result.isSuccess()) {
  20. return null;
  21. }
  22. // convert asset into a file descriptor and block until it‘s ready
  23. InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
  24. mGoogleApiClient, asset).await().getInputStream();
  25. mGoogleApiClient.disconnect();
  26. if (assetInputStream == null) {
  27. Log.w(TAG, "Requested an unknown Asset.");
  28. return null;
  29. }
  30. // decode the stream into a bitmap
  31. return BitmapFactory.decodeStream(assetInputStream);
  32. }
时间: 2024-08-25 13:29:31

在手机与可穿戴设备Wearable通过蓝牙Bluetooth传递Assets(图片等)的相关文章

Android Wear 开发入门——如何创建一个手机与可穿戴设备关联的通知(Notification)

创建通知 为了创建在手机与可穿戴设备中都能展现的通知,可以使用 NotificationCompat.Builder.通过该类创建的通知,系统会处理该通知是否展现在手机或者穿戴设备中. 导入必要的类库 在开发之前首先需要导入以下类库 importandroid.support.v4.app.NotificationCompat; importandroid.support.v4.app.NotificationManagerCompat; importandroid.support.v4.app

iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发

1 前言 当前有越来越多的可穿戴设备使用了蓝牙4.0 BLE(Bluetooth Low Energy).对于iOS开发而言,Apple之前专门推出CoreBluetooth的Framework来支持BLE的开发.对于硬件开发有了解的朋友应该知道,在之前使用低版本的蓝牙的设备,要连接到iOS设备上,需要注册MFI,拥有MFI协议才能进行相应的开发.如果大家关注我之前对LEGO EV3的研究,就可以发现,EV3是使用了蓝牙2.1,因此需要MFI协议来进行开发. 本文将一步一步讲解如何使用CoreB

android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications

注:本文内容来自:https://developer.android.com/training/wearables/notifications/index.html 翻译水平有限,如有疏漏,欢迎批评指教. 译:山人 增加可穿戴设备功能到通知中 Adding Wearable Features to Notifications When an Android handheld (phone or tablet) and Android wearable are connected, the han

android wear开发之:创建可穿戴设备应用 - Creating Wearable Apps

注:本文内容来自:https://developer.android.com/training/wearables/apps/index.html 翻译水平有限,如有疏漏,欢迎批评指教. 译:山人 创建可穿戴设备应用 Creating Wearable Apps Wearable apps run directly on the device, giving you access to hardware such as sensors and the GPU. They are fundamen

通过蓝牙调试(穿戴设备app) - 开发文档翻译

由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明"转",那么均为原创,转贴请注明本博客链接链接 Debugging over Bluetooth 通过蓝牙调试 You can debug your wearable over Bluetooth by routing its debug output to the handheld device that's connected to your development machine. 你可以通过蓝牙debug你的穿戴设备

可穿戴设备的尴尬 购买后半年就被弃用

相比过去的一年,可穿戴设备已经不是高高在上,而是走进了我们的生活之中.你会越来越多的发现身边的人佩戴着智能手环或者手表等设备,然而你同样也会发现,拥有这些设备的朋友们并不会持续佩戴,而是佩戴一段时间后可穿戴设备就销声匿迹了.这是为什么呢? 大多数的可穿戴设备都是打着健康的招牌,通过不断的记录我们生活中的热量摄入和消耗来帮助我们进行健康方面的调整.按理说这样的产品应该会让用户爱不释手,但实际情况却是有三分之一的用户在购买后六个月到一年后就放弃使用了.非常让人吃惊! 做出这份调查报告的机构负责人丹·

可穿戴设备的发展与挑战

可穿戴设备的发展和挑战 “可穿戴设备”已经不是新鲜词汇了,即直接穿在身上或整合到衣服或配件里的一种便携式设备.几十年的发展中,它已不仅仅是一种硬件设备,更是通过软件支持,数据交互,云端交互等高科技来实现强大的功能.它并非只是移动互联网时代产生的衍生品,在很久以前这种思想便已萌生在上世纪70年代的赌场里.经过平稳的发展后,现代社会里可穿戴设备已经对我们的生活和感知带来了比较大的转变. 关于它的发展史已经没有回顾的必要,总体是随着最新的科技前沿和智能手机的更迭在稳步发展.通过研讨,我们总结出了在现阶

打包穿戴设备app - 开发文档翻译

由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明"转",那么均为原创,转贴请注明本博客链接链接 Packaging Wearable Apps 打包穿戴设备app When publishing to users, you must package a wearable app inside of a handheld app, because users cannot browse and install apps directly on the wearable. I

你真在意可穿戴设备的续航吗?

自可穿戴设备发布之日起,伴随它的就是不断的质疑声.从外形.体验.性能到应用.价格,几乎都有可挑剔的地方.当然,质疑最多的还是其续航.我们知道,体积越大,越能容纳更大容量的电池--iPad Air就明显比iPad mini 2电量更大,续航时间更长.受限于可穿戴设备娇小的"身躯",即使苹果.索尼这样的工艺设计泰斗,也很难为其装入大容量电池,最终导致续航让用户不满意. 但是,你真的会在意可穿戴设备的续航吗?或者说,在实际体验过程中,你更在意续航,还是外形.性能.应用等其他方面?仔细想想,可