在Android姜饼传输大量的数据通过蓝牙 [英] Transferring large amounts of data over bluetooth on Android Gingerbread

查看:121
本文介绍了在Android姜饼传输大量的数据通过蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个android手机传输有关的任意数据的兆字节的时间到另一个。目前,我写的大小,一个命令code和数据到 DataOutputStream类围绕的BufferedOutputStream ,围绕在从的OutputStream 返回 bluetoothSocketInstance.getOutputStream()

的接收电话读取大小和命令code和然后从输入流中读取,直到它已经得到了所有它期待的数据。这适用于短字符串,但对于更大的文件不是所有的数据被传输。运行在调试器应用程序显示写入返回而没有任何例外与所读取的读出无限期预期的字节,然后块的一小部分。它也不会引发任何异常。

有一个缓冲的地方,是填补了?有没有别的东西,我需要做的,以确保所有的数据被传输?

我的code的发送者和接收者是如下:

发件人:

 尝试{

            DataOutputStream类D =新DataOutputStream类(新的BufferedOutputStream(mmOutStream,buffer.length + 8));
            // INT B = buffer.length;
            d.writeInt(buffer.length);
            d.writeInt(命令);

            d.write(缓冲液);
            d.flush();

        }赶上(IOException异常E){
            Log.e(TAG,在写例外,E);
        }
    }
 

接收器:

 尝试{

                //读取从InputStream
                INT messageSize = inStream.readInt();
                整型消息code = inStream.readInt();
                字节= 0;
                缓冲区=新的字节[messageSize]
                而(字节< messageSize)
                {
                    字节+ = inStream.read(缓冲器,字节,messageSize  - 字节);

                }
                    消息=字节;
            }赶上(IOException异常E){
                Log.e(TAG,断开,E);
                失去连接();
                打破;
            }
 

解决方案

在我的结束一些更多的测试,我改变了我送code看起来是这样的:

 的for(int i = 0; I< buffer.length; I + = BIG_NUM)
            {
                INT B =((1 + BIG_NUM)< buffer.length)? BIG_NUM:buffer.length  - 我;
                d.write(缓冲,I,B);
                d.flush();
            }
 

该文件现在被发送。有没有人有一个想法,为什么?是否调用的flush()块,直到数据实际上已经被转移?是否有对发送的大小的任何文件和接收缓冲区,这将有助于我来决定我是大可以放心地让 BIG_NUM

I'm trying to transfer about a megabyte of arbitrary data at a time from one android phone to another. Currently, I write the size, a command code and the data to a DataOutputStream around a BufferedOutputStream, around the OutputStream returned from bluetoothSocketInstance.getOutputStream().

The receiving phone reads the size and command code and then reads from the input stream until it has gotten all the data it is expecting. This works for short strings, but for larger files not all the data is transferred. Running the app in the debugger shows that the write returns without any exceptions and the read reads a fraction of the bytes expected and then blocks indefinitely. It also does not throw any exceptions.

Is there a buffer somewhere that is filling up? Is there something else I need to do to ensure that all the data gets transferred?

My code for the sender and receiver are below:

Sender:

  try {

            DataOutputStream d = new DataOutputStream(new BufferedOutputStream(mmOutStream,buffer.length+8));
            //int b= buffer.length;
            d.writeInt(buffer.length);
            d.writeInt(command);

            d.write(buffer);
            d.flush();

        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

Receiver:

 try {

                // Read from the InputStream
                int messageSize= inStream.readInt();
                int messageCode = inStream.readInt();
                bytes=0;
                buffer =new byte[messageSize];
                while(bytes < messageSize)
                {
                    bytes += inStream.read(buffer,bytes,messageSize - bytes);

                }
                    message = bytes;
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }

解决方案

After some more testing on my end, I changed my sending code to look like this:

for(int i=0; i<buffer.length;i+=BIG_NUM)
            {
                int b = ((i+BIG_NUM) < buffer.length) ? BIG_NUM: buffer.length - i;
                d.write(buffer,i,b);
                d.flush();
            }

The files now get sent. Does anyone have an idea why? Does the call to flush() block until the data has actually been transferred? Is there any documentation about the size of the send and receive buffers that would help me to decide how large I can safely make BIG_NUM?

这篇关于在Android姜饼传输大量的数据通过蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆