如何更改nrf51822的主服务uuid与特性的uuid不一样

最近有一个CSDN的好友给我留言,询问如何设置主服务uuid和特性uuid不一样,如下图:
在这里插入图片描述

我以为这个问题很简单,网上应该一大堆,但是搜索一下,却发现没有这样的文章,于是,把自己的代码给分享出来,供大家学习。

废话就少说,这里直接把代码复制下来。大家可以通过对比软件对比一下就行。使用的是SDK11的,串口服务ble_char,

uint32_t ble_char_init(ble_char_t * p_char, const ble_char_init_t * p_char_init)
{
    uint32_t        err_code;
    ble_uuid_t      ble_uuid,rx_uuid,tx_uuid;
	 #if 1  //add lvyapeng 20210126
      ble_uuid128_t   char_base_uuid = BLE_UUID_CHAR_BASE;
   #endif  //add end 
	
    if ((p_char == NULL) || (p_char_init == NULL))
    {
        return NRF_ERROR_NULL;
    }
    
    // Initialize service structure.
    p_char->conn_handle              = BLE_CONN_HANDLE_INVALID;
    p_char->data_handler             = p_char_init->data_handler;
//    p_char->is_notification_enabled  = false;
    
    /**@snippet [Adding proprietary Service to S110 SoftDevice] */

    // Add custom base UUID.
		#if 1 //add lvyapeng 20210126
    err_code = sd_ble_uuid_vs_add(&char_base_uuid, &p_char->uuid_type);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    ble_uuid.type = p_char->uuid_type;
    ble_uuid.uuid = BLE_UUID_STRING_SERVICE;
     #else	
     BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_STRING_SERVICE);
	  #endif  //add end 20210126
    // Add service.

    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                        &ble_uuid,
                                        &p_char->service_handle);

    /**@snippet [Adding proprietary Service to S110 SoftDevice] */
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
		
    // Add TX Characteristic.

    err_code = tx_char_add(p_char, p_char_init);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    } 

    // Add RX Characteristic.

    err_code = rx_char_add(p_char, p_char_init);
		
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

		// Add test Characteristic. 
    err_code = test_char_add(p_char, p_char_init);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
    
    return NRF_SUCCESS;
}

static uint32_t tx_char_add(ble_char_t * p_char, const ble_char_init_t * p_char_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;
	
	#if 1	//add 20210126 lvyapeng
	  ble_uuid128_t   tx_char_base_uuid = BLE_UUID_TX_CHAR_BASE;

	 BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_TX_STRING_CHAR);
	 ble_uuid.type=BLE_UUID_TYPE_VENDOR_BEGIN;
	 sd_ble_uuid_vs_add(&tx_char_base_uuid, &ble_uuid.type);
	#endif //add end
	
    memset(&char_md, 0, sizeof(char_md));
    
    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;

	
    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_CHAR_MAX_TX_CHAR_LEN;
    
    return sd_ble_gatts_characteristic_add(p_char->service_handle,
                                           &char_md,
                                           &attr_char_value,
                                           &p_char->tx_handles);
}
static uint32_t rx_char_add(ble_char_t * p_char, const ble_char_init_t * p_char_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;
	 uint32_t        err_code;

	
	#if 1	//add 20210126 lvyapeng
    ble_uuid128_t   rx_char_base_uuid = BLE_UUID_RX_CHAR_BASE;
	 BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_RX_STRING_CHAR);
	 ble_uuid.type=BLE_UUID_TYPE_VENDOR_BEGIN;
	 sd_ble_uuid_vs_add(&rx_char_base_uuid, &ble_uuid.type);
	#endif  //addend

    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  = NULL;
    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;
    
    
    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_CHAR_MAX_RX_CHAR_LEN;
    
    return sd_ble_gatts_characteristic_add(p_char->service_handle,
                                           &char_md,
                                           &attr_char_value,
                                           &p_char->rx_handles);
    /**@snippet [Adding proprietary characteristic to S110 SoftDevice] */

}

下面是ble_char.h里面自己的宏定义

#define BLE_UUID_CHAR_BASE				{0x3H, 0x23, 0xcf, 0x40, 0x73, 0x16, 0x42, 0x9A,\
												0x5C, 0x41, 0x7E, 0x7D, 0x00, 0x00, 0x83, 0x14}

#define BLE_UUID_TX_CHAR_BASE				{0xah, 0xe1, 0x26, 0x0a, 0xee, 0x9a, 0xe9, 0xbb,\
												0xb0, 0x49, 0x0b, 0xeb, 0x00, 0x00, 0x00, 0x8b}

#define BLE_UUID_RX_CHAR_BASE				{0x5h, 0x9a, 0x05, 0x43, 0x52, 0xcd, 0xb1, 0xa6,\
												0x1a, 0x4b, 0xe7, 0xa8, 0x00, 0x00, 0x34, 0x07}												
												
//#define BLE_UUID_STRING_SERVICE         0xA8A0                       /**< The UUID of the Nordic UART Service. */
//#define BLE_UUID_TX_STRING_CHAR  		0xa8a3                       /**< The UUID of the TX Characteristic. */
//#define BLE_UUID_RX_STRING_CHAR  		0xa8a4                       /**< The UUID of the RX Characteristic. */
//#define BLE_UUID_TEST_STRING_CHAR	  	0xa8a5

#define BLE_UUID_STRING_SERVICE         0x9ac4                       /**< The UUID of the Nordic UART Service. */
#define BLE_UUID_TX_STRING_CHAR  		    0xace7                       /**< The UUID of the TX Characteristic. */
#define BLE_UUID_RX_STRING_CHAR  		    0x594a                       /**< The UUID of the RX Characteristic. */

下面是协议栈初始化的vs_uuid_count更改由2增加到4,注意更改以后,会增加rom消耗,确保剩余rom能够使用

uint32_t softdevice_enable_get_default_config(uint8_t central_links_count,
                                              uint8_t periph_links_count,
                                              ble_enable_params_t * p_ble_enable_params)
{
    memset(p_ble_enable_params, 0, sizeof(ble_enable_params_t));
    p_ble_enable_params->common_enable_params.vs_uuid_count   = 4;
    p_ble_enable_params->gatts_enable_params.attr_tab_size    = SOFTDEVICE_GATTS_ATTR_TAB_SIZE;
    p_ble_enable_params->gatts_enable_params.service_changed  = SOFTDEVICE_GATTS_SRV_CHANGED;
    p_ble_enable_params->gap_enable_params.periph_conn_count  = periph_links_count;
    p_ble_enable_params->gap_enable_params.central_conn_count = central_links_count;
    if (p_ble_enable_params->gap_enable_params.central_conn_count != 0)
    {
        p_ble_enable_params->gap_enable_params.central_sec_count  = SOFTDEVICE_CENTRAL_SEC_COUNT;
    }

    return NRF_SUCCESS;
}

详细解释:(这里就添加几个图片,相信经常玩nordic能够清楚)
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
下面就是更改以后,烧录进去的成果,这样就更该完成:
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值