gpt4 book ai didi

android - 蓝牙文件传输安卓

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:45 27 4
gpt4 key购买 nike

我在通过蓝牙套接字发送大文件时遇到问题。较小的文件可以正确传输。我相信最多可以正确传输 161280 个字节。

编辑:我做了更多测试并缩小了原因范围。好像是

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

发送代码部分写入的字节数不超过 161280。我通过不关闭套接字连接看到了这种行为,从而导致接收部分的 read 在 161280 字节处“阻塞”。这里的蓝牙输出流有什么问题?我做错了什么?

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

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

发送代码:

    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();

接收码:

// 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();

日志:

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)

我正在使用摩托罗拉里程碑。安卓 2.1

最佳答案

我能够通过将小块数据发送到蓝牙外流来解决这个问题。事实证明,8 * 1024 是一个很好的缓冲区大小,它有助于通过流无缝发送数据以及防止接收端的数据损坏。

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);
}

关于android - 蓝牙文件传输安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9748219/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com