BES2300x notes (2)-TWS pairing and Bluetooth pairing (Peer or Pair silly and confused)

Hello everyone, this is the second article in this series~ article~ Reprint the article, please indicate the source

<<[Series Blog Index] Fast Track>>

First of all, the previous link: TWS headphones are all over Huaqiangbei! Why can’t Apple AirPods?

I. Introduction

I saw a "dao friend" leaving a message in the comment area: I am confused about the TWS pairing, BT matching and back-to-back process , so in this second article, let's talk about the relevant processes and interfaces of the BES platform. (PS: The basic part of Bluetooth will not be repeated, there are many materials on the Internet for reference)

二、Peer or Pair

1. Pair (Peer)

According to the previous experience on Qualcomm platform, the left and right ears are each other's peer device, because the level of the left and right ears is the same relative to the mobile phone. This is not quite accurate in the relay mode, but it is very vivid in the monitor mode. ;
(Apple’s snoop, Qualcomm’s mirror, Hengxuan’s ibrt, and Lorda’s MCsync are all similar to monitoring technologies. In addition, Qualcomm’s TWS+ and Huawei’s dual channels belong to the left and right dual channel transmission technology and require a mobile phone. Support at the same time, so mobile phone brand manufacturers can build an ecosystem here)
In order to facilitate the distinction, we temporarily call the process of establishing a connection between the left and right ears, simply called pairing.

2. Pair

When the headset is turned on inquiry scan and page scan, it can be searched by the mobile phone and a connection is established. For the time being, the process of establishing a connection between the headset and the mobile phone is simply called pairing.

Three, boot process

Insert picture description here
After power on, the program enters APP_POWERON_CASE_NORMAL mode according to pwron_case, where nv_record_env_get(&nvrecord_env) interface is used to read the flash, and then judge according to ibrt_mode.mode;

If(ibrt_mode.mode==IBRT_UNKNOW), the left and right ear pairing process will be entered;
otherwise, the out-of-box event will be passed in through the interface app_ibrt_ui_event_entry(IBRT_FETCH_OUT_EVENT) to trigger the following state machine;Insert picture description here
To open the lid, exit the box, insert the box, and close the lid, you must follow this procedure to call the interface in the figure, otherwise the executed action may not take effect, because the underlying state will be protected.

Three, TWS pair

1. When using for the first time, both the left and right ears need to call the app_ibrt_enter_limited_mode() interface to enter the BTIF_BAM_LIMITED_ACCESSIBLE mode for the first time . Then which earphone calls the app_start_tws_serching_direactly() interface , and which earphone is used as the master ear to search for the slave ear pair;
2. After the ear searches for the slave ear, it will judge whether the first three bytes of the Bluetooth address are consistent with the Bluetooth name. If they are the same, the pair connection will be initiated, and the master ear will use the Bluetooth address of the slave ear. This shows the Bluetooth address of the slave ear, and there is only one Bluetooth device for the mobile phone;
3. After the master-slave pair is successfully paired, the role information nv_role will be written into the flash, and the master ear will enter the Pairing mode
(BTIF_BAM_GENERAL_ACCESSIBLE). Connected by mobile phone search.
4. After the mobile phone is successfully connected, create an IBRT connection from the ear;

Fourth, pair with mobile phone Bluetooth

1. First use

Based on the third point, we know that after successfully using the left and right ear pairing for the first time, the main ear will enter the Pairing mode and can be searched and connected by the mobile phone. This point is clear, and then we will look down.

2. Normal use

During normal use of the headset, it may sometimes need to change the connected mobile phone. How to make the headset re-enter the Pairing mode and be searched and connected by other mobile phones? Here we provide two ideas:

思路1:
	osDelay(100);
	hal_sw_bootmode_clear(HAL_SW_BOOTMODE_REBOOT);
	hal_sw_bootmode_set(HAL_SW_BOOTMODE_REBOOT_ENTER_PAIRING);
	hal_cmu_sys_reboot();
开机后:
	if (hal_sw_bootmode_get() & HAL_SW_BOOTMODE_REBOOT_ENTER_PAIRING) 
	{
    	hal_sw_bootmode_clear(HAL_SW_BOOTMODE_REBOOT_ENTER_PAIRING);
		app_ibrt_if_enter_pairing_after_tws_connected();
	}


思路2:
	if(app_tws_is_freeman_mode())
	{
        if (app_tws_ibrt_mobile_link_connected())
        {
            app_tws_ibrt_disconnect_mobile();
        }
        app_ibrt_ui_event_entry(IBRT_FREEMAN_PAIRING_EVENT);
	}
	else if(app_tws_is_master_mode())
	{
        if (app_tws_ibrt_tws_link_connected() && app_tws_ibrt_mobile_link_connected())
        {
            app_tws_ibrt_disconnect_mobile();
        }
        app_ibrt_ui_event_entry(IBRT_TWS_PAIRING_EVENT);
	}

Five, regroup

In the production process or in the hands of the user, there may be situations that need to be re-paired. For example, a headset is broken or lost. How to re-pair the headset that has been paired?
Our idea is to clear the previous pair information, which is the mode, and then reset it to enter the process of first use;

代码流程如下:
	struct nvrecord_env_t *nvrecord_env;
    nv_record_env_get(&nvrecord_env);
    nvrecord_env->ibrt_mode.mode = IBRT_UNKNOW;

    nv_record_env_set(nvrecord_env);
    nv_record_flash_flush();

    osDelay(100);
    hal_sw_bootmode_clear(HAL_SW_BOOTMODE_REBOOT);
    hal_cmu_sys_reboot();

Six, common interfaces

1. TWS connection

btif_besaud_is_connected()
app_tws_ibrt_tws_link_connected()

2. TWS main ear

app_tws_ibrt_mobile_link_connected() //与手机连接

3. TWS from the ear

app_tws_ibrt_slave_ibrt_link_connected() //ibrt slave
app_ibrt_ui_ibrt_connected()

4. Single ear mode

app_ibrt_ui_get_freeman_enable() //实际测试在单耳模式或者TWS单个使用时,角色都是master,这个值都为0??
app_ibrt_if_enter_freeman_pairing()

5. Flash read and write

nv_record_env_get() //读数据

nv_record_env_set() //写数据
nv_record_flash_flush()

6. Phone type

btif_dip_check_is_ios_device()
btif_dip_check_is_ios_by_vend_id()

7. Other

app_ibrt_nvrecord_delete_all_mobile_record() //清除配对记录
app_ibrt_remove_history_paired_device() //清除peer组队记录
app_ibrt_if_event_entry() //触发底层状态机
app_ibrt_if_enter_pairing_after_tws_connected()

Seven, notice

Subsequent chapters will introduce ANC channel configuration, EQ function, OTA upgrade and master-slave switching, etc.;

Guess you like

Origin blog.csdn.net/zhanghuaishu0/article/details/108684583