Problem sending Android Bluetooth files

I am writing a small program to send a file between Android and PC via bluetooth. I already read the example of bluetooth chat on google android website.

Currently, my version works fine with sending a text message via Bluetooth, but when I send multiple files, about> = 20 KB, it stops working and throws an EOFException, as shown 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 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(); } 

On my pc, I read:

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()); } 
+6
source share
1 answer

You need to add after flushing the output stream

 out.close(); 

otherwise the flow may become damaged.

+1
source

Source: https://habr.com/ru/post/887846/


All Articles