上一篇文章介绍了使用alljoyn的raw方式进行文件的传输,其实那种方法不是很实用,并不是一种好的方式,这篇文章介绍一下使用alljoyn的signal方式进行文件的传输。
在alljoyn的sdk里面有个chat的例子,这个例子就是使用signal传输手机间的聊天信息的。我们所要做的就是将其修改一下来传输我们的文件。
首先修改ChatInterface.java里面的接口,将参数从string 类型改成byte[]类型,如下所示:
- public void Chat(byte[] str) throws Bu***ception;
这就表示我们传输的不是字符串而是byte数组了。所以接下来的问题就是将文件内容转换成一系列的byte数组发出去然后接收这些byte数组并重新组成文件就可以了。首先来看发送,发送时在AllJoynService.java的doSendMessages()方法。
- try {
- if (mJoinedToSelf) {
- if (mHostChatInterface != null) {
- // mHostChatInterface.Chat(message);
- }
- } else {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
- Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
- String str = formatter.format(curDate);
- Log.d("test", "start:" + str+"::::");
- String filepath = "/sdcard/Picture/100ANDRO/MOV_0426.mp4";
- File file = new File(filepath);
- InputStream in = null;
- try {
- in = new FileInputStream(file);
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- byte[] buffer = new byte[102400];
- int read = 0;
- while ((read = in.read(buffer)) != -1) {
- mChatInterface.Chat(buffer);
- buffer = new byte[102400];
- }
- in.close();
- curDate = new Date(System.currentTimeMillis());// 获取当前时间
- str = formatter.format(curDate);
- Log.d("test", "end:" + str+"::::");
- }
- } catch (Bu***ception ex) {
- mChatApplication.alljoynError(ChatApplication.Module.USE,
- "Bus exception while sending message: (" + ex + ")");
- } catch (IOException e) {
- Log.d("test", e.toString());
- }
在发送的前后加上系统的时间这样就可以测试发送文件花费的时间。接收是在Chat(byte[] string) 方法里面。
- public void Chat(byte[] string) {
- /*
- * See the long comment in doJoinSession() for more explanation of why
- * this is needed.
- *
- * The only time we allow a signal from the hosted session ID to pass
- * through is if we are in mJoinedToSelf state. If the source of the
- * signal is us, we also filter out the signal since we are going to
- * locally echo the signal.
- */
- String uniqueName = mBus.getUniqueName();
- MessageContext ctx = mBus.getMessageContext();
- Log.i(TAG, "Chat(): use sessionId is " + mUseSessionId);
- Log.i(TAG, "Chat(): message sessionId is " + ctx.sessionId);
- /*
- * Always drop our own signals which may be echoed back from the system.
- */
- if (ctx.sender.equals(uniqueName)) {
- Log.i(TAG, "Chat(): dropped our own signal received on session "
- + ctx.sessionId);
- return;
- }
- /*
- * Drop signals on the hosted session unless we are joined-to-self.
- */
- if (mJoinedToSelf == false && ctx.sessionId == mHostSessionId) {
- Log.i(TAG, "Chat(): dropped signal received on hosted session "
- + ctx.sessionId + " when not joined-to-self");
- return;
- }
- /*
- * To keep the application simple, we didn't force users to choose a
- * nickname. We want to identify the message source somehow, so we just
- * use the unique name of the sender's bus attachment.
- */
- String nickname = ctx.sender;
- nickname = nickname
- .substring(nickname.length() - 10, nickname.length());
- // Log.i(TAG, "Chat(): signal " + string + " received from nickname " +
- // nickname);
- // mChatApplication.newRemoteUserMessage(nickname, "get message");
- try {
- try {
- if (file == null) {
- file = new File("/mnt/sdcard/abc.mp4");
- }
- fos = new FileOutputStream(file, true);
- } catch (Exception e) {
- Log.e(TAG, "creat file error");
- }
- try {
- fos.write(string, 0, string.length);
- } 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;
- }
- }
这样就可以进行文件的传输了,测试表明这个方法比上个方法更快更稳定了。
阅读(8290) | 评论(4) | 转发(0) |