gt:                     if (discoveryCallback != null)
context: package it.ivotek.poor2.util;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.lang.reflect.Method;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.UUID;
  
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.bluetooth.BluetoothSocket;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.ParcelUuid;
  import android.os.Parcelable;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  
  /**
   * Created by Omar on 14/07/2015.
   * https://github.com/omaflak/Bluetooth-Library
   */
  public class Bluetooth {
      private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
      private BluetoothAdapter bluetoothAdapter;
      private BluetoothSocket socket;
      private BluetoothDevice device, devicePair, deviceServices;
      private BufferedReader input;
      private OutputStream out;
  
      private boolean connected=false;
      private CommunicationCallback communicationCallback=null;
      private DiscoveryCallback discoveryCallback=null;
  
      private Context context;
  
      public Bluetooth(Context context){
          this.context = context;
          bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      }
  
      public boolean isBluetoothEnabled() {
          return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
      }
  
      public void requestEnableBluetooth(Fragment fragment, int requestCode) {
          fragment.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), requestCode);
      }
  
      public void enableBluetooth() {
          if(bluetoothAdapter!=null) {
              if (!bluetoothAdapter.isEnabled()) {
                  bluetoothAdapter.enable();
              }
          }
      }
  
      public void disableBluetooth() {
          if(bluetoothAdapter!=null) {
              if (bluetoothAdapter.isEnabled()) {
                  bluetoothAdapter.disable();
              }
          }
      }
  
      public void connectToAddress(String address) {
          connectToDevice(bluetoothAdapter.getRemoteDevice(address));
      }
  
      public void connectToName(String name) {
          for (BluetoothDevice blueDevice : bluetoothAdapter.getBondedDevices()) {
              if (blueDevice.getName().equals(name)) {
                  connectToAddress(blueDevice.getAddress());
                  return;
              }
          }
      }
  
      public void connectToDevice(BluetoothDevice device) {
          // start service discovery first
          scanServices(device);
      }
  
      public void disconnect() {
          try {
              socket.close();
          } catch (IOException e) {
              if(communicationCallback!=null)
                  communicationCallback.onError(e.getMessage());
          }
      }
  
      public boolean isConnected(){
          return connected;
      }
  
      public void send(String msg){
          try {
              out.write(msg.getBytes());
          } catch (IOException e) {
              connected=false;
              if(communicationCallback!=null)
                  communicationCallback.onDisconnect(device, e.getMessage());
          }
      }
  
      private class ReceiveThread extends Thread implements Runnable{
          public void run(){
              String msg;
              try {
                  while ((msg = input.readLine()) != null) {
                      if (communicationCallback != null)
                          communicationCallback.onMessage(msg);
                  }
              } catch (IOException e) {
                  connected=false;
                  if (communicationCallback != null)
                      communicationCallback.onDisconnect(device, e.getMessage());
              }
          }
      }
  
      private class ConnectThread extends Thread {
          public ConnectThread(BluetoothDevice device) {
              Bluetooth.this.device=device;
              try {
                  Bluetooth.this.socket = device.createRfcommSocketToServiceRecord(MY_UUID);
              } catch (IOException e) {
                  if(communicationCallback!=null)
                      communicationCallback.onError(e.getMessage());
              }
          }
  
          public void run() {
              bluetoothAdapter.cancelDiscovery();
  
              try {
                  socket.connect();
                  out = socket.getOutputStream();
                  input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  connected=true;
  
                  new ReceiveThread().start();
  
                  if(communicationCallback!=null)
                      communicationCallback.onConnect(device);
              } catch (IOException e) {
                  if(communicationCallback!=null)
                      communicationCallback.onConnectError(device, e.getMessage());
  
                  try {
                      socket.close();
                  } catch (IOException closeException) {
                      if (communicationCallback != null)
                          communicationCallback.onError(closeException.getMessage());
                  }
              }
          }
      }
  
      public List<BluetoothDevice> getPairedDevices(){
          List<BluetoothDevice> devices = new ArrayList<>();
          for (BluetoothDevice blueDevice : bluetoothAdapter.getBondedDevices()) {
              devices.add(blueDevice);
          }
          return devices;
      }
  
      public BluetoothSocket getSocket(){
          return socket;
      }
  
      public BluetoothDevice getDevice(){
          return device;
      }
  
      public BluetoothDevice getRemoteDevice(String address) {
          return bluetoothAdapter.getRemoteDevice(address);
      }
  
      public void scanDevices(){
          IntentFilter filter = new IntentFilter();
  
          filter.addAction(BluetoothDevice.ACTION_FOUND);
          filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
          filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  
          context.registerReceiver(mReceiverScan, filter);
          bluetoothAdapter.startDiscovery();
      }
  
      public void scanServices(BluetoothDevice device) {
          context.registerReceiver(mUuidReceiver, new IntentFilter(BluetoothDevice.ACTION_UUID));
          deviceServices = device;
          device.fetchUuidsWithSdp();
      }
  
      public void pair(BluetoothDevice device){
          context.registerReceiver(mPairReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
          devicePair=device;
          try {
              // FIXME questo non funziona su Android API < 19
              Method method = device.getClass().getMethod("createBond", (Class[]) null);
              method.invoke(device, (Object[]) null);
          } catch (Exception e) {
              if(discoveryCallback!=null)
                  discoveryCallback.onError(e.getMessage());
          }
      }
  
      public void unpair(BluetoothDevice device) {
          devicePair=device;
          try {
              // FIXME questo non funziona su Android API < 19
              Method method = device.getClass().getMethod("removeBond", (Class[]) null);
              method.invoke(device, (Object[]) null);
          } catch (Exception e) {
              if(discoveryCallback!=null)
                  discoveryCallback.onError(e.getMessage());
          }
      }
  
      private BroadcastReceiver mReceiverScan = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              String action = intent.getAction();
  
              switch (action) {
                  case BluetoothAdapter.ACTION_STATE_CHANGED:
                      final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                      if (state == BluetoothAdapter.STATE_OFF) {
                          if (discoveryCallback != null)
                              discoveryCallback.onError("Bluetooth turned off");
                      }
                      break;
                  case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                      context.unregisterReceiver(mReceiverScan);
  