加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

nrf51822 --- 特征值添加描述

(2017-01-20 17:26:20)
分类: 蓝牙
FROM:http://blog.csdn.net/a369000753/article/details/51008929

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”

 

[cpp] view plain copy
 https://code.csdn.net/assets/ico_fork.svg--- 特征值添加描述" />
  1. static uint32_t tx_char_add(ble_nus_t p_nus, const ble_nus_init_t p_nus_init)  
  2.  
  3.     ble_gatts_char_md_t char_md;  
  4.     ble_gatts_attr_t    attr_char_value;  
  5.     ble_uuid_t          ble_uuid;  
  6.     ble_gatts_attr_md_t attr_md;  
  7.     char tx_desc[] "Tx"  //添加代码1  
  8.       
  9.     memset(&char_md, 0, sizeof(char_md));  
  10.   
  11.       char_md.p_char_user_desc  (uint8_t *)tx_desc //添加代码2  
  12.         char_md.char_user_desc_size strlen(tx_desc);//添加代码3  
  13.       char_md.char_user_desc_max_size =strlen(tx_desc);//添加代码4  
  14.       
  15.     char_md.char_props.write         1;  
  16.     char_md.char_props.write_wo_resp 1;  
  17.     //char_md.p_char_user_desc         NULL;  
  18.     char_md.p_char_pf                NULL;  
  19.     char_md.p_user_desc_md           NULL;  
  20.     char_md.p_cccd_md                NULL;  
  21.     char_md.p_sccd_md                NULL;  
  22.    ble_uuid.type p_nus->uuid_type;  
  23.     ble_uuid.uuid BLE_UUID_NUS_TX_CHARACTERISTIC;  
  24.   
  25.     memset(&attr_md, 0, sizeof(attr_md));  
  26.   
  27.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);  
  28.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);  
  29.   
  30.     attr_md.vloc    BLE_GATTS_VLOC_STACK;  
  31.     attr_md.rd_auth 0;  
  32.     attr_md.wr_auth 0;  
  33.     attr_md.vlen    1;  
  34.   
  35.     memset(&attr_char_value, 0, sizeof(attr_char_value));  
  36.   attr_char_value.p_uuid    &ble_uuid;  
  37.     attr_char_value.p_attr_md &attr_md;  
  38.     attr_char_value.init_len  1;  
  39.     attr_char_value.init_offs 0;  
  40.     attr_char_value.max_len   BLE_NUS_MAX_TX_CHAR_LEN;  
  41.   
  42.     return sd_ble_gatts_characteristic_add(p_nus->service_handle,  
  43.                                            &char_md,  
  44.                                            &attr_char_value,  
  45.                                            &p_nus->tx_handles);  
  46.  


 



[cpp] view plain copy
 https://code.csdn.net/assets/ico_fork.svg--- 特征值添加描述" />
  1. static uint32_t rx_char_add(ble_nus_t p_nus, const ble_nus_init_t p_nus_init)  
  2.  
  3.       
  4.     ble_gatts_char_md_t char_md;  
  5.     ble_gatts_attr_md_t cccd_md;  
  6.     ble_gatts_attr_t    attr_char_value;  
  7.     ble_uuid_t          ble_uuid;  
  8.     ble_gatts_attr_md_t attr_md;  
  9.     char rx_desc[] "Rx"   //添加代码1  
  10.       
  11.     memset(&cccd_md, 0, sizeof(cccd_md));  
  12.   
  13.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);  
  14.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);  
  15.   
  16.     cccd_md.vloc BLE_GATTS_VLOC_STACK;  
  17.   
  18.     memset(&char_md, 0, sizeof(char_md));  
  19.   
  20.     char_md.char_props.notify 1;  
  21.   char_md.p_char_user_desc  (uint8_t *)rx_desc  //添加代码2  
  22.         char_md.char_user_desc_size strlen(rx_desc);//添加代码3  
  23.       char_md.char_user_desc_max_size =strlen(rx_desc);//添加代码4  
  24.           
  25.     char_md.p_char_pf         NULL;  
  26.     char_md.p_user_desc_md    NULL;  
  27.     char_md.p_cccd_md         &cccd_md;  
  28.     char_md.p_sccd_md         NULL;  
  29.   
  30.     ble_uuid.type p_nus->uuid_type;  
  31.     ble_uuid.uuid BLE_UUID_NUS_RX_CHARACTERISTIC;  
  32.   
  33.     memset(&attr_md, 0, sizeof(attr_md));  
  34.   
  35.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);  
  36.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);  
  37.   
  38.     attr_md.vloc    BLE_GATTS_VLOC_STACK;  
  39.     attr_md.rd_auth 0;  
  40.     attr_md.wr_auth 0;  
  41.     attr_md.vlen    1;  
  42.   memset(&attr_char_value, 0, sizeof(attr_char_value));  
  43.   
  44.     attr_char_value.p_uuid    &ble_uuid;  
  45.     attr_char_value.p_attr_md &attr_md;  
  46.     attr_char_value.init_len  sizeof(uint8_t);  
  47.     attr_char_value.init_offs 0;  
  48.     attr_char_value.max_len   BLE_NUS_MAX_RX_CHAR_LEN;  
  49.   
  50.     return sd_ble_gatts_characteristic_add(p_nus->service_handle,  
  51.                                            &char_md,  
  52.                                            &attr_char_value,  
  53.                                            &p_nus->rx_handles);  
  54.       
  55.  

编译代码 downloda to the board!!!

 

用lightbule看结果:

  
http://img.blog.csdn.net/20160329202337423--- 特征值添加描述" />

用nordic-connect 查看效果如下

http://s5/bmiddle/003hEzXrzy788tFiJKY74&690--- 特征值添加描述" TITLE="nrf51822 --- 特征值添加描述" />

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有