文章目录
一、功能需求
单耳或对耳使用,有手机配对记录时,开机耳机先回连手机,超时未连接则进配对模式。
二、功能实现
开机时启动一个timer,设定回连时间,若超时无连接则进手机配对模式(BT可见可连接状态)
2-1、定义回连超时处理Timer
//apps.cpp
#ifdef USER_MOBILE_RECONN_TIMEOUT_PROCESS
osTimerId app_mobile_reconn_timer_id = NULL;
static void app_mobile_reconn_timeout_handler(void const *param);
osTimerDef (APP_MOBILE_RECONN_TIMER, app_mobile_reconn_timeout_handler);
static void app_mobile_reconn_timeout_handler(void const *param)
{
//ibrt_ctrl_t *p_ibrt_ctrl = app_ibrt_if_get_bt_ctrl_ctx();
bud_box_state local_box_state = app_ui_get_local_box_state();
TRACE(1, "%s", __func__);
TRACE(1, "is any mobile conn: %d", app_ui_any_mobile_device_connected());
TRACE(1, "tws conn status: %d", app_tws_ibrt_tws_link_connected());
TRACE(1, "ui role: %d ", app_ui_get_current_role());
TRACE(2, "is charging: %d, local_box_state: %d", app_battery_is_charging(), app_ui_get_local_box_state());
if(app_battery_is_charging() || local_box_state < IBRT_IN_BOX_OPEN)
return ;
if(!app_ibrt_if_is_ui_slave() && !app_ui_any_mobile_device_connected())
{
if(!app_tws_ibrt_tws_link_connected())
{
//app_tws_ibrt_clear_tws_reconnecting();
app_ibrt_conn_ibrt_tws_disconnect();
}
app_ibrt_conn_set_discoverable_connectable(true, true);
}
}
void app_mobile_reconn_timer_init(void)
{
if(NULL == app_mobile_reconn_timer_id)
{
app_mobile_reconn_timer_id = osTimerCreate(osTimer(APP_MOBILE_RECONN_TIMER), osTimerOnce, NULL);
}
}
void app_start_mobile_reconn_timer(uint32_t timeout)
{
osTimerStop(app_mobile_reconn_timer_id);
osTimerStart(app_mobile_reconn_timer_id, timeout);
}
void app_stop_mobile_reconn_timer(void)
{
osTimerStop(app_mobile_reconn_timer_id);
}
#endif
2-2、初始化timer
//apps.cpp
int app_init(void)
{
...
#ifdef USER_MOBILE_RECONN_TIMEOUT_PROCESS
app_mobile_reconn_timer_init();
#endif
...
}
2-3、开盖/按键/5V等方式开机时启动回连超时处理timer
//app_bt.cpp
#ifdef USER_MOBILE_RECONN_TIMEOUT_PROCESS
//nv_record_get_paired_dev_count() include tws link
int app_bt_get_mobile_paired_count(void)
{
uint8_t mobile_paired_num = 0;
btif_device_record_t record = {0};
int paired_dev_count = nv_record_get_paired_dev_count();
for(int i = 0; i < paired_dev_count; i++)
{
nv_record_enum_dev_records(i, &record);
if(MOBILE_LINK == app_tws_ibrt_get_link_type_by_addr(&record.bdAddr))
{
mobile_paired_num++;
}
}
TRACE(2, "%s, mobile_paired_num: %d", __func__, mobile_paired_num);
return mobile_paired_num;
}
#endif
//在开盖等需要的地方启动回连超时处理timer
#ifdef CUSTOM_APP_RECONN_PROCESS
if(app_bt_get_mobile_paired_count() > 0)
{
//若有手机配对记录,10秒后未回连/连接成功则配对模式
app_start_mobile_reconn_timer(10000);
}
else
{
//无手机配对记录,3秒后进配对模式
app_start_mobile_reconn_timer(3000);
}
#endif
2-4、手机连接成功时停止回连超时timer
//手机断开连接的地方停止回连超时处理timer
#ifdef CUSTOM_APP_RECONN_PROCESS
app_stop_mobile_reconn_timer();
#endif
2-5、对耳使用且未与手机连接时,主耳入仓关盖或关机,副耳回连手机超时也要进配对
(有空再补充完整)