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

How to make custom characteristic with Indication?

Based on Battery Service I have made my own Service and Characteristics but I have problem with setting Indication. This is how my characteristic add function looks like:

static uint32_t xyz_response_char_add(ble_bws_t * p_bws, 
						const ble_bws_init_t *p_bws_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 cccd_md;
	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.notify = 1;
	char_md.char_props.indicate = 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;

	ble_uuid.uuid = 0x0002;
	ble_uuid.type = BLE_UUID_TYPE_VENDOR_BEGIN;

	memset(&attr_md, 0, sizeof(attr_md));

	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_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  = 0;
	attr_char_value.init_offs = 0;
	attr_char_value.max_len   = CHAR_VAL_LEN;
	attr_char_value.p_value   = NULL;

	return sd_ble_gatts_characteristic_add(p_bws->service_handle,
		&char_md,&attr_char_value, &p_bws->response_handles);

}

Uisng nRF Master Control Panel I am able to Read Value of characteristic or enable Notification/Indication, however nor Notification neither Indication works. In application I use

sd_ble_gatts_value_set(params);

to change value of characteristic. The problem is that I have to perform Read Operation to get that value in Control Panel even if Notification/Indication is enabled.

Parents
  • Hi

    Try to use sd_ble_gatts_hvx(). "hvx" stands for Handle Value x, where x can be Indication or Notification. Notification/Indication Difference.

    This is how the heart rate example ble_app_hrs example in SDK 9 sends a notification:

    uint32_t ble_hrs_heart_rate_measurement_send(ble_hrs_t * p_hrs, uint16_t heart_rate)
    {
        uint32_t err_code;
    
        // Send value if connected and notifying
        if (p_hrs->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            uint8_t                encoded_hrm[MAX_HRM_LEN];
            uint16_t               len;
            uint16_t               hvx_len;
            ble_gatts_hvx_params_t hvx_params;
    
            len     = hrm_encode(p_hrs, heart_rate, encoded_hrm);
            hvx_len = len;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_hrs->hrm_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &hvx_len;
            hvx_params.p_data = encoded_hrm;
    
            err_code = sd_ble_gatts_hvx(p_hrs->conn_handle, &hvx_params);
            if ((err_code == NRF_SUCCESS) && (hvx_len != len))
            {
                err_code = NRF_ERROR_DATA_SIZE;
            }
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
    
        return err_code;
    }
    
Reply
  • Hi

    Try to use sd_ble_gatts_hvx(). "hvx" stands for Handle Value x, where x can be Indication or Notification. Notification/Indication Difference.

    This is how the heart rate example ble_app_hrs example in SDK 9 sends a notification:

    uint32_t ble_hrs_heart_rate_measurement_send(ble_hrs_t * p_hrs, uint16_t heart_rate)
    {
        uint32_t err_code;
    
        // Send value if connected and notifying
        if (p_hrs->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            uint8_t                encoded_hrm[MAX_HRM_LEN];
            uint16_t               len;
            uint16_t               hvx_len;
            ble_gatts_hvx_params_t hvx_params;
    
            len     = hrm_encode(p_hrs, heart_rate, encoded_hrm);
            hvx_len = len;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_hrs->hrm_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &hvx_len;
            hvx_params.p_data = encoded_hrm;
    
            err_code = sd_ble_gatts_hvx(p_hrs->conn_handle, &hvx_params);
            if ((err_code == NRF_SUCCESS) && (hvx_len != len))
            {
                err_code = NRF_ERROR_DATA_SIZE;
            }
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
    
        return err_code;
    }
    
Children
Related