自定义BLE数据发送函数,就是将数据发送、数据发送前的检查、以及conn_handle查询等封装在一起,脱离SDK中的相关回调函数,在程序任意位置实现发送数据功能。
1. SDK中的BLE数据发送函数
BLE的数据发送函数定义在apps\common\third_party_profile\jieli\gatt_common\le_gatt_common.c文件里。
int ble_comm_att_send_data(u16 conn_handle, u16 att_handle, u8 *data, u16 len, att_op_type_e op_type)
{
gatt_op_ret_e ret = GATT_OP_RET_SUCESS;
u16 tmp_16;
if (!conn_handle) {
return GATT_CMD_PARAM_ERROR;
}
switch (op_type) {
case ATT_OP_READ:
tmp_16 = 0x55A1;//fixed
ret = ble_op_multi_att_send_data(conn_handle, att_handle, &tmp_16, 2, op_type);
break;
case ATT_OP_READ_LONG:
tmp_16 = 0x55A2;//fixed
ret = ble_op_multi_att_send_data(conn_handle, att_handle, &tmp_16, 2, op_type);
break;
default:
ret = ble_op_multi_att_send_data(conn_handle, att_handle, data, len, op_type);
break;
}
if (ret) {
const char *err_string;
int error_id = (int)0 - (int)GATT_CMD_RET_BUSY + (int)ret;
if (error_id >= 0 && error_id < sizeof(gatt_op_error_string) / sizeof(char *)) {
err_string = gatt_op_error_string[error_id];
} else {
err_string = "UNKNOW GATT_ERROR";
}
log_error("att_send_fail: %d!!!,%s", ret, err_string);
log_error("param:%04x, %04x, %02x,len= %d", conn_handle, att_handle, op_type, len);
/* put_buf(data,len); */
}
return ret;

文章介绍了如何自定义BLE数据发送函数,将数据发送、检查和连接处理等步骤封装,以实现在程序任意位置发送数据。关键函数包括`ble_comm_att_send_data`,配合`ble_comm_att_check_send`检查缓存和`ble_gatt_server_characteristic_ccc_get`验证NOTIFY权限。此外,还详细阐述了连接句柄查询和ATT服务特征表的作用。

2990

被折叠的 条评论
为什么被折叠?



