1.打开dialog SDK内工程prox_reporter,在user_config.h中设置user_connection_param_conf,这里将最小和最大连接间隔设为一样都为125ms。
static const struct connection_param_configuration user_connection_param_conf = {
/// Connection interval minimum measured in ble double slots (1.25ms)
/// use the macro MS_TO_DOUBLESLOTS to convert from milliseconds (ms) to double slots
.intv_min = MS_TO_DOUBLESLOTS(125),
/// Connection interval maximum measured in ble double slots (1.25ms)
/// use the macro MS_TO_DOUBLESLOTS to convert from milliseconds (ms) to double slots
.intv_max = MS_TO_DOUBLESLOTS(125),
/// Latency measured in connection events
.latency = 0,
/// Supervision timeout measured in timer units (10 ms)
/// use the macro MS_TO_TIMERUNITS to convert from milliseconds (ms) to timer units
.time_out = MS_TO_TIMERUNITS(1250),
/// Minimum Connection Event Duration measured in ble double slots (1.25ms)
/// use the macro MS_TO_DOUBLESLOTS to convert from milliseconds (ms) to double slots
.ce_len_min = MS_TO_DOUBLESLOTS(0),
/// Maximum Connection Event Duration measured in ble double slots (1.25ms)
/// use the macro MS_TO_DOUBLESLOTS to convert from milliseconds (ms) to double slots
.ce_len_max = MS_TO_DOUBLESLOTS(0),
};
2.设置的参数将会在app.c文件内的app_easy_gap_param_update_msg_create函数调用,此函数是静态函数,会被app_easy_gap_param_update_start()函数调用。
static struct gapc_param_update_cmd* app_easy_gap_param_update_msg_create(uint8_t conidx)
{
// Allocate a message for GAP
if (param_update_cmd[conidx] == NULL)
{
struct gapc_param_update_cmd* cmd;
cmd = app_param_update_msg_create(conidx);
ASSERT_WARNING(conidx < APP_EASY_GAP_MAX_CONNECTION);
param_update_cmd[conidx] = cmd;
cmd->intv_max = user_connection_param_conf.intv_max;
cmd->intv_min = user_connection_param_conf.intv_min;
cmd->latency = user_connection_param_conf.latency;
cmd->time_out = user_connection_param_conf.time_out;
cmd->ce_len_min = user_connection_param_conf.ce_len_min;
cmd->ce_len_max = user_connection_param_conf.ce_len_max;
}
return param_update_cmd[conidx];
}
void app_easy_gap_param_update_start(uint8_t conidx)
{
struct gapc_param_update_cmd* cmd;
cmd = app_easy_gap_param_update_msg_create(conidx);
// Send the message
app_param_update_msg_send(cmd);
param_update_cmd[conidx] = NULL;
}
3.app_easy_gap_param_update_start默认是没有调用的,我们需要在设备连接成功后调用此函数,来更新连接间隔。我这里在gapc_connection_req_ind_handler内做了一个3s的定时器,连接成功后3秒钟更新连接间隔。
uint8_t updata_param_cnt_index = 0;
static void app_updata_connect_param_data(void)
{
app_easy_gap_param_update_start(updata_param_cnt_index);
}
static int gapc_connection_req_ind_handler(ke_msg_id_t const msgid,
struct gapc_connection_req_ind const *param,
ke_task_id_t const dest_id,
ke_task_id_t const src_id)
{
// Connection Index
if (ke_state_get(dest_id) == APP_CONNECTABLE)
{
uint8_t conidx = KE_IDX_GET(src_id);
ASSERT_WARNING(conidx < APP_EASY_MAX_ACTIVE_CONNECTION);
app_env[conidx].conidx = conidx;
updata_param_cnt_index = conidx;
if (conidx != GAP_INVALID_CONIDX)
{
app_env[conidx].connection_active = true;
ke_state_set(TASK_APP, APP_CONNECTED);
// Retrieve the connection info from the parameters
app_env[conidx].conhdl = param->conhdl;
app_env[conidx].peer_addr_type = param->peer_addr_type;
memcpy(app_env[conidx].peer_addr.addr, param->peer_addr.addr, BD_ADDR_LEN);
#if (BLE_APP_SEC)
// send connection confirmation
app_easy_gap_confirm(conidx, (enum gap_auth) app_sec_env[conidx].auth, 1);
#else
app_easy_gap_confirm(conidx, GAP_AUTH_REQ_NO_MITM_NO_BOND, 1);
#endif
}
CALLBACK_ARGS_2(user_app_callbacks.app_on_connection, conidx, param)
app_easy_timer(300, app_updata_connect_param_data);
}
else
{
// APP_CONNECTABLE state is used to wait the GAP_LE_CREATE_CONN_REQ_CMP_EVT message
ASSERT_ERROR(0);
}
return (KE_MSG_CONSUMED);
}
4.到此连接间隔的参数就更新完成了,通过手机连接设备后,等待连接参数更新后,可以看到设备与手机间通信事件由48ms一次变为了每125ms一次。