title: How do I pipe data received via bluetooth into an mp3 file between android devices?
body: <p>QUESTION: How do I pipe data received (sent via bluetooth chat sample code) into an mp3 file?</p>
  
  <p>AIM: To transfer an mp3 file from one android phone to another using a simple bluetooth file transfer method.</p>
  
  <p>METHOD: Using the bluetooth chat sample provided by the Android SDK I piped the MP3 file into a byte array and then transmitted the data to the second device.</p>
  
  <p>CURRENT STATUS: The data is received by the second device, (I checked by outputting the stream to the screen) however I am unable to open an file and pipe the data into it, give it an ".mp3" extension and then play it.</p>
  
  <p><strong>BluetoothChat.java</strong></p>
  
  <pre><code>public byte[] lilfilebuffer = null;
  
  
  private void sendMessage(String message) {
  
  
      lilfilebuffer = read(new File("/sdcard/media/07_The-organ.mp3"));
      mChatService.write(lilfilebuffer);
  
  
  }
  
  
  private byte[] read(final File file) {
      Throwable pending = null;
      FileInputStream in = null;
      final byte buffer[] = new byte[(int) file.length()];
      try {
          in = new FileInputStream(file);
          in.read(buffer);
      } catch (Exception e) {
          pending = new RuntimeException("Exception occured on reading file "
                          + file.getAbsolutePath(), e);
      } finally {
          if (in != null) {
                  try {
                          in.close();
                  } catch (Exception e) {
                          if (pending == null) {
                                  pending = new RuntimeException(
                                          "Exception occured on closing file" 
                               + file.getAbsolutePath(), e);
                          }
                  }
          }
          if (pending != null) {
                  throw new RuntimeException(pending);
          }
      }
      return buffer;
  }
  
  
  
  
  // The Handler that gets information back from the BluetoothChatService
  private final Handler mHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          switch (msg.what) {
          case MESSAGE_STATE_CHANGE:
              if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
              switch (msg.arg1) {
              case BluetoothChatService.STATE_CONNECTED:
                  setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
                  mConversationArrayAdapter.clear();
                  break;
              case BluetoothChatService.STATE_CONNECTING:
                  setStatus(R.string.title_connecting);
                  break;
              case BluetoothChatService.STATE_LISTEN:
              case BluetoothChatService.STATE_NONE:
                  setStatus(R.string.title_not_connected);
                  break;
              }
              break;
          case MESSAGE_WRITE:
              byte[] writeBuf = (byte[]) msg.obj;
  
              try {
                  FileOutputStream out = new FileOutputStream("/sdcard/media/testsong.mp3");
                  out.write(writeBuf);
                  out.close();
  
                  } 
                  catch (IOException e) 
                  { 
                  //System.out.println("Exception ");
  
                  }
              break;
          case MESSAGE_READ:
              byte[] readBuf = (byte[]) msg.obj;
  
              try {
                  FileOutputStream out = new FileOutputStream("/sdcard/media/testsong.mp3");
  
                  out.write(readBuf);
                  out.close();
  
  
  
                  } 
                  catch (IOException e) 
                  { 
                  //System.out.println("Exception ");
  
                  }
  
  
  
              break;
          case MESSAGE_DEVICE_NAME:
              // save the connected device's name
              mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
              Toast.makeText(getApplicationContext(), "Connected to "
                             + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
              break;
          case MESSAGE_TOAST:
              Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                             Toast.LENGTH_SHORT).show();
              break;
          }
      }
  };
  </code></pre>
  
  <p><strong>BluetoothChatService.java</strong></p>
  
  <pre><code>public void write(byte[] out) {
  
      ConnectedThread r;
  
      synchronized (this) {
          if (mState != STATE_CONNECTED) return;
          r = mConnectedThread;
      }
  
      r.write(out);
  }
  
  private class ConnectedThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final InputStream mmInStream;
      private final OutputStream mmOutStream;
  
      public ConnectedThread(BluetoothSocket socket, String socketType) {
          Log.d(TAG, "create ConnectedThread: " + socketType);
          mmSocket = socket;
          InputStream tmpIn = null;
          OutputStream tmpOut = null;
  
  
          try {
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream();
          } catch (IOException e) {
              Log.e(TAG, "temp sockets not created", e);
          }
  
          mmInStream = tmpIn;
          mmOutStream = tmpOut;
      }
  
      public void run() {
          Log.i(TAG, "BEGIN mConnectedThread");
          byte[] buffer = new byte[1024];
          int bytes;
  
  
          while (true) {
              try {
  
                  bytes = mmInStream.read(buffer);
  
  
                  mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "disconnected", e);
                  connectionLost();
                  // Start the service over to restart listening mode
                  BluetoothChatService.this.start();
                  break;
              }
          }
      }
  
      public void write(byte[] buffer) {
          try {
              mmOutStream.write(buffer);
  
  
              mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                      .sendToTarget();
          } catch (IOException e) {
              Log.e(TAG, "Exception during write", e);
          }
      }
  
  
  }}
  </code></pre>