蓝牙文件传输的Andr​​oid [英] Bluetooth file transfer Android

查看:126
本文介绍了蓝牙文件传输的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过蓝牙套接字发送大文件面临的一个问题。更小的文件获得正确传输。我相信,高达161280字节被正确传送。

I am facing a problem in sending large files over bluetooth sockets. Smaller files get transferred correctly. I believe upto 161280 bytes get transferred correctly.

编辑:我做了一些更多的测试和缩小的原因。看来,

I did some more testing and narrowed down the cause. It seems that

outStream.write(mybytearray, 0, mybytearray.length);

在发送code部分不是写超过161280字节。我看到这种行为通过不关闭套接字连接,从而使在接收部分块上的161280字节读。什么是错在这里蓝牙输出流?我究竟做错了什么?

in the sending code part is NOT writing more than 161280 bytes. I saw this behavior by not closing the socket connection, thereby causing the read in the receiving part to "block" on 161280 bytes. What is wrong with the bluetooth output stream here? What am I doing wrong?

编辑2: 这样做让它通过。

for(int i = 0 ; i < mybytearray.length ; i++){
    outStream.write(mybytearray[i]);
}

发送code:

    try {
        outStream = mBluetoothSocket.getOutputStream();
        Log.d(TAG,"outStream created success!");
    } catch (IOException e) {
        Log.d(TAG,
                "ON RESUME: Output stream creation failed.",
                e);
    }

    File myFile = new File(file_name);
    Log.d(TAG,"file /source.pdf created success!");

    byte[] mybytearray = new byte[(int)myFile.length()];
    Log.d(TAG,"file length() =" + (int)myFile.length());

    FileInputStream fis = new FileInputStream(myFile);
    Log.d(TAG,"fis created");

    BufferedInputStream bis = new BufferedInputStream(fis,1272254 );
    Log.d(TAG,"bis created success");

    bis.read(mybytearray,0,mybytearray.length);
    Log.d(TAG,"ALL Bytes read from bis");

    outStream.write(mybytearray, 0, mybytearray.length);
    Log.d(TAG,"BYTES WRITTEN to OUTSTREAM of socket");


    outStream.flush();
    Log.d(TAG,"bytes flushed");
    outStream.close();

接收code:

// Attach the i/p stream to the socket
    try {
        InputStream in = socket.getInputStream();
        mIn = in;
        Log.d(TAG, "input stream acquired");

    } catch (IOException e1) {
        e1.printStackTrace();
    }
    // Create output streams & write to file
    FileOutputStream fos = new FileOutputStream(
            Environment.getExternalStorageDirectory()
                    + "/copy.pdf");
    try {
        bytesRead = mIn.read(buffer, 0, buffer.length);
        Log.d(TAG, "bytesRead first time =" + bytesRead);
        current = bytesRead;

        do {
            Log.d(TAG, "do-while -- current: " + current);
            bytesRead = mIn.read(buffer, current,
                    buffer.length - current);
            Log.d(TAG, "bytesRead: =" + bytesRead);

            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);
    } catch (IOException e) {
        e.printStackTrace();
        Log.d(TAG, "do while end:-- buffer len= "
                + buffer.length + "  current: " + current);

        fos.write(buffer);
        Log.d(TAG, "fos.write success! buffer: "
                + buffer.length + "  current: " + current);

        fos.flush();
        fos.close();
    }
}
socket.close();

logcat的:

D/ReceiveService( 5761): do-while -- current: 155232
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 156240
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 157248
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 158256
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 159264
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 160272
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 161280
W/System.err( 5761): java.io.IOException: Software caused connection abort
W/System.err( 5761):    at android.bluetooth.BluetoothSocket.readNative(Native Method)
W/System.err( 5761):    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:307)
W/System.err( 5761):    at   android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
W/System.err( 5761):    at com.bt.server.ReceiveService$AcceptThread.run(ReceiveService.java:141)

我使用的摩托罗拉Milestone。 Android 2.1的

I am using Motorola Milestone. Android 2.1

推荐答案

我可以通过发送一小段数据输出到蓝牙outstream来解决这个问题。原来,8 * 1024是一个很好的缓冲大小,这有助于无缝地发送数据在流以及数据的接收端preventing腐败。

I was able to solve this problem by sending small chunks of data out to the bluetooth outstream. It turned out that 8 * 1024 was a good buffer size, which helped in sending out data seamlessly over the stream as well as preventing corruption of data at the receiving end.

BufferedInputStream bis = new BufferedInputStream(fis, 8 * 1024);


byte[] buffer = new byte[8192];
int len
while ((len = bis.read(buffer)) != -1) {
    outStream.write(buffer, 0, len);
}

这篇关于蓝牙文件传输的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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