Android 10 如何解决蓝牙耳机听音乐过程,插入再拔出有线耳机,音乐不从蓝牙耳机发出

news/2025/1/15 23:46:11/

问题点:蓝牙耳机听音乐过程,插入再拔出有线耳机,音乐不从蓝牙耳机发出
代码路径:
packages/apps/Bluetooth/src/com/android/bluetooth/btservice/ActiveDeviceManager.java

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 {@Overridepublic 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) {

http://www.ppmy.cn/news/116793.html

相关文章

[RK3399-Android9.0] ES8316或ES8336 播放音乐时切换耳机,当状态栏耳机图标消失时,播放的音乐声音会关闭再打开一下的解决办法

问题描述&#xff1a; ES8316或ES8336 播放音乐时切换耳机&#xff0c;当状态栏耳机图标消失时&#xff0c;播放的音乐声音会关闭再打开一下的解决办法如下&#xff1a; frameworks\base\services\core\java\com\android\server\audio\AudioService.java private int checkSen…

蓝牙耳机连接笔记本后,音乐会断断续续,卡顿。

由于蓝牙传输带宽限制&#xff0c;在声音的传输中会有少许延迟。这个问题是无法避免的&#xff0c;改善的方法就是换一个好一点的耳机。 但是在蓝牙耳机连接到笔记本后&#xff0c;听歌时声音会出现断断续续的现象&#xff0c;时不时顿一下&#xff0c;十分影响体验。 后来发现…

苹果用什么蓝牙耳机好?适合苹果的音乐蓝牙耳机推荐

苹果耳机的市场一直以来都是火爆市场的&#xff0c;但价格一直都十分的高&#xff0c;近期我了解了多个品牌的蓝牙耳机&#xff0c;每个人对于蓝牙耳机的使用需求不同&#xff0c;下面我来推荐几款适合苹果的蓝牙耳机吧&#xff01; 一、南卡Lite pro2蓝牙耳机 耳机类型&…

简约而不失雅致,Sanag蓝牙耳机J2,让纯净音乐伴你左右

如今&#xff0c;真无线蓝牙耳机非常普遍&#xff0c;选择性也非常多。对于经常出差的我来说&#xff0c;耳机在便携性和续航等方面成了我最为关注的指标。新年初始&#xff0c;一款Sanag蓝牙耳机J2映入我的眼帘&#xff0c;它最为吸引我的是它独特的抽拉式充电仓设计&#xff…

骑行听音乐用什么耳机?骑行耳机推荐

作为一名骑行爱好者&#xff0c;自然少不了准备一副耳机。在骑行过程中不仅能听音乐还能连接手机&#xff0c;听地图导航十分的方便。市面上的而运动耳机琳琅满目&#xff0c;对于热爱运动的人来说一副适合自己的运动耳机必然会提高运动体验&#xff0c;如何挑选一副适合自己的…

哪种款式的耳机不伤耳朵,五款不伤害耳朵听力的骨传导耳机推荐

骨传导耳机是一种最为健康的听音方式了&#xff0c;利用开放式不入耳的听音方式&#xff0c;能够有效减少声波对耳膜的冲击&#xff0c;即使是长时间使用都能保证听力不受损害&#xff0c;选择不伤耳的耳机可以看看下面这些骨传导耳机分享~ 1、南卡Runner Pro4骨传导蓝牙耳机 &…

骨传导耳机的音质怎么样?

常常看到有小伙伴在问&#xff0c; 骨传导耳机怎么样&#xff0c;音质好不好 &#xff0c;在聊这个话题之前&#xff0c;我们先要知道传统入耳式耳机和骨传导耳机的到底有那些区别。传统耳机业内还叫气传导耳机&#xff0c;是通过外耳道内的空气传递声波至鼓膜后再到达听觉神经…

十大公认音质好的骨传导耳机,五款最值得入手的骨传导耳机

都说骨传导耳机只是听个响&#xff1f;但是现在做的好的骨传导耳机品牌&#xff0c;已经把耳机的音质技术调到十分好了&#xff0c;大家在选择骨传导耳机的时候&#xff0c;还是建议大家选择一些做到较大的骨传导品牌&#xff0c;这样耳机的整体体验听感也会更好。下面就给大家…