Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1570876
  • 博文数量: 113
  • 博客积分: 3526
  • 博客等级: 中校
  • 技术积分: 1815
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-08 09:46
个人简介

记录总结自己的工作

文章分类

全部博文(113)

文章存档

2015年(19)

2014年(10)

2013年(6)

2012年(16)

2011年(24)

2010年(21)

2009年(17)

分类: 嵌入式

2012-12-19 14:39:41

    上一篇文章介绍了使用alljoyn的raw方式进行文件的传输,其实那种方法不是很实用,并不是一种好的方式,这篇文章介绍一下使用alljoyn的signal方式进行文件的传输。
     在alljoyn的sdk里面有个chat的例子,这个例子就是使用signal传输手机间的聊天信息的。我们所要做的就是将其修改一下来传输我们的文件。
     首先修改ChatInterface.java里面的接口,将参数从string 类型改成byte[]类型,如下所示:

点击(此处)折叠或打开

  1. public void Chat(byte[] str) throws Bu***ception;
    这就表示我们传输的不是字符串而是byte数组了。所以接下来的问题就是将文件内容转换成一系列的byte数组发出去然后接收这些byte数组并重新组成文件就可以了。首先来看发送,发送时在AllJoynService.java的doSendMessages()方法。


点击(此处)折叠或打开

  1. try {
  2.                 if (mJoinedToSelf) {
  3.                     if (mHostChatInterface != null) {
  4.                         // mHostChatInterface.Chat(message);

  5.                     }
  6.                 } else {
  7.                     SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
  8.                     Date curDate = new Date(System.currentTimeMillis());// 获取当前时间

  9.                     String str = formatter.format(curDate);
  10.                     Log.d("test", "start:" + str+"::::");
  11.                     String filepath = "/sdcard/Picture/100ANDRO/MOV_0426.mp4";
  12.                     File file = new File(filepath);
  13.                     InputStream in = null;
  14.                     try {
  15.                         in = new FileInputStream(file);
  16.                     } catch (FileNotFoundException e1) {
  17.                         // TODO Auto-generated catch block

  18.                         e1.printStackTrace();
  19.                     }
  20.                     byte[] buffer = new byte[102400];
  21.                     int read = 0;
  22.                     while ((read = in.read(buffer)) != -1) {
  23.                         mChatInterface.Chat(buffer);
  24.                         buffer = new byte[102400];
  25.                     }
  26.                     in.close();
  27.                     curDate = new Date(System.currentTimeMillis());// 获取当前时间

  28.                     str = formatter.format(curDate);
  29.                     Log.d("test", "end:" + str+"::::");
  30.                 }
  31.             } catch (Bu***ception ex) {
  32.                 mChatApplication.alljoynError(ChatApplication.Module.USE,
  33.                         "Bus exception while sending message: (" + ex + ")");
  34.             } catch (IOException e) {
  35.                 Log.d("test", e.toString());
  36.             }
     在发送的前后加上系统的时间这样就可以测试发送文件花费的时间。接收是在Chat(byte[] string) 方法里面。

点击(此处)折叠或打开

  1. public void Chat(byte[] string) {

  2.         /*
  3.          * See the long comment in doJoinSession() for more explanation of why
  4.          * this is needed.
  5.          *
  6.          * The only time we allow a signal from the hosted session ID to pass
  7.          * through is if we are in mJoinedToSelf state. If the source of the
  8.          * signal is us, we also filter out the signal since we are going to
  9.          * locally echo the signal.
  10.          */
  11.         String uniqueName = mBus.getUniqueName();
  12.         MessageContext ctx = mBus.getMessageContext();
  13.         Log.i(TAG, "Chat(): use sessionId is " + mUseSessionId);
  14.         Log.i(TAG, "Chat(): message sessionId is " + ctx.sessionId);

  15.         /*
  16.          * Always drop our own signals which may be echoed back from the system.
  17.          */
  18.         if (ctx.sender.equals(uniqueName)) {
  19.             Log.i(TAG, "Chat(): dropped our own signal received on session "
  20.                     + ctx.sessionId);
  21.             return;
  22.         }

  23.         /*
  24.          * Drop signals on the hosted session unless we are joined-to-self.
  25.          */
  26.         if (mJoinedToSelf == false && ctx.sessionId == mHostSessionId) {
  27.             Log.i(TAG, "Chat(): dropped signal received on hosted session "
  28.                     + ctx.sessionId + " when not joined-to-self");
  29.             return;
  30.         }

  31.         /*
  32.          * To keep the application simple, we didn't force users to choose a
  33.          * nickname. We want to identify the message source somehow, so we just
  34.          * use the unique name of the sender's bus attachment.
  35.          */
  36.         String nickname = ctx.sender;
  37.         nickname = nickname
  38.                 .substring(nickname.length() - 10, nickname.length());

  39.         // Log.i(TAG, "Chat(): signal " + string + " received from nickname " +

  40.         // nickname);

  41. //        mChatApplication.newRemoteUserMessage(nickname, "get message");

  42.         try {
  43.             try {
  44.                 if (file == null) {
  45.                     file = new File("/mnt/sdcard/abc.mp4");
  46.                 }
  47.                 fos = new FileOutputStream(file, true);
  48.             } catch (Exception e) {
  49.                 Log.e(TAG, "creat file error");
  50.             }
  51.             try {
  52.                 fos.write(string, 0, string.length);
  53.             } catch (IOException e) {
  54.                 Log.e(TAG, "write file error");
  55.             }

  56.         } finally {
  57.             if (fos != null) {
  58.                 try {
  59.                     fos.close();
  60.                 } catch (IOException e) {
  61.                     Log.e(TAG, e.toString());
  62.                 }
  63.             }
  64.             fos = null;
  65.         }
  66.     }
      这样就可以进行文件的传输了,测试表明这个方法比上个方法更快更稳定了。
 


 
 
 
 
阅读(8301) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

云少嘎嘎嘎2013-07-31 09:08:57

wqycsu:请问,chat消息发送的机制是什么,我看代码中执行了doSendMessage(),但该方法也没有具体去实现发送呀

是使用了alljoyn的signal机制进行发送的。你可以试用一下sdk里面的chat的例子啊,用两个手机,是可以实现发送和接收消息的。

回复 | 举报

wqycsu2013-07-26 10:20:45

请问,chat消息发送的机制是什么,我看代码中执行了doSendMessage(),但该方法也没有具体去实现发送呀

云少嘎嘎嘎2012-12-24 10:34:12

中国物讯网11: 这代码看的我很晕,,,
福建物流物流信息平台.....
哦。这是基于alljoyn sdk里面的小例子修改来的,想要完全看懂得去先下载一下sdk然后就容易看懂了。

中国物讯网112012-12-20 16:48:32

这代码看的我很晕,,,
福建物流物流信息平台