0

我需要一些指导来使用远程设备或服务器传输文件或数据。帮助我找出最近 5 天出现此错误的代码中发生的情况:

W/System.err: java.io.IOException: bt socket closed, read return: -1 
W/System.err: at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:495) 08-12 03:49:13.242 6973-11690/io.connection.bluetooth 
W/System.err: at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 08-12 03:49:13.242 6973-11690/io.connection.bluetooth 
W/System.err: at io.connection.bluetooth.Thread.AcceptThread$readFile.run(AcceptThread.java:90)

代码:

package io.connection.bluetooth.Thread;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Environment; 
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import io.connection.bluetooth.utils.Constants;


public class AcceptThread extends Thread {
private final BluetoothServerSocket serverSocket;
private static final String TAG = "AcceptThread";
BluetoothSocket socket = null;

public AcceptThread(BluetoothAdapter bluetoothAdapter) {
    BluetoothServerSocket tmp = null;

    try {
        tmp = bluetoothAdapter
                .listenUsingRfcommWithServiceRecord(
                        Constants.NAME_UUID, Constants.uuid);

    } catch (IOException e) {
    }
    serverSocket = tmp;
 }

public void run() {


    while (true) {
        try {
            socket = serverSocket.accept();

            if (socket.isConnected()) {
                Log.d(TAG, "run:  connection successfull");
                Log.d(TAG, "run: " + socket.getRemoteDevice().getName() + "  " +
                        socket.getRemoteDevice().getAddress());
                readFile readfile = new readFile(socket);
                readfile.start();
            }


        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "run: " + e.getMessage());
        }

    }
}

class readFile extends Thread {
    private static final String TAG = "readFile";
    BluetoothSocket socket = null;
    InputStream in = null;
    OutputStream out = null;

    readFile(BluetoothSocket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        Log.d(TAG, "run:  reading Start");
        byte[] bytes = new byte[16 * 1024];
        int count;
        int total = 0;
        String filename = System.nanoTime() + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), filename);
        Log.d(TAG, "run:  file path "+file.getPath());

        try {
            in = socket.getInputStream();
            out = new FileOutputStream(file);

            try {
   //got error on this while loop after reading all data from stream
         while ((count = in.read(bytes)) > 0) {
                    total += count;
                    out.write(bytes, 0, count);
                    Log.d(TAG, "run: " + total + "    " + count + "  " + in.available());

                }
                Log.d(TAG, "run: count End " + in.available());
                out.close();
                in.close();
                socket.close();

            } catch (Exception e) {
                e.printStackTrace();

            }

        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "run:  readFile  " + e.getMessage());
        } finally {
            try {

                Log.d(TAG, "run: socket close");
                socket.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }


   }
 }  
4

1 回答 1

0

问题:

“如果由于到达流的末尾而没有可用的字节,则返回值-1。此方法阻塞,直到输入数据可用,检测到流的末尾或引发异常。” https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

您正在到达流的末尾,但仍在 while 循环检查中调用 read()。最后一个 read() 可能会占用 <1024 字节,但仍大于零,因此 while 循环将再进行一次迭代(这是预期的)。但是当它尝试再次检查读取的字节数时,它会遇到错误。

笔记:

公共 int 读取(字节 [] b)

“InputStream 类的 read(b) 方法具有与以下相同的效果:

读(b, 0, b.length)"

public int read(byte[] b, int off, int len) 抛出 IOException

“InputStream 类的 read(b, off, len) 方法只是重复调用方法 read()”

于 2016-08-11T20:53:04.557 回答