Android 如何解决蓝牙耳机听音乐过程,插入再拔出有线耳机,音乐不从蓝牙耳机发出
https://blog.csdn.net/wq892373445/article/details/125113108
问题点:蓝牙耳机听音乐过程,插入再拔出有线耳机,音乐不从蓝牙耳机发出
代码路径:
packages/apps/Bluetooth/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
高通定制代码路径:
vendor/qcom/opensource/commonsys/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
修复如上问题的patch:
a/src/com/android/bluetooth/btservice/ActiveDeviceManager.java +++ b/src/com/android/bluetooth/btservice/ActiveDeviceManager.java @@ -110,12 +110,13 @@ class ActiveDeviceManager { private static final int MESSAGE_HFP_ACTION_CONNECTION_STATE_CHANGED = 4; private static final int MESSAGE_HFP_ACTION_ACTIVE_DEVICE_CHANGED = 5; private static final int MESSAGE_HEARING_AID_ACTION_ACTIVE_DEVICE_CHANGED = 6; - + private static final int MESSAGE_DELAY_ENTER_A2DP_ACTIVE_DEVICE= 7; private final AdapterService mAdapterService; private final ServiceFactory mFactory; private HandlerThread mHandlerThread = null; private Handler mHandler = null; private final AudioManager mAudioManager; + private int volume ; private final AudioManagerAudioDeviceCallback mAudioManagerAudioDeviceCallback; private final List<BluetoothDevice> mA2dpConnectedDevices = new LinkedList<>(); @@ -320,6 +321,13 @@ class ActiveDeviceManager { } } break; + case MESSAGE_DELAY_ENTER_A2DP_ACTIVE_DEVICE: { + Log.d(TAG, "MESSAGE_DELAY_ENTER_A2DP_ACTIVE_DEVICE "); + setA2dpActiveDevice(); + mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); + } + break; + } } } @@ -361,6 +369,21 @@ class ActiveDeviceManager { @Override public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) { + Log.d(TAG, "onAudioDevicesRemoved() isWiredHeadsetOn " +mAudioManager.isWiredHeadsetOn()); + if (mAudioManager.isWiredHeadsetOn()) { + return; + } + + for (AudioDeviceInfo device : removedDevices) { + Log.d(TAG, "onAudioDevicesRemoved: removedDevices: " + device.getType()+" isWiredAudioHeadset "+ isWiredAudioHeadset(devi ce)); + if (isWiredAudioHeadset(device)) { + volume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); + mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); + mHandler.removeMessages(MESSAGE_DELAY_ENTER_A2DP_ACTIVE_DEVICE); + mHandler.sendEmptyMessageDelayed(MESSAGE_DELAY_ENTER_A2DP_ACTIVE_DEVICE,1000); + break; + } + } } } @@ -368,6 +391,7 @@ class ActiveDeviceManager { mAdapterService = service; mFactory = factory; mAudioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE); + volume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); mAudioManagerAudioDeviceCallback = new AudioManagerAudioDeviceCallback(); } @@ -433,6 +457,15 @@ class ActiveDeviceManager { } mA2dpActiveDevice = device; } + + private void setA2dpActiveDevice() { + Log.d(TAG, "setA2dpActiveDevice()"); + if (mA2dpActiveDevice == null && !mA2dpConnectedDevices.isEmpty()) { + int prevActiveDeviceIndex = mA2dpConnectedDevices.size() - 1; + BluetoothDevice prevActiveDevice = mA2dpConnectedDevices.get(prevActiveDeviceIndex); + setA2dpActiveDevice(prevActiveDevice); + } + } private void setHfpActiveDevice(BluetoothDevice device) {