title: Bluetooth Handler in Android
body: <p>Can someone explain what the following Handler does? and how do we use a handler? I am new to Android and java, so please bear with me</p>
  
  <p>here's the code snippet from the first class </p>
  
  <pre><code> 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:
                      mTitle.setText(R.string.title_connected_to);
                      mTitle.append(mConnectedDeviceName);
                      mConversationArrayAdapter.clear();
                      break;
                  case BluetoothChatService.STATE_CONNECTING:
                      mTitle.setText(R.string.title_connecting);
                      break;
                  case BluetoothChatService.STATE_LISTEN:
                  case BluetoothChatService.STATE_NONE:
                      mTitle.setText(R.string.title_not_connected);
                      break;
                  }
                  break;
              case MESSAGE_WRITE:
                  byte[] writeBuf = (byte[]) msg.obj;
                  // construct a string from the buffer
                  String writeMessage = new String(writeBuf);
                  mConversationArrayAdapter.add("Me:  " + writeMessage);
                  break;
              case MESSAGE_READ:
                  byte[] readBuf = (byte[]) msg.obj;
                  // construct a string from the valid bytes in the buffer
                  String readMessage = new String(readBuf, 0, msg.arg1);
                  mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
                  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>here's the code from the connectThread class</p>
  
  <pre><code>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;
  
              // Get the BluetoothSocket input and output streams
              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;
  
              // Keep listening to the InputStream while connected
              while (true) {
                  try {
                      // Read from the InputStream
                      bytes = mmInStream.read(buffer);
  
                      // Send the obtained bytes to the UI Activity
                      mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                              .sendToTarget();
                  } catch (IOException e) {
                      Log.e(TAG, "disconnected", e);
                      connectionLost();
                      break;
                  }
              }
          }
  
          /**
           * Write to the connected OutStream.
           * @param buffer  The bytes to write
           */
          public void write(byte[] buffer) {
              try {
                  mmOutStream.write(buffer);
  
                  // Share the sent message back to the UI Activity
                  mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "Exception during write", e);
              }
          }
  
          public void cancel() {
              try {
                  mmSocket.close();
              } catch (IOException e) {
                  Log.e(TAG, "close() of connect socket failed", e);
              }
          }
      }
  }
  </code></pre>
  
  <p>Now please do explain as to how they are used. If there are any good tutorials , give that too</p>