title: How to serialize an object and then send it over bluetooth
body: <p>I'm making a Battleships game and I want to send an Array of a class named Ships(which contains stuff like ship name, size, rotated or not and an arraylist for coordinates). I've googled this and looked on Stack overflow and I basically need to serialize the array, but this is where I'm stuck. I need to use ObjectOutputStream, but how do I encorporate that into the code below (taken from android dev site). Note I have already made the ship class implement serializable. Thanks in advance</p>
  
  <pre><code>public class ConnectedThread extends Thread {
          private final BluetoothSocket mmSocket;
          private final InputStream mmInStream;
          private final OutputStream mmOutStream;
  
          public ConnectedThread(BluetoothSocket socket) {
              Log.d(TAG, "connectedthread started");
              // mHandler.obtainMessage(TEST).sendToTarget();
              mmSocket = socket;
              InputStream tmpIn = null;
              OutputStream tmpOut = null;
  
              // Get the input and output streams, using temp objects because
              // member streams are final
              try {
  
                  tmpIn = socket.getInputStream();
                  tmpOut = socket.getOutputStream();
  
              } catch (IOException e) {
                  Log.e(TAG, "temp sockets not created");
              }
  
              mmInStream = tmpIn;
              mmOutStream = tmpOut;
  
          }
  
          public void run() {
              Log.i(TAG, "Begin mConnectedThread");
              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) {
                  try {
                      // Read from the InputStream
                      bytes = mmInStream.read(buffer);
                      // Send the obtained bytes to the UI activity
                      Log.i(TAG, "reaaaad msg");
                      mHandler.obtainMessage(SetUpGame.MESSAGE_READ2, bytes, -1, buffer).sendToTarget();
                  } catch (IOException e) {
                      Log.e(TAG, "disconnectd");
                      break;
                  }
              }
          }
  
          /*
           * Call this from the main activity to send data to the remote
           * device
           */
          public void write(byte[] buffer) {
  
              try {
  
                  mmOutStream.write(buffer);
                  Log.i(TAG, "writeeee msg");
                  mHandler.obtainMessage(SetUpGame.MESSAGE_WRITE, -1,-1, buffer).sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "Exception during write");
              }
          }
  
          /* Call this from the main activity to shutdown the connection */
          public void cancel() {
              try {
                  mmSocket.close();
              } catch (IOException e) {
                  Log.e(TAG, "close of connect socket failed");
              }
          }
  
      }
  </code></pre>
  
  <p>and my handler:</p>
  
  <pre><code>        final Handler mHandler = new Handler() {
          @Override
          public void handleMessage(android.os.Message msg) {
  
              switch (msg.what) {
              case MESSAGE_READ2:
  
                  byte[] readBuf = (byte[]) msg.obj;
                  String readMessage = new String(readBuf, 0, msg.arg1);
                  break;
  
              case MESSAGE_WRITE:
                  byte[] writeBuf = (byte[]) msg.obj;
                  String writeMessage = new String(writeBuf);
                  //Toast.makeText(getApplicationContext(),"Me:" + writeMessage, Toast.LENGTH_SHORT).show();
                  break;
  </code></pre>