b/2293042 Fixed the problem where A2DP connections may fail if there's an existing connection to another device.
Change-Id: I1b4963a167b633c0905e2719ab3f651ff8be9f2c
diff --git a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
index 03328b5..f749cf7 100644
--- a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
+++ b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
@@ -41,6 +41,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.Set;
/**
* CachedBluetoothDevice represents a remote Bluetooth device. It contains
@@ -401,6 +402,7 @@
.getProfileManager(mLocalManager, profile);
if (profileManager.isPreferred(mDevice)) {
++preferredProfiles;
+ disconnectConnected(profile);
queueCommand(new BluetoothJob(BluetoothCommand.CONNECT, this, profile));
}
}
@@ -423,6 +425,7 @@
LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
.getProfileManager(mLocalManager, profile);
profileManager.setPreferred(mDevice, false);
+ disconnectConnected(profile);
queueCommand(new BluetoothJob(BluetoothCommand.CONNECT, this, profile));
}
}
@@ -432,9 +435,24 @@
mConnectAttempted = SystemClock.elapsedRealtime();
// Reset the only-show-one-error-dialog tracking variable
mIsConnectingErrorPossible = true;
+ disconnectConnected(profile);
queueCommand(new BluetoothJob(BluetoothCommand.CONNECT, this, profile));
}
+ private void disconnectConnected(Profile profile) {
+ LocalBluetoothProfileManager profileManager =
+ LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
+ CachedBluetoothDeviceManager cachedDeviceManager = mLocalManager.getCachedDeviceManager();
+ Set<BluetoothDevice> devices = profileManager.getConnectedDevices();
+ if (devices == null) return;
+ for (BluetoothDevice device : devices) {
+ CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(device);
+ if (cachedDevice != null) {
+ queueCommand(new BluetoothJob(BluetoothCommand.DISCONNECT, cachedDevice, profile));
+ }
+ }
+ }
+
private boolean connectInt(CachedBluetoothDevice cachedDevice, Profile profile) {
if (!cachedDevice.ensurePaired()) return false;
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index 44e2be3..da3c69f 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -16,17 +16,18 @@
package com.android.settings.bluetooth;
+import com.android.settings.R;
+
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothUuid;
-import android.os.ParcelUuid;
import android.os.Handler;
+import android.os.ParcelUuid;
import android.util.Log;
-import com.android.settings.R;
-
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -121,6 +122,8 @@
mLocalManager = localManager;
}
+ public abstract Set<BluetoothDevice> getConnectedDevices();
+
public abstract boolean connect(BluetoothDevice device);
public abstract boolean disconnect(BluetoothDevice device);
@@ -164,6 +167,11 @@
}
@Override
+ public Set<BluetoothDevice> getConnectedDevices() {
+ return mService.getConnectedSinks();
+ }
+
+ @Override
public boolean connect(BluetoothDevice device) {
Set<BluetoothDevice> sinks = mService.getConnectedSinks();
if (sinks != null) {
@@ -261,6 +269,17 @@
}
@Override
+ public Set<BluetoothDevice> getConnectedDevices() {
+ Set<BluetoothDevice> devices = null;
+ BluetoothDevice device = mService.getCurrentHeadset();
+ if (device != null) {
+ devices = new HashSet<BluetoothDevice>();
+ devices.add(device);
+ }
+ return devices;
+ }
+
+ @Override
public boolean connect(BluetoothDevice device) {
// Since connectHeadset fails if already connected to a headset, we
// disconnect from any headset first
@@ -334,6 +353,11 @@
}
@Override
+ public Set<BluetoothDevice> getConnectedDevices() {
+ return null;
+ }
+
+ @Override
public boolean connect(BluetoothDevice device) {
return false;
}