简体   繁体   English

Android蓝牙发送文件问题

[英]Android Bluetooth sending file problem

I am writing a small program to send file between Android and PC through bluetooth. 我正在编写一个小程序,通过蓝牙在Android和PC之间发送文件。 I already read the bluetooth chat example in google android site. 我已经阅读了谷歌android站点中的蓝牙聊天示例。

Currently, my version works really well with sending a text message via bluetooth, but when I send some files, around >= 20 KB, it stops working and throwing EOFException as below: 目前,我的版本通过蓝牙发送文本消息非常有效,但是当我发送一些文件时,大约> = 20 KB,它会停止工作并抛出EOFException,如下所示:

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)

Currently, my java program on the PC using bluecove-2.1.0 目前,我的PC上的java程序使用的是bluecove-2.1.0

Here are my main codes: 这是我的主要代码:

In Android: 在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();        
}

In My PC, I read it: 在我的电脑中,我读到它:

PC Version, server 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());   
}

You need to add after flushing the outputstream 您需要在刷新输出流后添加

out.close();

otherwise the stream can become corrupted. 否则流可能会被破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM