通过alljoyn传输文件

Alljoyn是高通开发的一个开源的p2p通信框架,支持wifi/蓝牙和wifi-direct。目前已经有了很多基于这个框架开发的联机游戏。
    在Alljoyn的sdk里有很多例子,其中有一个通过raw session发送信息的,对这个例子略加修改就可以实现两个手机之间文件的传输。
     在client端,其发送的信息是通过一个FileOutputStream将要发送的信息写入到一个中间文件来发送到对方的。所以我们只要将要发送的文件通过流的形式输入到这个FileOutputStream中就可以了。
if (mStreamUp == true) {
                    String string = (String) msg.obj + "\n";
                    try {
                        // logInfo(String.format("Writing %s to output stream",

                        // string));

                        // mOutputStream.write(string.getBytes());

                        // logInfo(String.format("Flushing stream", string));

                        // mOutputStream.flush();

                        String filepath = "/sdcard/test.txt";
                        File file = new File(filepath);
                        InputStream in =new FileInputStream(file);
                        byte[] buffer = new byte[1024];
                        int read;
                        long total = 0;
                        while ((read = in.read(buffer)) != -1) {
                            mOutputStream.write(buffer, 0, read);
                            total+=read;
                            Log.d("test","total:"+total);
                            if(total>100000){
                                mOutputStream.flush();
                                total=0;
                                //容易出现io exception,所以稍微等待一下。

                                Thread.sleep(100);
                            }
                        }
                        mOutputStream.flush();
                        Log.d("test","flush");
                        in.close();
                    } catch (IOException ex) {
                        Log.e("test",ex.toString());
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block

                        e.printStackTrace();
                    }
                }
                break;
            }
其中前面注释掉的代码是实例程序原来的,下面的就是我修改的,将文件输入到输出流中。需要注意的是如果不加中间的sleep的话就会报IO Exception,这是因为raw使用的是非阻塞式socket发送的,如果发送得过快而网络传输得慢就会产生异常,中断传输。就好像一个大水池,一个水管往里注水,一个水管往外放水,如果注水的速度大于放水的速度的话水池就会溢出。所以在写入的时候稍微睡眠一下,也就是稍微控制一下注入的速度,让底层把内容发出去。
    虽然这样可以在一定程度上减少异常的发生,但是传输的文件如果太大的话还是容易产生异常的,在这种情况下可以使用try catch来捕获这个异常然后休眠一段时间从中断的位置重新传输。
 
    在server端,我们只需要将接受到的流写入到一个新建的文件中就可以了。其中注释掉的也是其原来的程序。
mReader = new Thread(new Runnable() {
                        public void run() {
                            logInfo("Thread running");
                            Looper.myLooper().prepare(); 
                            StringBuilder stringBuilder = new StringBuilder();
                            try {
                                while (true) {
                                    int c;
                                    /*
                                     * Wait until a character is available.
                                     */
                                    if (reader.ready() == false) {
                                        Thread.sleep(100);
                                        continue;
                                    }
                                    
                                    /*
                                     * Build a string out of the characters and
                                     * when we see a newline, cook up a toast
                                     * do display them.
                                     */
                                    if(saveFile("/mnt/sdcard/test.txt",is)){
                                        Toast.makeText(Service.this, "save success", Toast.LENGTH_LONG).show();
                                    }else{
                                        Toast.makeText(Service.this, "save faild", Toast.LENGTH_LONG).show();
                                    }
//                     try {

//                         c = reader.read();

//                         logInfo(String.format("Read %d from stream", c));

//                         stringBuilder.append((char)c);

//                         if (c == 10) {

//                             String s = stringBuilder.toString();

//                         logInfo(String.format("Read %s from stream", s));

//              Message toastMsg = mHandler.obtainMessage(MESSAGE_POST_TOAST, s);

//              mHandler.sendMessage(toastMsg);

//          stringBuilder.delete(0, stringBuilder.length() - 1);

//                         }

//                     } catch (IOException ex) {

//                     }

                                }
                            } catch (Throwable ex) {
                                 logInfo(String.format("Exception reading raw Java stream: %s", ex.toString()));
                            }
                            logInfo("Thread exiting");
                        }
                    }, "reader");
                    mReader.start();
我们还需要加入一个保存文件的函数:
private static boolean saveFile(String filePath, InputStream is) {
        boolean saved = false;
        byte[] buffer = new byte[1024];
        FileOutputStream fos = null;
        File file = null;
        try {
            try {
                file = new File(filePath);
                fos = new FileOutputStream(file,true);
            } catch (Exception e) {
                Log.e(TAG,"creat file error");
            }
            int readlen = -1;
            long readCount = 0;
            while (true) {
                saved = true;
                try {
                    readlen = is.read(buffer);
                } catch (IOException e) {
                     Log.e(TAG,"read inputstream error");
                } finally {
                    if (readlen <= 0) {
                        break;
                    }
                }
                readCount += readlen;
                Log.d("test","FILE length::::::::::::::::::::"+readCount);
                try {
                    fos.write(buffer, 0, readlen);
                } catch (IOException e) {
                    Log.e(TAG,"write file error");
                }
            }
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                    }
                }
                fos = null;
            }
     Log.d("test","saved:"+saved);
        return saved;
    }
好了,这样在client端的sd卡的根目录中新建一个test.txt文件,当两个手机连接起来后,client端随便发送一个字符串就可以触发传输文件的操作了。

通过alljoyn传输文件(2)  
上一篇文章介绍了使用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.     }
       这样就可以进行文件的传输了,测试表明这个方法比上个方法更快更稳定了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值