private synchronized void stop() {
   if (mListeningThread != null) {
     mListeningThread.cancel();
     mListeningThread = null;
   }
   setServiceState(State.STATE_NONE);
 }
  private synchronized void listen() {

    setServiceState(State.STATE_LISTENING);

    if (mListeningThread == null) {
      mLogger.info("BluetoothService.listen: starting listening thread");
      mListeningThread = new ListeningThread("Bluetooth Inspector", SPP_UUID);
      mListeningThread.start();
    }
  }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ENABLE_BT_REQUEST_CODE) {
      // Bluetooth successfully enabled!
      if (resultCode == Activity.RESULT_OK) {
        Toast.makeText(
                getApplicationContext(),
                "Bluetooth enabled." + "\n" + "Scanning for peers",
                Toast.LENGTH_SHORT)
            .show();

        makeDiscoverable();
        discoverDevices();

        t = new ListeningThread();
        t.start();

      } else {
        Toast.makeText(getApplicationContext(), "Bluetooth is not enabled.", Toast.LENGTH_SHORT)
            .show();
      }
    } else if (requestCode == DISCOVERABLE_BT_REQUEST_CODE) {
      if (resultCode == DISCOVERABLE_DURATION) {
        Toast.makeText(
                getApplicationContext(),
                "Your device is now discoverable for " + DISCOVERABLE_DURATION + " seconds",
                Toast.LENGTH_SHORT)
            .show();
      } else {
        Toast.makeText(
                getApplicationContext(), "Fail to enable discoverable mode.", Toast.LENGTH_SHORT)
            .show();
      }
    } else if (resultCode == Finished_Activity) {
      bluetoothAdapter.disable();
      adapter.clear();
      refreshEnabled = false;
      btn.setText("Find Opponent");
    }
  }
 private synchronized void onInboundConnection(BluetoothSocket socket) {
   // cancel the listening thread, because we only want to connect to one device at a time
   mListeningThread.cancel();
   setServiceState(State.STATE_CONNECTED);
 }