Android 当通过蓝牙发送序列化对象时,StreamCorruptedException

Android 当通过蓝牙发送序列化对象时,StreamCorruptedException,android,bluetooth,inputstream,outputstream,serializable,Android,Bluetooth,Inputstream,Outputstream,Serializable,我有一个序列化的类,需要通过Bluetooth作为对象发送,还实现了Runnable。因此,我首先设置了几个变量,然后将其作为一个对象发送给另一个Android设备执行,然后另一个Android设备将其结果设置为一个变量,并将相同的对象和结果发送回其中一个变量。在通过BluetoothSocket的OutputStream发送之前,我一直在使用以下两种方法序列化我的对象并获取ByteArray,然后反序列化该ByteArray以获取我的对象 public static byte[] serial

我有一个序列化的类,需要通过Bluetooth作为对象发送,还实现了Runnable。因此,我首先设置了几个变量,然后将其作为一个对象发送给另一个Android设备执行,然后另一个Android设备将其结果设置为一个变量,并将相同的对象和结果发送回其中一个变量。在通过BluetoothSocket的OutputStream发送之前,我一直在使用以下两种方法序列化我的对象并获取ByteArray,然后反序列化该ByteArray以获取我的对象

public static byte[] serializeObject(Object o) throws IOException { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    ObjectOutput out = new ObjectOutputStream(bos);
    out.writeObject(o);  
    out.flush();
    out.close();

    // Get the bytes of the serialized object 
    byte[] buf = bos.toByteArray(); 

    return buf; 
} 

public static Object deserializeObject(byte[] b) throws OptionalDataException, ClassNotFoundException, IOException { 
    ByteArrayInputStream bis = new ByteArrayInputStream(b);

    ObjectInputStream in = new ObjectInputStream(bis); 
    Object object = in.readObject(); 
    in.close(); 

    return object; 
}
我仍然从这两个方法中得到相同的错误,因此我尝试将其与通过BluetoothSocket的OutputStream发送ByteArray的方法合并,如下所示

public void sendObject(Object obj) throws IOException{
    ByteArrayOutputStream bos = new ByteArrayOutputStream() ; 
    ObjectOutputStream out = new ObjectOutputStream( bos );
    out.writeObject(obj); 
    out.flush();
    out.close();

    byte[] buf = bos.toByteArray();
    sendByteArray(buf);
}
public void sendByteArray(byte[] buffer, int offset, int count) throws IOException{
    bluetoothSocket.getOutputStream().write(buffer, offset, count);
}

public Object getObject() throws IOException, ClassNotFoundException{
    byte[] buffer = new byte[10240];
    Object obj = null;
    if(bluetoothSocket.getInputStream().available() > 0){
        bluetoothSocket.getInputStream().read(buffer);

        ByteArrayInputStream b = new ByteArrayInputStream(buffer);
        ObjectInputStream o = new ObjectInputStream(b);
        obj = o.readObject();
    }
    return obj;
}
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readStreamHeader (ObjectInputStream.java:2102)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
and so on...
最后,在接收设备上反序列化对象时,我得到了相同的错误,如下所示

public void sendObject(Object obj) throws IOException{
    ByteArrayOutputStream bos = new ByteArrayOutputStream() ; 
    ObjectOutputStream out = new ObjectOutputStream( bos );
    out.writeObject(obj); 
    out.flush();
    out.close();

    byte[] buf = bos.toByteArray();
    sendByteArray(buf);
}
public void sendByteArray(byte[] buffer, int offset, int count) throws IOException{
    bluetoothSocket.getOutputStream().write(buffer, offset, count);
}

public Object getObject() throws IOException, ClassNotFoundException{
    byte[] buffer = new byte[10240];
    Object obj = null;
    if(bluetoothSocket.getInputStream().available() > 0){
        bluetoothSocket.getInputStream().read(buffer);

        ByteArrayInputStream b = new ByteArrayInputStream(buffer);
        ObjectInputStream o = new ObjectInputStream(b);
        obj = o.readObject();
    }
    return obj;
}
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readStreamHeader (ObjectInputStream.java:2102)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
and so on...
java.io.StreamCorruptedException
位于java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2102)
位于java.io.ObjectInputStream。(ObjectInputStream.java:372)
等等
有人能帮忙吗?我很绝望,这已经折磨了我好几个星期了,几天后我就需要这个了S

根据和线程:

在套接字的生命周期中,应该使用单个
ObjectOutputStream
ObjectInputStream
,并且不要在套接字上使用任何其他流

其次,使用
ObjectOutputStream.reset()
清除以前的值


让我知道这是否有效

尝试删除
out.flush()
或将其替换为
out.reset()
。让我知道这是否有效!堆栈满后跟踪您没有使用反序列化对象。您不是在测试所读取缓冲区的长度或完整性。我不明白首先将对象放入字节数组,而不是在蓝牙操作系统上打开OOS的意义。您不知道或者无法知道缓冲区的内容实际上包含一个完整的对象,并且从正确的位置开始。