1.目的
给串口的2个特征值加上描述
2.分析
很多时候,我们定义特征值的时候,都希望一目了然,知道这个特征值干什么的,这里我们来介绍给特征值加上描述符,这样一看描述就知道这个特征值做什么的了
3.平台:
协议栈版本:SDK10.0.0
编译软件:keil 5.14
硬件平台:nrf51822最小系统
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4
4.步骤
在这里,我们把串口发送服务,添加描述符为 “Tx”
串口接收服务,添加描述符为“Rx”
static uint32_t tx_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init) { ble_gatts_char_md_t char_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md; char tx_desc[] = "Tx" ; //添加代码1 memset(&char_md, 0, sizeof(char_md)); char_md.p_char_user_desc = (uint8_t *)tx_desc ; //添加代码2 char_md.char_user_desc_size = strlen(tx_desc);//添加代码3 char_md.char_user_desc_max_size =strlen(tx_desc);//添加代码4 char_md.char_props.write = 1; char_md.char_props.write_wo_resp = 1; //char_md.p_char_user_desc = NULL; char_md.p_char_pf = NULL; char_md.p_user_desc_md = NULL; char_md.p_cccd_md = NULL; char_md.p_sccd_md = NULL; ble_uuid.type = p_nus->uuid_type; ble_uuid.uuid = BLE_UUID_NUS_TX_CHARACTERISTIC; memset(&attr_md, 0, sizeof(attr_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); attr_md.vloc = BLE_GATTS_VLOC_STACK; attr_md.rd_auth = 0; attr_md.wr_auth = 0; attr_md.vlen = 1; 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 = 1; attr_char_value.init_offs = 0; attr_char_value.max_len = BLE_NUS_MAX_TX_CHAR_LEN; return sd_ble_gatts_characteristic_add(p_nus->service_handle, &char_md, &attr_char_value, &p_nus->tx_handles); }
static uint32_t rx_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init) { /**@snippet [Adding proprietary characteristic to S110 SoftDevice] */ ble_gatts_char_md_t char_md; ble_gatts_attr_md_t cccd_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md; char rx_desc[] = "Rx" ; //添加代码1 memset(&cccd_md, 0, sizeof(cccd_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); cccd_md.vloc = BLE_GATTS_VLOC_STACK; memset(&char_md, 0, sizeof(char_md)); char_md.char_props.notify = 1; char_md.p_char_user_desc = (uint8_t *)rx_desc ; //添加代码2 char_md.char_user_desc_size = strlen(rx_desc);//添加代码3 char_md.char_user_desc_max_size =strlen(rx_desc);//添加代码4 char_md.p_char_pf = NULL; char_md.p_user_desc_md = NULL; char_md.p_cccd_md = &cccd_md; char_md.p_sccd_md = NULL; ble_uuid.type = p_nus->uuid_type; ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC; memset(&attr_md, 0, sizeof(attr_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); attr_md.vloc = BLE_GATTS_VLOC_STACK; attr_md.rd_auth = 0; attr_md.wr_auth = 0; attr_md.vlen = 1; 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 = sizeof(uint8_t); attr_char_value.init_offs = 0; attr_char_value.max_len = BLE_NUS_MAX_RX_CHAR_LEN; return sd_ble_gatts_characteristic_add(p_nus->service_handle, &char_md, &attr_char_value, &p_nus->rx_handles); /**@snippet [Adding proprietary characteristic to S110 SoftDevice] */ }
用lightbule看结果:
是不是成功了呀!!!!其他的也是如此修改。。。。。。
疑问:这个描述有什么限制呢??比如长度??