Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Pass object via bluetooth

The Bluetooth chat example for Android is very useful to learn how to pass strings between phones - is it possible to use the same code to pass objects between phones? I have the same classes defined in both phones, I just want to pass the instance of one class from one phone to another. Is there any sample code available? I tried using serialization and replacing outputstream and inputstream in the chat example with objectoutputstream and objectinputstream but it didn't seem to work

like image 666
Fabio Avatar asked Jun 08 '11 07:06

Fabio


2 Answers

The best way I found to handle this was the following:

  1. I set up my objects as implementing Serializable that I wanted to send.
  2. I set up the following code to manage the messages:

    public byte[] serialize() throws IOException {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o = new ObjectOutputStream(b);
        o.writeObject(this);
        return b.toByteArray();
    }
    //AbstractMessage was actually the message type I used, but feel free to choose your own type
    public static AbstractMessage deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream b = new ByteArrayInputStream(bytes);
        ObjectInputStream o = new ObjectInputStream(b);
        return (AbstractMessage) o.readObject();
    
  3. I changed the write statements to accept a Serializable, and then make the final write:

    /**
     * Write to the connected OutStream.
     * @param buffer  The bytes to write
     */
    public void write(AbstractMessage buffer) {
        try {
            Log.v(TAG,"Writing \""+(buffer.serialize())+"\"");
            mmOutStream.write(buffer.serialize());
    
            // Share the sent message back to the UI Activity
            mHandler.obtainMessage(AbstractMessageManager.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }
    
like image 122
PearsonArtPhoto Avatar answered Oct 21 '22 00:10

PearsonArtPhoto


The Bluetooth Chat example is a demonstration of using the Serial Port Profile (SPP) which is based upon RFCOMM. You can serially send across any data you like once the connection is established; you simply need to be able to represent your objects into a serial stream of bytes, i.e. serialize them.

Therefore the use of serialization would certainly be a way of getting your objects sent over the link. The Bluetooth API's send and receive functions deal with arrays of bytes, but you could easily adapt the Bluetooth Chat example to use streams, e.g. the send function would read bytes out of a stream and put them into an array buffer, then you send that buffer, etc. Then the application code would simply talk via input and output stream pipes - that's one way I've done it in the past.

So there's nothing wrong with your actual idea. The bigger problem is that the way you've implemented it is not right, and more problematic still is that the way you've asked your question is quite poor, too. You need to be more descriptive about exactly what didn't work, explain what debugging you've already tried, and post code samples and Logcat outputs so we can help you properly.

Finally, I did find what I think is a bug in the Bluetooth Chat code example: The data receive function passes a reference of the receive byte array to the ArrayList that's used to show each line of text received. This is alright when small amounts of slow text are being transmitted across, but when you try to send large amounts of data, you start to see the data being corrupted, presumably because the ArrayList adapter is still reading bytes out of that same array when the array is being filled with even newer data.

like image 37
Trevor Avatar answered Oct 21 '22 00:10

Trevor