2011-05-11 83 views
6

我正在寫一個小程序,通過藍牙在Android和PC之間發送文件。我已經 在google android網站上閱讀藍牙聊天的例子。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) 

目前,

在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();   
} 
0123使用 bluecove-2.1.0

這裏是我的主要代碼在PC上我的java程序

在我的電腦,我的理解是:

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

ProgramConstants.BLUETOOTH_UUID.toString()的值是什麼? – Milhous 2011-06-23 19:59:33

+0

你有沒有得到這個工作? – Jack 2011-08-11 19:16:22

+0

嘿,你可以請分享你的完整源代碼。提前致謝。 – anddev 2012-03-29 05:52:18

回答

1

您需要衝洗的OutputStream

out.close(); 

否則流可能會被破壞後添加。