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

unable to enable Notifications using sd_ble_gattc_write (SDK 15.2)

Hi there,

I am migrating my central project from SDK 13 to SDK 15.2, almost all the project is already migrated, but I am having an issue with sd_ble_gattc_write() it is not enabling the notifications when I start a connection and skip ble_db_discovery_start()

so, the first thing I do in my central when it has established a connection with a peripheral is enable the CCCD using sd_ble_gattc_write(), I do it to avoid extra consumption discovering every time the same attribute table for the same Peripheral that already known 

SDK 13 code: it works fine

case BLE_GAP_EVT_CONNECTED:
		{
			NRF_LOG_INFO("Connected\r\n");
			
			(void) sd_ble_gap_scan_stop();

			uint32_t err_code;	
			ble_gattc_write_params_t write_params;
			uint8_t	EnableNotification[2] = {0x01, 0x00};		

			write_params.write_op = BLE_GATT_OP_WRITE_REQ;						
			write_params.handle   = 0x0011;  			
			write_params.offset   = 0;															
			write_params.len      = 2;																
			write_params.p_value  = EnableNotification;													
			
			err_code = sd_ble_gattc_write(0x0000 , &write_params);
			APP_ERROR_CHECK(err_code);

			bsp_board_led_on(LED_CONNECTED);
		} break;

since then I am using it Bypass or skip the discovery service nRF52 SDK 12.2

I am trying to use the same code for the SDK 15.2, it is writing on the peripheral 0x01 0x00 but it is still not enabling the notification (I am writting it on the CCCD handle like when the discovery service is performed) the only way I can make it work is after a ble_db_discovery_start() and event BLE_LBS_C_EVT_DISCOVERY_COMPLETE, but it no makes sense to me if I want to avoid performing a discovery service each time for the same device

as a reference, I am using the BLE Blinky peripheral and central projects to replicate the issue

any suggestion?

Regards,

Arepa

Parents
  • Hi Arepa.

     

    so, the first thing I do in my central when it has established a connection with a peripheral is enable the CCCD using sd_ble_gattc_write(), I do it to avoid extra consumption discovering every time the same attribute table for the same Peripheral that already known 

     You could take a look at nRF5_SDK_15.2.0_9412b96\examples\ble_central_and_peripheral\experimental\ble_app_interactive\cli_m.c and the function static void cccd_set(nrf_cli_t const * p_cli, uint16_t cccd, char * p_addr, char * p_uuid), and make sure that you set up CCCD correct.

    /**@brief Function for setting the CCCD Descriptor value.
     *
     * @param[in] p_cli          Instance of the command line.
     * @param[in] cccd           New CCCD value.
     * @param[in] addr           Device address in string format.
     * @param[in] uuid           Characteristic UUID in string format.
     */
    static void cccd_set(nrf_cli_t const * p_cli, uint16_t cccd, char * p_addr, char * p_uuid)
    {
        uint16_t                 conn_handle;
        uint16_t                 desc_handle;
        ret_code_t               err_code;
        ble_gattc_write_params_t write_params;
    
        // Search for connection handle.
        conn_handle = addr_string_to_conn_handle(p_addr);
    
        if(!conn_handle_is_valid(conn_handle, p_cli))
        {
            return;
        }
    
        memset(&write_params, 0, sizeof(write_params));
    
        desc_handle = cccd_descriptors_handle_get(p_uuid);
    
        if (desc_handle == BLE_GATT_HANDLE_INVALID)
        {
            nrf_cli_fprintf(p_cli,
                            NRF_CLI_ERROR,
                            "Wrong characteristic UUID or the CCCD descriptor has not been found yet\r\n");
            return;
        }
    
        uint16_t cccd_val    = cccd;
        uint8_t  data_buf[2] = {LSB_16(cccd_val), MSB_16(cccd_val)};
        uint8_t  data_len    = sizeof(data_buf);
    
        write_params.write_op = BLE_GATT_OP_WRITE_REQ;
        write_params.handle   = desc_handle;
        write_params.len      = data_len;
        write_params.p_value  = data_buf;
        write_params.offset   = 0;
        write_params.flags    = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
    
        // Set CCCD descriptor value.
        err_code = sd_ble_gattc_write(conn_handle, &write_params);
    
        if (err_code != NRF_SUCCESS)
        {
            nrf_cli_fprintf(p_cli,
                            NRF_CLI_ERROR,
                            "wrong characteristic UUID or the CCCD descriptor has not been found yet\r\n");
        }
    }
    

    - Andreas

Reply
  • Hi Arepa.

     

    so, the first thing I do in my central when it has established a connection with a peripheral is enable the CCCD using sd_ble_gattc_write(), I do it to avoid extra consumption discovering every time the same attribute table for the same Peripheral that already known 

     You could take a look at nRF5_SDK_15.2.0_9412b96\examples\ble_central_and_peripheral\experimental\ble_app_interactive\cli_m.c and the function static void cccd_set(nrf_cli_t const * p_cli, uint16_t cccd, char * p_addr, char * p_uuid), and make sure that you set up CCCD correct.

    /**@brief Function for setting the CCCD Descriptor value.
     *
     * @param[in] p_cli          Instance of the command line.
     * @param[in] cccd           New CCCD value.
     * @param[in] addr           Device address in string format.
     * @param[in] uuid           Characteristic UUID in string format.
     */
    static void cccd_set(nrf_cli_t const * p_cli, uint16_t cccd, char * p_addr, char * p_uuid)
    {
        uint16_t                 conn_handle;
        uint16_t                 desc_handle;
        ret_code_t               err_code;
        ble_gattc_write_params_t write_params;
    
        // Search for connection handle.
        conn_handle = addr_string_to_conn_handle(p_addr);
    
        if(!conn_handle_is_valid(conn_handle, p_cli))
        {
            return;
        }
    
        memset(&write_params, 0, sizeof(write_params));
    
        desc_handle = cccd_descriptors_handle_get(p_uuid);
    
        if (desc_handle == BLE_GATT_HANDLE_INVALID)
        {
            nrf_cli_fprintf(p_cli,
                            NRF_CLI_ERROR,
                            "Wrong characteristic UUID or the CCCD descriptor has not been found yet\r\n");
            return;
        }
    
        uint16_t cccd_val    = cccd;
        uint8_t  data_buf[2] = {LSB_16(cccd_val), MSB_16(cccd_val)};
        uint8_t  data_len    = sizeof(data_buf);
    
        write_params.write_op = BLE_GATT_OP_WRITE_REQ;
        write_params.handle   = desc_handle;
        write_params.len      = data_len;
        write_params.p_value  = data_buf;
        write_params.offset   = 0;
        write_params.flags    = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
    
        // Set CCCD descriptor value.
        err_code = sd_ble_gattc_write(conn_handle, &write_params);
    
        if (err_code != NRF_SUCCESS)
        {
            nrf_cli_fprintf(p_cli,
                            NRF_CLI_ERROR,
                            "wrong characteristic UUID or the CCCD descriptor has not been found yet\r\n");
        }
    }
    

    - Andreas

Children
No Data
Related