1

I have a class that containts two class objects with some variables. I want to send a instance of the class via USB with a send function (and recieve on the other side). The send function accepts byte arrays (byte[]).

My question is how do i convert the instance of the class to a byte array so that i can send it? And how do i properly reconstruct it on the other side?

Below is te class Comsstruct that i want to send and recieve. Any sugestions are welcome!

     // ObjectInfo struct definition
public class ObjectInfo {
    int ObjectXCor;
    int ObjectYCor;
    int ObjectMass;
};

// ObjectInfo struct definition
public class SensorDataStruct{
    int PingData;
    int IRData;
    int ForceData;
    int CompassData;
};

// ObjectInfo struct definition
public class CommStruct{
        public ObjectInfo VisionData;
        public SensorDataStruct SensorData;
};

public CommStruct SendPacket;
public CommStruct RecievePacket;

Update

I have found a solution but with the suggestions that i got i wonder if this will work (and if it is a good solution)?

There is a serialsation method and a send method:

// Method to convert object to a byte array
public static byte[] serializeObject(CommStruct obj) throws IOException
{
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bytesOut);
    oos.writeObject(obj);
    oos.flush();
    byte[] bytes = bytesOut.toByteArray();
    bytesOut.close();
    oos.close();
    return bytes;
}

// Send struct function
public void Send(){

    try
{
        // First convert the CommStruct to a byte array
        // Then send the byte array
        server.send(serializeObject(SendPacket));

} 
    catch (IOException e)
{
    Log.e("microbridge", "problem sending TCP message", e);
}   

And a recieve handler function:

    public void onReceive(com.example.communicationmodulebase.Client client, byte[] data)
{

    // Event handler for recieving data


    // Try to receive CommStruct 
    try
    {
        ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
        ObjectInputStream ois = new ObjectInputStream(bytesIn);
        RecievePacket = (CommStruct) ois.readObject();
        ois.close();
    } 
    catch (IOException e)
    {
        Log.e("microbridge", "problem recieving TCP message", e);
    } 
    catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("microbridge", "problem recieving TCP message", e);
    }

    // Send the recieved data packet back
    SendPacket = RecievePacket;

    // Send CommStruct
    Send();
}

My question is if this is a good solution or not?

Roy08
  • 253
  • 2
  • 7
  • 21
  • http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array – Chill Oct 28 '13 at 19:29
  • In your comment to my answer you said that the receiver would be running on a Arduino? The receiver you've written is in Java? I'm sorry but it's hard to give you a decent answer without knowing more precisely what it is that you're trying to accomplish. – Daniel Figueroa Oct 28 '13 at 23:12

2 Answers2

6

You could have your classes implement the Serializable interface, and then send that over a bytestream. But that's a really bad idea and will most probably not work.

The reasons for why this wouldn't work, or isn't guaranteed to work, is that there's no guarantee that different implementations of java will use the same serializing mechanism, thus the bytearray could very well differ in different Java implementations.

Instead I'd recommend you to convert the objects to some intermediary format like xml or json and send that instead. That way the receiver doesn't even have to be coded in java.

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • Thank you for your comment, the otherside is a Arduino microcontroller. The two systems are connected via the adb/usb. The arduino code is C/C++. I have found some solution and will put that in post under update. Do you think that is a good soluition and will work or use JSON? – Roy08 Oct 28 '13 at 20:06
  • The C/C++ code will not be able to read/write serialized java objects! – Arne Burmeister Oct 28 '13 at 20:24
  • @ArneBurmeister : Thanks your for your comment, maby astupit question but why is that? I'm using the Microbridge server for adb connection between the Android Phone and a Arduino with usb shield. – Roy08 Oct 29 '13 at 11:15
  • @Roy08 It's because they both use completely different languages. I may speak swedish and you may speak english. If I write something down in swedish you might recognize the letters but you won't understand anything. So sure you can send a bunch of bytes from your Android to an arduino, but that doesn't mean that the arduino is capable of making sense of what it is receiveing. – Daniel Figueroa Oct 29 '13 at 11:56
  • @ Daniel Figeuroa : Thanks you for your comment, i use the Microbridge server and Microbridge adb Arduino library. I managed to send bytes and integers from and to the Arduino. So if i understand what you are saying it is not possible even with the library due to the languages? – Roy08 Oct 29 '13 at 12:25
1

You can reference here: Convert object to byte array and convert byte array to object http://www.java2s.com/Code/Java/File-Input-Output/Convertobjecttobytearrayandconvertbytearraytoobject.htm