蓝牙耳机Priority设置流程

132 篇文章 88 订阅

当蓝牙耳机配对成功后,会发送一个BONDING_STATE_CHANGE的消息,在BondStateMachine状态机里会对这个消息进行处理,调用setProfilePriorty(dev)函数对当前的耳机进行优先级的设置,

把以前的设置优先级顺序Hid、A2DP、Headset改为Headset、A2DP、Hid,请看这个函数修改后的具体code:

private void setProfilePriorty(BluetoothDevice device){

       HidService hidService = HidService.getHidService();

       A2dpService a2dpService = A2dpService.getA2dpService();

       HeadsetService headsetService = HeadsetService.getHeadsetService();

       if ((headsetService != null) &&

            (headsetService.getPriority(device)== BluetoothProfile.PRIORITY_UNDEFINED)){

            headsetService.setPriority(device,BluetoothProfile.PRIORITY_ON);

       }

       if ((a2dpService != null) &&

            (a2dpService.getPriority(device) ==BluetoothProfile.PRIORITY_UNDEFINED)){

            a2dpService.setPriority(device,BluetoothProfile.PRIORITY_ON);

       }

              if((hidService != null) &&

            (hidService.getPriority(device) ==BluetoothProfile.PRIORITY_UNDEFINED)){

           hidService.setPriority(device,BluetoothProfile.PRIORITY_ON);

       }

   }

会把当前耳机的Headset Profile 和A2DP Profile的优先级设置为PRIORITY_ON。

    当蓝牙耳机连接成功时,processProfileStateChanged函数会调用 setProfileAutoConnectionPriority(device,profileId)函数设置优先级,看修改后这个函数的具体code:

void setProfileAutoConnectionPriority(BluetoothDevice device, int profileId){

             HeadsetService  hsService = HeadsetService.getHeadsetService();

             A2dpService a2dpService =A2dpService.getA2dpService();

             if ((hsService != null) &&

               (BluetoothProfile.PRIORITY_AUTO_CONNECT !=hsService.getPriority(device))){

                 adjustOtherHeadsetPriorities(hsService,device);

                hsService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);

             }

             if ((a2dpService != null)&&

               (BluetoothProfile.PRIORITY_AUTO_CONNECT != a2dpService.getPriority(device))){

                adjustOtherSinkPriorities(a2dpService, device);

                a2dpService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT);

             }

   }

会把当前耳机的Headset和A2DP的Profile优先级都设置为PRIORITY_AUTO_CONNECT,同时会把其他的耳机设备的优先级设为PRIORITY_ON,保证耳机自动连接优先级的设备只有一个。

蓝牙耳机自动连接过程:

当蓝牙打开时,如果有配对的蓝牙耳机存在,就会根据它的优先级,判断是否进行自动连接。如果优先级为PRIORITY_AUTO_CONNECT,该耳机就会自动进行连接。
--------------------- 
版权声明:本文为CSDN博主「就爱吃鲜橙」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/OswinWang/article/details/60871463

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值