Java Android蓝牙发送文件问题

Java Android蓝牙发送文件问题,java,android,sockets,bluetooth,desktop,Java,Android,Sockets,Bluetooth,Desktop,我正在写一个小程序,通过蓝牙在Android和PC之间发送文件。我已经 阅读谷歌安卓网站上的蓝牙聊天示例 目前,我的版本非常适合通过蓝牙发送文本消息,但当我发送一些大于等于20 KB的文件时,它会停止工作并引发EOFEException,如下所示: java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2716) at java.io.

我正在写一个小程序,通过蓝牙在Android和PC之间发送文件。我已经 阅读谷歌安卓网站上的蓝牙聊天示例

目前,我的版本非常适合通过蓝牙发送文本消息,但当我发送一些大于等于20 KB的文件时,它会停止工作并引发EOFEException,如下所示:

java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2716)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1665)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1963)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1887)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
    at com.test.pcserver.BluetoothServerListener.run(BluetoothServerListener.java:74)
    at java.lang.Thread.run(Thread.java:636)
目前,我的java程序在PC上使用
bluecove-2.1.0

以下是我的主要代码:

在Android中:

// Get the BLuetoothDevice object
if (BluetoothAdapter.checkBluetoothAddress(address)) {
    device = mBtAdapter.getRemoteDevice(address);

        // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    socket = device .createRfcommSocketToServiceRecord(ProgramConstants.BLUETOOTH_UUID);
    socket.connect();

        out = new ObjectOutputStream(socket.getOutputStream());

    // Send it to PC
    out.writeObject(contentObject);
    out.flush();        
}
在我的电脑里,我读到:

PC版本,服务器

StreamConnectionNotifier streamConnNotifier = null;

// Create the service url
String connectionString = "btspp://localhost:" + ProgramConstants.BLUETOOTH_UUID.toString()
                    + ";name=myappname";
// open server url
streamConnNotifier = (StreamConnectionNotifier) Connector.open(connectionString);

while (true) {
  // Wait for client connection
  StreamConnection connection = streamConnNotifier.acceptAndOpen();
  ObjectInputStream in = new ObjectInputStream(connection.openInputStream());
  RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);

  // read string from spp client
  DataInController data = new DataInController(model);
  data.processDataIn(in.readObject(), dev.getBluetoothAddress());   
}

您需要在刷新outputstream后添加

out.close();

否则,流可能会损坏。

ProgramConstants.BLUETOOTH_UUID.toString()的值是多少?您是否曾经使用过此功能?嘿,您能分享完整的源代码吗。??提前谢谢。@vodkhang,你能分享在PC上运行的完整源代码吗?你能满足这个需要吗。