• Android9 查看连接多个蓝牙耳机查看使用中的蓝牙耳机


    #Android9 查看连接多个蓝牙耳机查看使用中的蓝牙耳机

    Android 9.0之后,支持一台手机可以同时连接多个蓝牙设备。

    但是判断那个蓝牙设备是使用中,需要经过一些复杂判断才知道!

    一、主要api:

     boolean a2dpPlaying = BluetoothA2dp.isA2dpPlaying(BluetoothDevice);
    
    
    • 1
    • 2

    看起来不难,但是 BluetoothA2dp 和 BluetoothDevice 如何获取到?

    BluetoothA2dp 获取需要连接服务,BluetoothDevice 遍历所有蓝牙即可。

    二、BluetoothA2dp 对象的获取

    
    
        BluetoothA2dp mA2dp;
    	
        public void initA2dpAdapter() {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter.isEnabled()) { //判断蓝牙是否开启
                //获取A2DP代理对象
                if (mA2dp == null) { //不能重复连接服务,否则会报错
                    mBluetoothAdapter.getProfileProxy(getContext(), mBluetoothProfileListener, BluetoothProfile.A2DP);
                }
            } else {
                LogUtil.error("getA2dpAdapter error. bluetooth is not Enabled");
            }
        }
    	
        //getProfileProxy并不会直接返回A2DP代理对象,而是通过mListener中回调获取。
        private BluetoothProfile.ServiceListener mBluetoothProfileListener = new BluetoothProfile.ServiceListener() {
            @Override
            public void onServiceDisconnected(int profile) {
                if (profile == BluetoothProfile.A2DP) {
                    LogUtil.error("A2dp onServiceDisconnected ");
                    mA2dp = null;
                }
            }
    
    		// getProfileProxy 到 连接成功一般需要几十毫秒!
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                if (profile == BluetoothProfile.A2DP) {
                    LogUtil.error("A2dp onServiceConnected ");
                    mA2dp = (BluetoothA2dp) proxy; //转换 ,这个就是  BluetoothA2dp 对象
                    //不显示蓝牙界面连接耳机会出现多个连接状态的蓝牙耳机,断开非使用中的蓝牙耳机
                    judgeIsConnectManyBluetooth();
                }
            }
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    三、获取 BluetoothDevice 对象,

    	//判断所有的耳机设备是否存在多个连接状态的
        private boolean isConnectManyBluetooth() {
            //未从已连接设备列表中找到断开的api,从所有的设备列表中进行判断连接状态后断开
    		LocalBluetoothManager mBluetoothManager = LocalBluetoothManager.getInstance(context, mOnInitCallback);
            Collection cachedDevices = mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();
            LogUtil.debug("cachedDevices size = " + cachedDevices.size());
            //更新蓝牙列表UI
            int connectDevices = 0;
            for (CachedBluetoothDevice cachedDevice : cachedDevices) {
                String name = cachedDevice.getDevice().getName();
                if (cachedDevice.isConnected()) {
                    LogUtil.debug("cachedDevices name = " + name);
                    connectDevices++;
                }
                if (connectDevices > 1) {
                    return true;
                }
            }
            return false;
        }
    
        //判断是否多个蓝牙连接状态,如果是就断开非使用中的蓝牙
        private void judgeIsConnectManyBluetooth() {
            if (isConnectManyBluetooth()) {
                //获取当前使用中的蓝牙耳机对象,断开其他耳机对象
    			LocalBluetoothManager mBluetoothManager = LocalBluetoothManager.getInstance(context, mOnInitCallback);
                Collection cachedDevices = mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();
                LogUtil.debug("cachedDevices size = " + cachedDevices.size());
                //更新蓝牙列表UI
                for (CachedBluetoothDevice cachedDevice : cachedDevices) {
                    String name = cachedDevice.getDevice().getName();
                    if (mA2dp != null && cachedDevice.getDevice() != null) {
                        boolean a2dpPlaying = mA2dp.isA2dpPlaying(cachedDevice.getDevice());  //判断蓝牙是否使用中!
                        if (a2dpPlaying) {
                            LogUtil.debug("cachedDevices a2dpPlaying name = " + name);
                            disconnectOtherExceptOne(name, cachedDevice.getDevice().getBluetoothClass().getMajorDeviceClass());
                        }
                    }
    
                }
            }
        }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    LocalBluetoothManager 这个类是SettingLib 里面的类,如果不是系统源码或者系统应用是调用不到的!可以用反射!

    上面是实际项目中的一段代码,其实具体获取 BluetoothDevice api只有个两句:

    CachedBluetoothDevice cachedDevice;//单个蓝牙对象
    BluetoothDevice  bluetoothDevice = cachedDevice.getDevice();
    
    
    • 1
    • 2
    • 3

    四、其他:

         //已连接/绑定设备列表
         Set bondedDevices = mBluetoothManager.getBluetoothAdapter().getBondedDevices();
    
    
    • 1
    • 2
    • 3

    BluetoothDevice 对象没有啥连接和断开操作的方法!只有一下配置信息。

  • 相关阅读:
    FullCalendar日历组件:进行任务增删改,参考gitee例子修改
    全球超9千台VNC服务器无保护措施,中国占比最高
    Github每日精选(第33期):Screenshot-to-code训练 AI 将设计模型转换为 HTML 和 CSS
    ict的终极模式 是软件研发
    1.3优先级
    element-ui 修改el-form-item样式
    基于ssm技术的校自助阅览室的设计与实现毕业设计源码242326
    项目焊接(上)
    C规范编辑笔记(二)
    软件项目验收测试报告-软件项目验收流程
  • 原文地址:https://blog.csdn.net/wenzhi20102321/article/details/133715403