该文章基于Android Q
1 连接音频
在手机音频正常连接时,接通电话,点选蓝牙通话。mDeviceManager.connectAudio返回true。
如果是之前默认蓝牙mDeviceManager.connectAudio返回false。
packages/services/Telecomm/src/com/android/server/telecom/bluetooth/BluetoothRouteManager.java
private String connectBtAudio(String address, int retryCount) {
......
if (!mDeviceManager.connectAudio(actualAddress)) {
boolean shouldRetry = retryCount < MAX_CONNECTION_RETRIES;
Log.w(LOG_TAG, "Could not connect to %s. Will %s", actualAddress,
shouldRetry ? "retry" : "not retry");
if (shouldRetry) {
SomeArgs args = SomeArgs.obtain();
args.arg1 = Log.createSubsession();
args.arg2 = actualAddress;
args.argi1 = retryCount + 1;
sendMessageDelayed(RETRY_HFP_CONNECTION, args,
mTimeoutsAdapter.getRetryBluetoothConnectAudioBackoffMillis(
mContext.getContentResolver()));
}
return null;
}
return actualAddress;
}
packages/services/Telecomm/src/com/android/server/telecom/bluetooth/BluetoothDeviceManager.java
public boolean connectAudio(String address) {
if (mHearingAidDevicesByAddress.containsKey(address)) {
......
} else if (mHfpDevicesByAddress.containsKey(address)) {
BluetoothDevice device = mHfpDevicesByAddress.get(address);
if (mBluetoothHeadsetService == null) {
Log.w(this, "Attempting to turn on audio when the headset service is null");
return false;
}
boolean success = mBluetoothHeadsetService.setActiveDevice(device);
if (!success) {
Log.w(this, "Couldn't set active device to %s", address);
return false;
}
if (!mBluetoothHeadsetService.isAudioOn()) {
return mBluetoothHeadsetService.connectAudio();
}
return true;
} else {
Log.w(this, "Attempting to turn on audio for a disconnected device");
return false;
}
}
packages/services/Telecomm/src/com/android/server/telecom/BluetoothHeadsetProxy.java
public boolean connectAudio() {
return mBluetoothHeadset.connectAudio();
}
frameworks/base/core/java/android/bluetooth/BluetoothHeadset.java
public boolean connectAudio() {
final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try {
return service.connectAudio();
} catch (RemoteException e) {
Log.e(