-3

I am currently working on sending an object (Shopping) from an user to another via Bluetooth. When i click on the "SEND" button from my server phone, the object Shopping is sent correctly, and i print it on my terminal via Log.d(StringNameOfShopping)

But i can only send bytes[] array, that is transformed to int bytes then creates a new String(buffer[], offset, bytes)

So is there a method to cast from String (that has my object Shopping reference for example : Shopping@bd429a9) to a Shopping object ?

Here is my methods for listening to Input and Output.

    public void run(){

        byte[] buffer = new byte[1024];  // buffer store for the stream

        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            // Read from the InputStream
            try {
                bytes = mmInStream.read(buffer);
                String incomingMessage = new String(buffer, 0, bytes);
                Log.d(TAG, "InputStream: " + incomingMessage);
            } catch (IOException e) {
                Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
                break;
            }
        }
    }

    //Call this from the main activity to send data to the remote device
    public void write(byte[] bytes) {
        String text = new String(bytes, Charset.defaultCharset());
        Log.d(TAG, "write: Writing to outputstream: " + text);
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) {
            Log.e(TAG, "write: Error writing to output stream. " + e.getMessage() );
        }
    }

And here are my Serialize/Deserialize methods (But where should i put them ? In my MainActivity, or Shopping Class? Or Bluetooth class ?

public byte[] serialize(Shopping shopping) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(shopping);
    return b.toByteArray();
}

//AbstractMessage was actually the message type I used, but feel free to choose your own type
public static Shopping deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream b = new ByteArrayInputStream(bytes);
    ObjectInputStream o = new ObjectInputStream(b);
    return (Shopping) o.readObject();
}

1 Answers1

0

This won't work, because the other phone cannot resolve the object in its own memory. You need to serialize your object on one phone and deserialize it on the other phone.

Concerning your edit: You've decided to use the Serialization mechanism of Java. For this to work properly you need to implement the Serializable interface in Shopping. This is just a "marker interface", i.e. it has no methods and just indicates, that the class can be used with the serialization facilities of Java.

The next step is to call the serialize method with the Shopping instance that you want to transmit. This gives you a byte array containing the serialized object. Now you can call the write function with this byte array.

On the receiving side you need to read the entire input stream into a byte array. This array can then be passed to deserialize to obtain the Shopping instance.

Jannik
  • 1,583
  • 1
  • 14
  • 22
  • What should i serialize ? The class Shopping ? Thanks – Nouamane Azzouzi May 11 '19 at 12:48
  • Exactly. Serialization takes an object and transforms it into a string (or byte array) representation. This string can be sent to the other phone and there the deserialization will recreate an object with the same content. There are many serialization techniques, for example xml, json or the built-in Java serialization. – Jannik May 11 '19 at 12:55
  • implement parcelable to your object (pojo/entity) – hemen May 11 '19 at 12:56
  • I edited my post and included the codes, can you please take a look at it ? thanks – Nouamane Azzouzi May 11 '19 at 13:15