蓝牙耳机Priority设置流程

     当蓝牙耳机配对成功后,会发送一个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,该耳机就会自动进行连接。

你可能感兴趣的:(蓝牙耳机)