Auto connect hf/a2dp

Change-Id: I4cb260a1f794c2e094a0b19bdc1df919c4287232
This commit is contained in:
Swaminatha Balaji
2012-04-27 09:18:38 -07:00
committed by Matthew Xie
parent 83b0d575dd
commit 2ac143fff7
4 changed files with 109 additions and 3 deletions

20
src/com/android/settings/bluetooth/A2dpProfile.java Normal file → Executable file
View File

@@ -42,6 +42,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
}; };
static final String NAME = "A2DP"; static final String NAME = "A2DP";
private final LocalBluetoothProfileManager mProfileManager;
// Order of this profile in device profiles list // Order of this profile in device profiles list
private static final int ORDINAL = 1; private static final int ORDINAL = 1;
@@ -52,14 +53,18 @@ final class A2dpProfile implements LocalBluetoothProfile {
public void onServiceConnected(int profile, BluetoothProfile proxy) { public void onServiceConnected(int profile, BluetoothProfile proxy) {
mService = (BluetoothA2dp) proxy; mService = (BluetoothA2dp) proxy;
mProfileManager.setA2dpServiceUp(true);
} }
public void onServiceDisconnected(int profile) { public void onServiceDisconnected(int profile) {
mService = null; mService = null;
mProfileManager.setA2dpServiceUp(false);
} }
} }
A2dpProfile(Context context) { A2dpProfile(Context context, LocalBluetoothProfileManager profileManager) {
mProfileManager = profileManager;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(context, new A2dpServiceListener(), adapter.getProfileProxy(context, new A2dpServiceListener(),
BluetoothProfile.A2DP); BluetoothProfile.A2DP);
@@ -94,6 +99,19 @@ final class A2dpProfile implements LocalBluetoothProfile {
return mService.disconnect(device); return mService.disconnect(device);
} }
// This function is added as the AUTO CONNECT priority could not be set by using setPreferred(),
// as setPreferred() takes only boolean input but getPreferred() supports interger output.
// Also this need not implemented by all profiles so this has been added here.
public void enableAutoConnect(BluetoothDevice device, boolean enable) {
if (enable) {
mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT);
} else {
if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
}
}
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }

View File

@@ -25,6 +25,7 @@ import android.os.ParcelUuid;
import android.os.SystemClock; import android.os.SystemClock;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.bluetooth.BluetoothAdapter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@@ -118,6 +119,29 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
Log.d(TAG, "onProfileStateChanged: profile " + profile + Log.d(TAG, "onProfileStateChanged: profile " + profile +
" newProfileState " + newProfileState); " newProfileState " + newProfileState);
} }
if (profile instanceof HeadsetProfile) {
if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
if (BluetoothProfile.PRIORITY_AUTO_CONNECT != profile.getPreferred(mDevice))
mProfileManager.enableAutoConnectForHf(mDevice, true);
} else if (newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
// dont reset auto connect priority when bluetooth turned off
if ((BluetoothAdapter.STATE_ON == mLocalAdapter.getBluetoothState())
|| (BluetoothAdapter.STATE_TURNING_ON == mLocalAdapter.getBluetoothState())) {
mProfileManager.enableAutoConnectForHf(mDevice, false);
}
}
} else if (profile instanceof A2dpProfile ) {
if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
if (BluetoothProfile.PRIORITY_AUTO_CONNECT != profile.getPreferred(mDevice))
mProfileManager.enableAutoConnectForA2dp(mDevice,true);
} else if (newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
// dont reset auto connect priority when bluetooth turned off
if ((BluetoothAdapter.STATE_ON == mLocalAdapter.getBluetoothState())
|| (BluetoothAdapter.STATE_TURNING_ON == mLocalAdapter.getBluetoothState())) {
mProfileManager.enableAutoConnectForA2dp(mDevice, false);
}
}
}
mProfileConnectionState.put(profile, newProfileState); mProfileConnectionState.put(profile, newProfileState);
if (newProfileState == BluetoothProfile.STATE_CONNECTED) { if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
@@ -238,7 +262,7 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
connectInt(profile); connectInt(profile);
} }
private void connectInt(LocalBluetoothProfile profile) { synchronized void connectInt(LocalBluetoothProfile profile) {
if (!ensurePaired()) { if (!ensurePaired()) {
return; return;
} }

16
src/com/android/settings/bluetooth/HeadsetProfile.java Normal file → Executable file
View File

@@ -63,6 +63,7 @@ final class HeadsetProfile implements LocalBluetoothProfile {
// headset device. // headset device.
List<BluetoothDevice> deviceList = mService.getConnectedDevices(); List<BluetoothDevice> deviceList = mService.getConnectedDevices();
if (deviceList.isEmpty()) { if (deviceList.isEmpty()) {
mProfileManager.setHfServiceUp(true);
return; return;
} }
BluetoothDevice firstDevice = deviceList.get(0); BluetoothDevice firstDevice = deviceList.get(0);
@@ -76,12 +77,14 @@ final class HeadsetProfile implements LocalBluetoothProfile {
BluetoothProfile.STATE_CONNECTED); BluetoothProfile.STATE_CONNECTED);
mProfileManager.callServiceConnectedListeners(); mProfileManager.callServiceConnectedListeners();
mProfileManager.setHfServiceUp(true);
} }
public void onServiceDisconnected(int profile) { public void onServiceDisconnected(int profile) {
mProfileReady = false; mProfileReady = false;
mService = null; mService = null;
mProfileManager.callServiceDisconnectedListeners(); mProfileManager.callServiceDisconnectedListeners();
mProfileManager.setHfServiceUp(false);
} }
} }
@@ -158,6 +161,19 @@ final class HeadsetProfile implements LocalBluetoothProfile {
} }
} }
// This function is added as the AUTO CONNECT priority could not be set by using setPreferred(),
// as setPreferred() takes only boolean input but getPreferred() supports interger output.
// Also this need not implemented by all profiles so this has been added here.
public void enableAutoConnect(BluetoothDevice device, boolean enable) {
if (enable) {
mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT);
} else {
if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
}
}
}
public synchronized boolean isProfileReady() { public synchronized boolean isProfileReady() {
return mProfileReady; return mProfileReady;
} }

View File

@@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* LocalBluetoothProfileManager provides access to the LocalBluetoothProfile * LocalBluetoothProfileManager provides access to the LocalBluetoothProfile
@@ -77,6 +78,8 @@ final class LocalBluetoothProfileManager {
private final HidProfile mHidProfile; private final HidProfile mHidProfile;
private OppProfile mOppProfile; private OppProfile mOppProfile;
private final PanProfile mPanProfile; private final PanProfile mPanProfile;
private boolean isHfServiceUp;
private boolean isA2dpServiceUp;
/** /**
* Mapping from profile name, e.g. "HEADSET" to profile object. * Mapping from profile name, e.g. "HEADSET" to profile object.
@@ -128,7 +131,7 @@ final class LocalBluetoothProfileManager {
if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSource)) { if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSource)) {
if (mA2dpProfile == null) { if (mA2dpProfile == null) {
Log.d(TAG, "Adding local A2DP profile"); Log.d(TAG, "Adding local A2DP profile");
mA2dpProfile = new A2dpProfile(mContext); mA2dpProfile = new A2dpProfile(mContext, this);
addProfile(mA2dpProfile, A2dpProfile.NAME, addProfile(mA2dpProfile, A2dpProfile.NAME,
BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
} }
@@ -264,6 +267,51 @@ final class LocalBluetoothProfileManager {
} }
} }
synchronized void setHfServiceUp(boolean isUp) {
isHfServiceUp = isUp;
if (isHfServiceUp && isA2dpServiceUp) {
// connect hf and then a2dp
// this order is maintained as per the white paper
handleAutoConnect(mHeadsetProfile);
handleAutoConnect(mA2dpProfile);
}
}
synchronized void setA2dpServiceUp(boolean isUp) {
isA2dpServiceUp= isUp;
if (isHfServiceUp && isA2dpServiceUp) {
// connect hf and then a2dp
// this order is maintained as per the white paper
handleAutoConnect(mHeadsetProfile);
handleAutoConnect(mA2dpProfile);
}
}
private void handleAutoConnect(LocalBluetoothProfile profile) {
Set<BluetoothDevice> bondedDevices = mLocalAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
if (profile.getPreferred(device) ==
BluetoothProfile.PRIORITY_AUTO_CONNECT) {
Log.d(TAG,"handleAutoConnect for device");
CachedBluetoothDevice cacheDevice = mDeviceManager.findDevice(device);
if (null != cacheDevice) {
cacheDevice.connectInt(profile);
break;
}
else
Log.e(TAG,"Bluetooth cache devices mismatch with actual");
}
}
}
public void enableAutoConnectForHf(BluetoothDevice device,boolean enable) {
mHeadsetProfile.enableAutoConnect(device,enable);
}
public void enableAutoConnectForA2dp(BluetoothDevice device,boolean enable) {
mA2dpProfile.enableAutoConnect(device,enable);
}
// This is called by DockService, so check Headset and A2DP. // This is called by DockService, so check Headset and A2DP.
public synchronized boolean isManagerReady() { public synchronized boolean isManagerReady() {
// Getting just the headset profile is fine for now. Will need to deal with A2DP // Getting just the headset profile is fine for now. Will need to deal with A2DP