/** Stop all threads */
  public synchronized void stop() {
    if (DEBUG) {
      Log.i(TAG, "Synchronized stop()");
    }

    if (mConnectThread != null) {
      mConnectThread.closeSocket();
      mConnectThread = null;
    }

    if (mConnectedThread != null) {
      mConnectedThread.closeSocket();
      mConnectedThread = null;
    }

    if (mSecureAcceptThread != null) {
      mSecureAcceptThread.closeSocket();
      mSecureAcceptThread = null;
    }

    if (mInsecureAcceptThread != null) {
      mInsecureAcceptThread.closeSocket();
      mInsecureAcceptThread = null;
    }
    setState(STATE_NONE);
  }
  /**
   * Start the ConnectedThread to begin managing a Bluetooth connection
   *
   * @param socket The BluetoothSocket on which the connection was made
   * @param device The BluetoothDevice that has been connected
   * @param socketType
   */
  public synchronized void connected(
      BluetoothSocket socket, BluetoothDevice device, final String socketType) {
    if (DEBUG) {
      Log.i(TAG, "Synchronized connected, Socket Type:" + socketType);
    }

    // Cancel the thread that completed the connection
    if (mConnectThread != null) {
      mConnectThread.closeSocket();
      mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
      mConnectedThread.closeSocket();
      mConnectedThread = null;
    }

    // Cancel the accept thread because we only want to connect to one device
    if (mSecureAcceptThread != null) {
      mSecureAcceptThread.closeSocket();
      mSecureAcceptThread = null;
    }
    if (mInsecureAcceptThread != null) {
      mInsecureAcceptThread.closeSocket();
      mInsecureAcceptThread = null;
    }

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket, socketType);
    mConnectedThread.start();

    // Send the name of the connected device back to the UI Activity
    Message msg = mHandler.obtainMessage(MESSAGE_DEVICE_NAME);
    Bundle bundle = new Bundle();
    bundle.putString(DEVICE_NAME, device.getName());
    msg.setData(bundle);
    mHandler.sendMessage(msg);

    setState(STATE_CONNECTED);
  }