@Override
  public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
      handleAdapterStateChange(
          intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
    } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
      handleConnectionStateChange(
          intent.getIntExtra(
              BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.STATE_DISCONNECTED));
    } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
      // Fall through and update bonded devices and refresh view
    }
    refreshViews();
    updateBondedBluetoothDevices();
  }
  public BluetoothController(Context context) {
    mContext = context;

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    context.registerReceiver(this, filter);

    final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
      handleAdapterStateChange(adapter.getState());
      handleConnectionStateChange(adapter.getConnectionState());
    }
    refreshViews();
    updateBondedBluetoothDevices();
  }