Android蓝牙发送文件问题

发布于 2024-11-06 16:12:13 字数 2365 浏览 3 评论 0原文

我正在编写一个小程序,通过蓝牙在 Android 和 PC 之间发送文件。我已经 阅读 google android 网站中的蓝牙聊天示例。

目前,我的版本通过蓝牙发送短信效果非常好,但是当我发送一些文件时,大约> = 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)

目前,我在 PC 上的 java 程序使用 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中,我读到:

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

I am writing a small program to send file between Android and PC through bluetooth. I already
read the bluetooth chat example in google android site.

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:

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

Here are my main codes:

In 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2024-11-13 16:12:13

您需要在刷新输出流后添加,

out.close();

否则流可能会损坏。

You need to add after flushing the outputstream

out.close();

otherwise the stream can become corrupted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文