This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to add an additional characteristic to the NUS service

I tried to add an additional characteristic to NUS service as below.

However, any mobile apps could find only existing Nordic Rx and Nordic Tx characteristics, other than the new characteristics.

Based on the several experiment, only two characteristics under NUS service should be allowed.

Can you please let me know how to add the new one under NUS service?

FYI, there is no error though.

uint32_t ble_nus_init(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
{
    uint32_t      err_code;
    ble_uuid_t    ble_uuid;
    ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;

    VERIFY_PARAM_NOT_NULL(p_nus);
    VERIFY_PARAM_NOT_NULL(p_nus_init);

    // Initialize the service structure.
    p_nus->conn_handle             = BLE_CONN_HANDLE_INVALID;
    p_nus->data_handler            = p_nus_init->data_handler;
    p_nus->is_notification_enabled = false;

   
    // Add a custom base UUID.
    err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_nus->uuid_type;
    ble_uuid.uuid = BLE_UUID_NUS_SERVICE;

    // Add the service.
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                        &ble_uuid,
                                        &p_nus->service_handle);
    
    VERIFY_SUCCESS(err_code);
		
    // Add the RX Characteristic.
    err_code = rx_char_add(p_nus, p_nus_init);
    VERIFY_SUCCESS(err_code);

    // Add the TX Characteristic.
    err_code = tx_char_add(p_nus, p_nus_init);
    VERIFY_SUCCESS(err_code);
		
	// Add A new Characteristic.
	err_code = a_new_char_add(p_nus, p_nus_init);
	VERIFY_SUCCESS(err_code);


    return NRF_SUCCESS;
}

static uint32_t a_new_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
{
    uint32_t   err_code = 0; // Variable to hold return codes from library and softdevice functions
    
		ble_gatts_char_md_t char_md;
		ble_gatts_attr_md_t cccd_md;
		ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          char_uuid;
		ble_gatts_attr_md_t attr_md;
		
		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.read   = 1; 
    char_md.char_props.write  = 1;
	  char_md.p_cccd_md           = &cccd_md;
    char_md.char_props.notify   = 1;
		char_uuid.type = p_nus->uuid_type;
		char_uuid.uuid      = BLE_UUID_ASI_CHARACTERISTC_UUID;
    
		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      = &char_uuid;
    attr_char_value.p_attr_md   = &attr_md;
    attr_char_value.max_len     = 128;
    attr_char_value.init_len    = 128;
    uint8_t value[128]            = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
																		 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
    attr_char_value.p_value     = value;

    // ASI_JOB: Step 2.E, Add a new characteristic to the service
    err_code = sd_ble_gatts_characteristic_add(p_nus->service_handle,
                                       &char_md,
                                       &attr_char_value,
                                       &p_nus->char_handles);
    APP_ERROR_CHECK(err_code);
    
    printf("\r\nService handle: %#x\n\r", p_nus->service_handle);
    printf("Char value handle: %#x\r\n", p_nus->char_handles.value_handle);
    printf("Char cccd handle: %#x\r\n\r\n", p_nus->char_handles.cccd_handle);

    return NRF_SUCCESS;
}

Related