title: Bluetooth multiple UUID's
body: <p>I implemented a <code>BluetoothService</code> where i can connect between two devices. The next step is to connect to devices with different UUID's. I have three Devices:</p>
  
  <ul>
  <li>Nexus 7</li>
  <li>Nexus 4</li>
  <li>Microcontroller</li>
  </ul>
  
  <p>The Nexus 7 should be able to connect to Nexus 4 and the Microncontroller. The Nexus 4 and the Microcontroller are both having different UUID's. It is not necessary to open a connection to both devices at the same time. Only 1:1 connection needed. </p>
  
  <p>The Nexus 7 is opening the <code>BluetoothServerSocket</code> so it needs to no both UUID's.</p>
  
  <p>What do i have to do to make it possible to check if it is the UUID of the Nexus 4 or the UUID of the Microcontroller who tries to connect? Unfortunately i cant do something like this:</p>
  
  <pre><code>tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_NEXUS_UUID);
  tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_MICRO_UUID);
  </code></pre>
  
  <p>To listen to both UUID's. The same thing for the connection:</p>
  
  <pre><code>tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_NEXUS_UUID | MY_MICRO_UUID);
  </code></pre>
  
  <p><strong>Edit</strong> </p>
  
  <p>I use the following <code>BluetoothService</code></p>
  
  <pre><code>public class BluetoothService extends Thread {
      private static final String TAG = BluetoothService.class.getSimpleName();
      private static final String NAME = "My_App";
      private static final UUID MY_UUID = UUID.fromString("d0c722b0-7e15-11e1-b0c4-0800200c9a66");
  
      public static final int STATE_NONE = 0;  
      public static final int STATE_LISTEN = 1;
      public static final int STATE_CONNECTING = 2;
      public static final int STATE_CONNECTED = 3;
  
      private BluetoothAdapter bluetoothAdapter = null;
      private Handler handler = null;
      private AcceptThread acceptThread = null;
      private ConnectThread connectThread = null;
      private ConnectedThread connectedThread = null;
      private int bluetoothState = STATE_NONE;
  
      public BluetoothService(Handler handler) {
          this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          this.bluetoothState = STATE_NONE;
          this.handler = handler;
      }
  
      public synchronized void startConnection() {
          if (this.connectThread != null) {
              this.connectThread.cancel();
              this.connectThread = null;
          }
  
          if (this.connectedThread != null) {
              this.connectedThread.cancel();
              this.connectedThread = null;
          }
  
          this.setBluetoothState(STATE_LISTEN);
  
          if (this.acceptThread == null) {
              this.acceptThread = new AcceptThread();
              this.acceptThread.start();
          }
      }
  
      public synchronized void connect(BluetoothDevice device) {
          if (this.bluetoothState == STATE_CONNECTING) {
              if (this.connectThread != null) {
                  this.connectThread.cancel();
                  this.connectThread = null;
              }
          }
  
          if (this.connectedThread != null) {
              this.connectedThread.cancel();
              this.connectedThread = null;
          }
  
          this.connectThread = new ConnectThread(device);
          this.connectThread.start();
  
          this.setBluetoothState(STATE_CONNECTING);
      }
  
      public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
          if (this.connectThread != null) {
              this.connectThread.cancel();
              this.connectThread = null;
          }
  
          if (this.connectedThread != null) {
              this.connectedThread.cancel();
              this.connectedThread = null;
          }
  
          if (this.acceptThread != null) {
              this.acceptThread.cancel(); 
              this.acceptThread = null;
          }
  
          this.connectedThread = new ConnectedThread(socket);
          this.connectedThread.start();
  
          this.setBluetoothState(STATE_CONNECTED);
          Message msg = this.handler.obtainMessage(Globals.MESSAGE_DEVICE_NAME);
  
          Bundle bundle = new Bundle();
  
          bundle.putString(Globals.EXTRA_DEVICE_NAME, device.getName());
          msg.setData(bundle);
  
          this.handler.sendMessage(msg);
      }
  
      public synchronized void stopConnection() {
          if (this.connectThread != null) {
              this.connectThread.cancel();
              this.connectThread = null;
          }
  
          if (this.connectedThread != null) {
              this.connectedThread.cancel();
              this.connectedThread = null;
          }
  
          if (this.acceptThread != null) {
              this.acceptThread.cancel(); 
              this.acceptThread = null;
          }
  
          this.setBluetoothState(STATE_NONE);
      }
  
      public void write(byte[] out) {
          ConnectedThread connectedThread = null;
  
          synchronized (this) {
              if (this.bluetoothState != STATE_CONNECTED) {
                  return;
              }
  
              connectedThread = this.connectedThread;
          }
  
          connectedThread.write(out);
      }
  
      private void connectionFailed() {
          this.setBluetoothState(STATE_LISTEN);
  
          Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
  
          Bundle bundle = new Bundle();
          bundle.putString(Globals.TOAST, "Unable to connect device");
  
          msg.setData(bundle);
  
          this.handler.sendMessage(msg);
  
          BluetoothService.this.startConnection();
      }
  
      private void connectionLost() {
          this.setBluetoothState(STATE_LISTEN);
  
          Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST);
          Bundle bundle = new Bundle();
  
          bundle.putString(Globals.TOAST, "Device connection was lost");
          msg.setData(bundle);
  
          this.handler.sendMessage(msg);
  
          BluetoothService.this.startConnection();
      }
  
      public synchronized int getBluetoothState() {
          return this.bluetoothState;
      }
  
      private synchronized void setBluetoothState(int bluetoothState) {
          this.bluetoothState = bluetoothState;
      }
  
      private class AcceptThread extends Thread {
          private BluetoothServerSocket serverSocket = null;
  
          public AcceptThread() {
              BluetoothServerSocket tempBluetoothServerSocket = null;
  
              try {
                  tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
              } catch (IOException e) {
                  Log.e(TAG, "listen() failed", e);
              }
  
              this.serverSocket = tempBluetoothServerSocket;
          }
  
          public void run() {
              this.setName("AcceptThread");
  
              BluetoothSocket socket = null;
  
              while (bluetoothState != STATE_CONNECTED) {
                  try {
                      socket = this.serverSocket.accept();
                  } catch (IOException e) {
                      Log.e(TAG, "accept() failed", e);
  
                      break;
                  }
  
                  if (socket != null) {
                      synchronized (BluetoothService.this) {
                          switch (bluetoothState) {
                          case STATE_LISTEN:
                          case STATE_CONNECTING:
                              connected(socket, socket.getRemoteDevice());
  
                              break;
                          case STATE_NONE:
                          case STATE_CONNECTED:
                              try {
                                  socket.close();
                              } catch (IOException e) {
                                  Log.e(TAG, "Could not close unwanted socket", e);
                              }
  
                              break;
                          }
                      }
                  }
              }
          }
  
          public void cancel() {
              try {
                  this.serverSocket.close();
              } catch (IOException e) {
                  Log.e(TAG, "close() of server failed", e);
              }
          }
      }
  
      private class ConnectThread extends Thread {
          private BluetoothSocket bluetoothSocket = null;
          private BluetoothDevice bluetoothDevice = null;
  
          public ConnectThread(BluetoothDevice bluetoothDevice) {
              this.bluetoothDevice = bluetoothDevice;
  
              BluetoothSocket tempBluetoothSocket = null;
  
              try {
                  tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
              } catch (IOException e) {
                  Log.e(TAG, "Socket Type: " + "create() failed", e);
              }
  
              this.bluetoothSocket = tempBluetoothSocket;
          }
  
          public void run() {
              this.setName("ConnectThread");
  
              bluetoothAdapter.cancelDiscovery();
  
              try {
                  this.bluetoothSocket.connect();
              } catch (IOException e) {
                  Log.e(TAG, e.getMessage(), e);
  
                  connectionFailed();
  
                  try {
                      this.bluetoothSocket.close();
                  } catch (IOException e2) {
                      Log.e(TAG, "unable to close() socket during connection failure", e2);
                  }
  
                  return;
              }
  
              synchronized (BluetoothService.this) {
                  connectThread = null;
              }
  
              connected(this.bluetoothSocket, this.bluetoothDevice);
          }
  
          public void cancel() {
              try {
                  this.bluetoothSocket.close();
              } catch (IOException e) {
                  Log.e(TAG, "close() of connect socket failed", e);
              }
          }
      }
  
      private class ConnectedThread extends Thread {
          private BluetoothSocket bluetoothSocket = null;
          private InputStream inputStream = null;
          private OutputStream outputStream = null;
  
          public ConnectedThread(BluetoothSocket bluetoothSocket) {
              this.bluetoothSocket = bluetoothSocket;
  
              InputStream tempInputStream = null;
              OutputStream tempOutputStream = null;
  
              try {
                  tempInputStream = this.bluetoothSocket.getInputStream();
                  tempOutputStream = this.bluetoothSocket.getOutputStream();
              } catch (IOException e) {
                  Log.e(TAG, "temp sockets not created", e);
              }
  
              this.inputStream = tempInputStream;
              this.outputStream = tempOutputStream;
          }
  
          public void run() {
              byte[] buffer = new byte[1024];
              int bytes = 0;
  
              while (true) {
                  try {
                      bytes = this.inputStream.read(buffer);
  
                      handler.obtainMessage(Globals.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                  } catch (IOException e) {
                      Log.e(TAG, "disconnected", e);
  
                      connectionLost();
  
                      BluetoothService.this.start();
  
                      break;
                  }
              }
          }
  
          public void write(byte[] buffer) {
              try {
                  this.outputStream.write(buffer);
  
                  handler.obtainMessage(Globals.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "Exception during write", e);
              }
          }
  
          public void cancel() {
              try {
                  this.bluetoothSocket.close();
              } catch (IOException e) {
                  Log.e(TAG, "close() of connect socket failed", e);
              }
          }
      }
  }
  </code></pre>