Program: 23280
Project: 23280
Buggy source:
@Override public void onReceive(Context context,Intent intent){
  String action=intent.getAction();
  if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
    int state=intent.getIntExtra(BluetoothA2dp.EXTRA_SINK_STATE,BluetoothA2dp.STATE_DISCONNECTED);
    BluetoothDevice btDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String address=btDevice.getAddress();
    boolean isConnected=(mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) && ((String)mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)).equals(address));
    if (isConnected && state != BluetoothA2dp.STATE_CONNECTED && state != BluetoothA2dp.STATE_PLAYING) {
      if (address.equals(sBtDockAddress)) {
        Log.v(TAG,"String_Node_Str");
        AudioSystem.setForceUse(AudioSystem.FOR_DOCK,AudioSystem.FORCE_NONE);
      }
      AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,AudioSystem.DEVICE_STATE_UNAVAILABLE,address);
      mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP);
    }
 else     if (!isConnected && (state == BluetoothA2dp.STATE_CONNECTED || state == BluetoothA2dp.STATE_PLAYING)) {
      if (btDevice.isBluetoothDock()) {
        Log.v(TAG,"String_Node_Str");
        sBtDockAddress=address;
        Intent i=context.registerReceiver(null,new IntentFilter(Intent.ACTION_DOCK_EVENT));
        if (i != null) {
          int dockState=i.getIntExtra(Intent.EXTRA_DOCK_STATE,Intent.EXTRA_DOCK_STATE_UNDOCKED);
          int config;
switch (dockState) {
case Intent.EXTRA_DOCK_STATE_DESK:
            config=AudioSystem.FORCE_BT_DESK_DOCK;
          break;
case Intent.EXTRA_DOCK_STATE_CAR:
        config=AudioSystem.FORCE_BT_CAR_DOCK;
      break;
case Intent.EXTRA_DOCK_STATE_UNDOCKED:
default :
    config=AudioSystem.FORCE_NONE;
}
AudioSystem.setForceUse(AudioSystem.FOR_DOCK,config);
}
}
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,AudioSystem.DEVICE_STATE_AVAILABLE,address);
mConnectedDevices.put(new Integer(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP),address);
}
}
 else if (action.equals(BluetoothHeadset.ACTION_STATE_CHANGED)) {
int state=intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,BluetoothHeadset.STATE_ERROR);
int device=AudioSystem.DEVICE_OUT_BLUETOOTH_SCO;
BluetoothDevice btDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String address=null;
if (btDevice != null) {
address=btDevice.getAddress();
BluetoothClass btClass=btDevice.getBluetoothClass();
if (btClass != null) {
switch (btClass.getDeviceClass()) {
case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
device=AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
break;
case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
device=AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
break;
}
}
}
boolean isConnected=(mConnectedDevices.containsKey(device) && ((String)mConnectedDevices.get(device)).equals(address));
if (isConnected && state != BluetoothHeadset.STATE_CONNECTED) {
AudioSystem.setDeviceConnectionState(device,AudioSystem.DEVICE_STATE_UNAVAILABLE,address);
mConnectedDevices.remove(device);
}
 else if (!isConnected && state == BluetoothHeadset.STATE_CONNECTED) {
AudioSystem.setDeviceConnectionState(device,AudioSystem.DEVICE_STATE_AVAILABLE,address);
mConnectedDevices.put(new Integer(device),address);
}
}
 else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
int state=intent.getIntExtra("String_Node_Str",0);
int microphone=intent.getIntExtra("String_Node_Str",0);
if (microphone != 0) {
boolean isConnected=mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_WIRED_HEADSET);
if (state == 0 && isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADSET,AudioSystem.DEVICE_STATE_UNAVAILABLE,"String_Node_Str");
mConnectedDevices.remove(AudioSystem.DEVICE_OUT_WIRED_HEADSET);
}
 else if (state == 1 && !isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADSET,AudioSystem.DEVICE_STATE_AVAILABLE,"String_Node_Str");
mConnectedDevices.put(new Integer(AudioSystem.DEVICE_OUT_WIRED_HEADSET),"String_Node_Str");
}
}
 else {
boolean isConnected=mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE);
if (state == 0 && isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE,AudioSystem.DEVICE_STATE_UNAVAILABLE,"String_Node_Str");
mConnectedDevices.remove(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE);
}
 else if (state == 1 && !isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE,AudioSystem.DEVICE_STATE_AVAILABLE,"String_Node_Str");
mConnectedDevices.put(new Integer(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE),"String_Node_Str");
}
}
}
}

Fixed version (ground truth):
@Override public void onReceive(Context context,Intent intent){
  String action=intent.getAction();
  if (action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED)) {
    int state=intent.getIntExtra(BluetoothA2dp.EXTRA_SINK_STATE,BluetoothA2dp.STATE_DISCONNECTED);
    BluetoothDevice btDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String address=btDevice.getAddress();
    boolean isConnected=(mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) && ((String)mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)).equals(address));
    if (isConnected && state != BluetoothA2dp.STATE_CONNECTED && state != BluetoothA2dp.STATE_PLAYING) {
      if (address.equals(sBtDockAddress)) {
        Log.v(TAG,"String_Node_Str");
        AudioSystem.setForceUse(AudioSystem.FOR_DOCK,AudioSystem.FORCE_NONE);
      }
      AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,AudioSystem.DEVICE_STATE_UNAVAILABLE,address);
      mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP);
    }
 else     if (!isConnected && (state == BluetoothA2dp.STATE_CONNECTED || state == BluetoothA2dp.STATE_PLAYING)) {
      if (btDevice.isBluetoothDock()) {
        Log.v(TAG,"String_Node_Str");
        sBtDockAddress=address;
        Intent i=context.registerReceiver(null,new IntentFilter(Intent.ACTION_DOCK_EVENT));
        if (i != null) {
          int dockState=i.getIntExtra(Intent.EXTRA_DOCK_STATE,Intent.EXTRA_DOCK_STATE_UNDOCKED);
          int config;
switch (dockState) {
case Intent.EXTRA_DOCK_STATE_DESK:
            config=AudioSystem.FORCE_BT_DESK_DOCK;
          break;
case Intent.EXTRA_DOCK_STATE_CAR:
        config=AudioSystem.FORCE_BT_CAR_DOCK;
      break;
case Intent.EXTRA_DOCK_STATE_UNDOCKED:
default :
    config=AudioSystem.FORCE_NONE;
}
AudioSystem.setForceUse(AudioSystem.FOR_DOCK,config);
}
}
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,AudioSystem.DEVICE_STATE_AVAILABLE,address);
AudioSystem.setParameters("String_Node_Str");
mConnectedDevices.put(new Integer(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP),address);
}
}
 else if (action.equals(BluetoothHeadset.ACTION_STATE_CHANGED)) {
int state=intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,BluetoothHeadset.STATE_ERROR);
int device=AudioSystem.DEVICE_OUT_BLUETOOTH_SCO;
BluetoothDevice btDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String address=null;
if (btDevice != null) {
address=btDevice.getAddress();
BluetoothClass btClass=btDevice.getBluetoothClass();
if (btClass != null) {
switch (btClass.getDeviceClass()) {
case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
device=AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
break;
case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
device=AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
break;
}
}
}
boolean isConnected=(mConnectedDevices.containsKey(device) && ((String)mConnectedDevices.get(device)).equals(address));
if (isConnected && state != BluetoothHeadset.STATE_CONNECTED) {
AudioSystem.setDeviceConnectionState(device,AudioSystem.DEVICE_STATE_UNAVAILABLE,address);
mConnectedDevices.remove(device);
}
 else if (!isConnected && state == BluetoothHeadset.STATE_CONNECTED) {
AudioSystem.setDeviceConnectionState(device,AudioSystem.DEVICE_STATE_AVAILABLE,address);
mConnectedDevices.put(new Integer(device),address);
}
}
 else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
int state=intent.getIntExtra("String_Node_Str",0);
int microphone=intent.getIntExtra("String_Node_Str",0);
if (microphone != 0) {
boolean isConnected=mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_WIRED_HEADSET);
if (state == 0 && isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADSET,AudioSystem.DEVICE_STATE_UNAVAILABLE,"String_Node_Str");
mConnectedDevices.remove(AudioSystem.DEVICE_OUT_WIRED_HEADSET);
}
 else if (state == 1 && !isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADSET,AudioSystem.DEVICE_STATE_AVAILABLE,"String_Node_Str");
mConnectedDevices.put(new Integer(AudioSystem.DEVICE_OUT_WIRED_HEADSET),"String_Node_Str");
}
}
 else {
boolean isConnected=mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE);
if (state == 0 && isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE,AudioSystem.DEVICE_STATE_UNAVAILABLE,"String_Node_Str");
mConnectedDevices.remove(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE);
}
 else if (state == 1 && !isConnected) {
AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE,AudioSystem.DEVICE_STATE_AVAILABLE,"String_Node_Str");
mConnectedDevices.put(new Integer(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE),"String_Node_Str");
}
}
}
}