Program: 11374
Project: 11374
Buggy source:
@SuppressLint("String_Node_Str") @Override public void onServicesDiscovered(final BluetoothGatt gatt,int status){
  super.onServicesDiscovered(gatt,status);
  if (status != BluetoothGatt.GATT_SUCCESS) {
    return;
  }
  final String gattDeviceAddress=gatt.getDevice().getAddress();
  if (midiInputDevicesMap.containsKey(gattDeviceAddress)) {
synchronized (midiInputDevicesMap) {
      Set<MidiInputDevice> midiInputDevices=midiInputDevicesMap.get(gattDeviceAddress);
      for (      MidiInputDevice midiInputDevice : midiInputDevices) {
        midiInputDevice.setOnMidiInputEventListener(null);
      }
      midiInputDevicesMap.remove(gattDeviceAddress);
    }
  }
  MidiInputDevice midiInputDevice=null;
  try {
    midiInputDevice=new InternalMidiInputDevice(context,gatt);
  }
 catch (  IllegalArgumentException iae) {
    Log.d(Constants.TAG,iae.getMessage());
  }
  if (midiInputDevice != null) {
synchronized (midiInputDevicesMap) {
      Set<MidiInputDevice> midiInputDevices=midiInputDevicesMap.get(gattDeviceAddress);
      if (midiInputDevices == null) {
        midiInputDevices=new HashSet<>();
        midiInputDevicesMap.put(gattDeviceAddress,midiInputDevices);
      }
      midiInputDevices.add(midiInputDevice);
    }
    if (!deviceAddressGattMap.containsKey(gattDeviceAddress)) {
      if (midiDeviceAttachedListener != null) {
        midiDeviceAttachedListener.onMidiInputDeviceAttached(midiInputDevice);
      }
    }
  }
  if (midiOutputDevicesMap.containsKey(gattDeviceAddress)) {
synchronized (midiOutputDevicesMap) {
      midiOutputDevicesMap.remove(gattDeviceAddress);
    }
  }
  MidiOutputDevice midiOutputDevice=null;
  try {
    midiOutputDevice=new InternalMidiOutputDevice(context,gatt);
  }
 catch (  IllegalArgumentException iae) {
    Log.d(Constants.TAG,iae.getMessage());
  }
  if (midiOutputDevice != null) {
synchronized (midiOutputDevicesMap) {
      Set<MidiOutputDevice> midiOutputDevices=midiOutputDevicesMap.get(gattDeviceAddress);
      if (midiOutputDevices == null) {
        midiOutputDevices=new HashSet<>();
        midiOutputDevicesMap.put(gattDeviceAddress,midiOutputDevices);
      }
      midiOutputDevices.add(midiOutputDevice);
    }
    if (!deviceAddressGattMap.containsKey(gattDeviceAddress)) {
      if (midiDeviceAttachedListener != null) {
        midiDeviceAttachedListener.onMidiOutputDeviceAttached(midiOutputDevice);
      }
    }
  }
  if (midiInputDevice != null || midiOutputDevice != null) {
synchronized (deviceAddressGattMap) {
      deviceAddressGattMap.put(gattDeviceAddress,gatt);
    }
    if (needsBonding && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      BluetoothDevice bluetoothDevice=gatt.getDevice();
      if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
        bluetoothDevice.createBond();
        bluetoothDevice.setPairingConfirmation(true);
        if (bondingBroadcastReceiver != null) {
          context.unregisterReceiver(bondingBroadcastReceiver);
        }
        bondingBroadcastReceiver=new BondingBroadcastReceiver(midiInputDevice,midiOutputDevice);
        IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        context.registerReceiver(bondingBroadcastReceiver,filter);
      }
    }
 else {
      if (midiInputDevice != null) {
        ((InternalMidiInputDevice)midiInputDevice).configureAsCentralDevice();
      }
      if (midiOutputDevice != null) {
        ((InternalMidiOutputDevice)midiOutputDevice).configureAsCentralDevice();
      }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      gatt.requestMtu(23);
      gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
    }
  }
}

Fixed version (ground truth):
@SuppressLint("String_Node_Str") @Override public void onServicesDiscovered(final BluetoothGatt gatt,int status){
  super.onServicesDiscovered(gatt,status);
  if (status != BluetoothGatt.GATT_SUCCESS) {
    return;
  }
  final String gattDeviceAddress=gatt.getDevice().getAddress();
  if (midiInputDevicesMap.containsKey(gattDeviceAddress)) {
synchronized (midiInputDevicesMap) {
      Set<MidiInputDevice> midiInputDevices=midiInputDevicesMap.get(gattDeviceAddress);
      for (      MidiInputDevice midiInputDevice : midiInputDevices) {
        ((InternalMidiInputDevice)midiInputDevice).stop();
        midiInputDevice.setOnMidiInputEventListener(null);
      }
      midiInputDevicesMap.remove(gattDeviceAddress);
    }
  }
  MidiInputDevice midiInputDevice=null;
  try {
    midiInputDevice=new InternalMidiInputDevice(context,gatt);
  }
 catch (  IllegalArgumentException iae) {
    Log.d(Constants.TAG,iae.getMessage());
  }
  if (midiInputDevice != null) {
synchronized (midiInputDevicesMap) {
      Set<MidiInputDevice> midiInputDevices=midiInputDevicesMap.get(gattDeviceAddress);
      if (midiInputDevices == null) {
        midiInputDevices=new HashSet<>();
        midiInputDevicesMap.put(gattDeviceAddress,midiInputDevices);
      }
      midiInputDevices.add(midiInputDevice);
    }
    if (!deviceAddressGattMap.containsKey(gattDeviceAddress)) {
      if (midiDeviceAttachedListener != null) {
        midiDeviceAttachedListener.onMidiInputDeviceAttached(midiInputDevice);
      }
    }
  }
  if (midiOutputDevicesMap.containsKey(gattDeviceAddress)) {
synchronized (midiOutputDevicesMap) {
      midiOutputDevicesMap.remove(gattDeviceAddress);
    }
  }
  MidiOutputDevice midiOutputDevice=null;
  try {
    midiOutputDevice=new InternalMidiOutputDevice(context,gatt);
  }
 catch (  IllegalArgumentException iae) {
    Log.d(Constants.TAG,iae.getMessage());
  }
  if (midiOutputDevice != null) {
synchronized (midiOutputDevicesMap) {
      Set<MidiOutputDevice> midiOutputDevices=midiOutputDevicesMap.get(gattDeviceAddress);
      if (midiOutputDevices == null) {
        midiOutputDevices=new HashSet<>();
        midiOutputDevicesMap.put(gattDeviceAddress,midiOutputDevices);
      }
      midiOutputDevices.add(midiOutputDevice);
    }
    if (!deviceAddressGattMap.containsKey(gattDeviceAddress)) {
      if (midiDeviceAttachedListener != null) {
        midiDeviceAttachedListener.onMidiOutputDeviceAttached(midiOutputDevice);
      }
    }
  }
  if (midiInputDevice != null || midiOutputDevice != null) {
synchronized (deviceAddressGattMap) {
      deviceAddressGattMap.put(gattDeviceAddress,gatt);
    }
    if (needsBonding && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      BluetoothDevice bluetoothDevice=gatt.getDevice();
      if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
        bluetoothDevice.createBond();
        bluetoothDevice.setPairingConfirmation(true);
        if (bondingBroadcastReceiver != null) {
          context.unregisterReceiver(bondingBroadcastReceiver);
        }
        bondingBroadcastReceiver=new BondingBroadcastReceiver(midiInputDevice,midiOutputDevice);
        IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        context.registerReceiver(bondingBroadcastReceiver,filter);
      }
    }
 else {
      if (midiInputDevice != null) {
        ((InternalMidiInputDevice)midiInputDevice).configureAsCentralDevice();
      }
      if (midiOutputDevice != null) {
        ((InternalMidiOutputDevice)midiOutputDevice).configureAsCentralDevice();
      }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      gatt.requestMtu(23);
      gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
    }
  }
}