Program: 58225
Project: 58225
Buggy source:
@Override public void onAdvertisementUpdate(ScanResult scanResult){
  if (mConnectedDevices.contains(scanResult.getDevice().getAddress())) {
    return;
  }
  if (mConnectingDevices.contains(scanResult.getDevice().getAddress())) {
    return;
  }
  if (mConnectionGovernor != null && !mConnectionGovernor.shouldConnectToPeripheral(scanResult)) {
    return;
  }
  mConnectingDevices.add(scanResult.getDevice().getAddress());
  logEvent("String_Node_Str" + scanResult.getDevice().getAddress());
  scanResult.getDevice().connectGatt(mContext,false,new BluetoothGattCallback(){
    @Override public void onConnectionStateChange(    BluetoothGatt gatt,    int status,    int newState){
      if (status == BluetoothGatt.GATT_SUCCESS) {
        logEvent("String_Node_Str");
      }
 else {
      }
switch (newState) {
case BluetoothProfile.STATE_DISCONNECTED:
        logEvent("String_Node_Str" + gatt.getDevice().getAddress());
      mConnectedDevices.remove(gatt.getDevice().getAddress());
    mConnectingDevices.remove(gatt.getDevice().getAddress());
  if (mConnectionListener != null)   mConnectionListener.disconnectedFrom(gatt.getDevice().getAddress());
gatt.close();
break;
case BluetoothProfile.STATE_CONNECTED:
logEvent("String_Node_Str" + gatt.getDevice().getAddress());
mConnectedDevices.add(gatt.getDevice().getAddress());
mConnectingDevices.remove(gatt.getDevice().getAddress());
if (mConnectionListener != null) mConnectionListener.connectedTo(gatt.getDevice().getAddress());
boolean discovering=gatt.discoverServices();
logEvent("String_Node_Str" + discovering);
break;
}
super.onConnectionStateChange(gatt,status,newState);
}
@Override public void onServicesDiscovered(BluetoothGatt gatt,int status){
if (status == BluetoothGatt.GATT_SUCCESS) logEvent("String_Node_Str");
 else logEvent("String_Node_Str" + status);
boolean foundService=false;
try {
List<BluetoothGattService> serviceList=gatt.getServices();
for (BluetoothGattService service : serviceList) {
if (service.getUuid().equals(GATT.SERVICE_UUID)) {
logEvent("String_Node_Str");
foundService=true;
List<BluetoothGattCharacteristic> characteristics=service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
if (mCharacteristicUUIDToRequest.containsKey(characteristic.getUuid())) {
mCharacteristicUUIDToRequest.get(characteristic.getUuid()).mCharacteristic=characteristic;
}
}
}
}
}
 catch (Exception e) {
logEvent("String_Node_Str" + e.getLocalizedMessage());
e.printStackTrace();
}
if (!foundService) logEvent("String_Node_Str");
 else beginRequestFlowWithPeripheral(gatt);
super.onServicesDiscovered(gatt,status);
}
@Override public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status){
if (status != BluetoothGatt.GATT_SUCCESS) logEvent(String.format("String_Node_Str"));
logEvent(String.format("String_Node_Str",GATT.getNameForCharacteristic(characteristic),characteristic.getValue() == null ? "String_Node_Str" : DataUtil.bytesToHex(characteristic.getValue()),status));
handleResponseForCurrentRequestToPeripheral(gatt,BLECentralRequest.RequestType.READ,characteristic,status);
super.onCharacteristicRead(gatt,characteristic,status);
}
@Override public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status){
logEvent(String.format("String_Node_Str",characteristic.getUuid(),status));
handleResponseForCurrentRequestToPeripheral(gatt,BLECentralRequest.RequestType.WRITE,characteristic,status);
super.onCharacteristicWrite(gatt,characteristic,status);
}
@Override public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic){
String toLog=null;
try {
toLog="String_Node_Str" + characteristic.getStringValue(0);
}
 catch (Exception e) {
toLog="String_Node_Str" + characteristic.getUuid().toString();
}
logEvent(toLog);
super.onCharacteristicChanged(gatt,characteristic);
}
@Override public void onReadRemoteRssi(BluetoothGatt gatt,int rssi,int status){
Log.i(TAG,"String_Node_Str");
super.onReadRemoteRssi(gatt,rssi,status);
}
}
);
}

Fixed version (ground truth):
@Override public void onAdvertisementUpdate(ScanResult scanResult){
  if (mConnectedDevices.contains(scanResult.getDevice().getAddress())) {
    return;
  }
  if (mConnectingDevices.contains(scanResult.getDevice().getAddress())) {
    return;
  }
  if (mConnectionGovernor != null && !mConnectionGovernor.shouldConnectToPeripheral(scanResult)) {
    return;
  }
  mConnectingDevices.add(scanResult.getDevice().getAddress());
  logEvent("String_Node_Str" + scanResult.getDevice().getAddress());
  scanResult.getDevice().connectGatt(mContext,false,new BluetoothGattCallback(){
    @Override public void onConnectionStateChange(    BluetoothGatt gatt,    int status,    int newState){
      if (status == BluetoothGatt.GATT_SUCCESS) {
        logEvent("String_Node_Str");
      }
 else {
      }
switch (newState) {
case BluetoothProfile.STATE_DISCONNECTED:
        logEvent("String_Node_Str" + gatt.getDevice().getAddress());
      mConnectedDevices.remove(gatt.getDevice().getAddress());
    mConnectingDevices.remove(gatt.getDevice().getAddress());
  if (mConnectionListener != null)   mConnectionListener.disconnectedFrom(gatt.getDevice().getAddress());
gatt.close();
break;
case BluetoothProfile.STATE_CONNECTED:
logEvent("String_Node_Str" + gatt.getDevice().getAddress());
mConnectedDevices.add(gatt.getDevice().getAddress());
mConnectingDevices.remove(gatt.getDevice().getAddress());
if (mConnectionListener != null) mConnectionListener.connectedTo(gatt.getDevice().getAddress());
boolean discovering=gatt.discoverServices();
logEvent("String_Node_Str" + discovering);
break;
}
super.onConnectionStateChange(gatt,status,newState);
}
@Override public void onServicesDiscovered(BluetoothGatt gatt,int status){
if (status == BluetoothGatt.GATT_SUCCESS) logEvent("String_Node_Str");
 else logEvent("String_Node_Str" + status);
boolean foundService=false;
try {
List<BluetoothGattService> serviceList=gatt.getServices();
for (BluetoothGattService service : serviceList) {
if (service.getUuid().equals(GATT.SERVICE_UUID)) {
logEvent("String_Node_Str");
foundService=true;
List<BluetoothGattCharacteristic> characteristics=service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
Pair<UUID,BLECentralRequest.RequestType> readKey=new Pair<>(characteristic.getUuid(),BLECentralRequest.RequestType.READ);
Pair<UUID,BLECentralRequest.RequestType> writeKey=new Pair<>(characteristic.getUuid(),BLECentralRequest.RequestType.WRITE);
if (mCharacteristicUUIDToRequest.containsKey(readKey)) {
mCharacteristicUUIDToRequest.get(readKey).mCharacteristic=characteristic;
}
 else if (mCharacteristicUUIDToRequest.containsKey(writeKey)) {
mCharacteristicUUIDToRequest.get(writeKey).mCharacteristic=characteristic;
}
}
}
}
}
 catch (Exception e) {
logEvent("String_Node_Str" + e.getLocalizedMessage());
e.printStackTrace();
}
if (!foundService) logEvent("String_Node_Str");
 else beginRequestFlowWithPeripheral(gatt);
super.onServicesDiscovered(gatt,status);
}
@Override public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status){
if (status != BluetoothGatt.GATT_SUCCESS) logEvent(String.format("String_Node_Str"));
logEvent(String.format("String_Node_Str",GATT.getNameForCharacteristic(characteristic),characteristic.getValue() == null ? "String_Node_Str" : DataUtil.bytesToHex(characteristic.getValue()),status));
handleResponseForCurrentRequestToPeripheral(gatt,BLECentralRequest.RequestType.READ,characteristic,status);
super.onCharacteristicRead(gatt,characteristic,status);
}
@Override public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status){
logEvent(String.format("String_Node_Str",characteristic.getUuid(),status));
handleResponseForCurrentRequestToPeripheral(gatt,BLECentralRequest.RequestType.WRITE,characteristic,status);
super.onCharacteristicWrite(gatt,characteristic,status);
}
@Override public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic){
String toLog=null;
try {
toLog="String_Node_Str" + characteristic.getStringValue(0);
}
 catch (Exception e) {
toLog="String_Node_Str" + characteristic.getUuid().toString();
}
logEvent(toLog);
super.onCharacteristicChanged(gatt,characteristic);
}
@Override public void onReadRemoteRssi(BluetoothGatt gatt,int rssi,int status){
Log.i(TAG,"String_Node_Str");
super.onReadRemoteRssi(gatt,rssi,status);
}
}
);
}