特性中的属性有两种:属性值或描述符。
支持通知或指示的特性中默认有一个描述符:客户端特性配置描述符(Client Characteristic Configuration Descriptor,CCCD)。它的UUID是0x2902。
添加描述符的函数是:
uint32_t sd_ble_gatts_descriptor_add ( uint16_t char_handle,
ble_gatts_attr_t const *const p_attr,
uint16_t *const p_handle
)
Parameters
[in] | char_handle | Handle of the characteristic where the descriptor is to be placed, if BLE_GATT_HANDLE_INVALID is used, it will be placed sequentially. |
[in] | p_attr | Pointer to the attribute structure. |
[out] | p_handle | Pointer to a 16-bit word where the assigned handle will be stored. |
示例代码:
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_REPORT_REF_DESCR);
memset(&attr_md, 0, sizeof(attr_md));
attr_md.read_perm = p_bas_init->battery_level_report_read_perm;
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 0;
attr_md.vlen = 0;
init_len = ble_srv_report_ref_encode(encoded_report_ref, p_bas_init->p_report_ref);
memset(&attr_char_value, 0, sizeof(attr_char_value));
attr_char_value.p_uuid = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = init_len;
attr_char_value.init_offs = 0;
attr_char_value.max_len = attr_char_value.init_len;
attr_char_value.p_value = encoded_report_ref;
err_code = sd_ble_gatts_descriptor_add(p_bas->battery_level_handles.value_handle,
&attr_char_value,
&p_bas->report_ref_handle);
if (err_code != NRF_SUCCESS)
{
return err_code;
}