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

where use sd_ble_gattc_read

Hi, I would like read a long characteristic. I know that I have to use sd_ble_gattc_read and I have to multiple call this function. But I don't know where I have to call this function? After service discovery in this block? In some 'for loop' ?

if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
        p_evt->params.discovered_db.srv_uuid.uuid == BLE_DEUFOL_SERVICE_UUID &&
        p_evt->params.discovered_db.srv_uuid.type == p_deufol->uuid_type)
    {
counter++;
for(uint16_t x=0;x<25;x++)
											err_code = sd_ble_gattc_read(p_deufol->conn_handle,p_chars[i].characteristic.handle_value , x * 20);


}

You don't have any central example for long read.

When I tried use volatile global variable as counter, but counter is still zero. I increased counter in that block too. Why this variable is not hold in memory ?

  • Hi,

    dont call functions which trigger events inside event handlers.

    Call sd_ble_gattc_read inside main anytime you have a connection (m_conn_handle != BLE_CONN_HANDLE_INVALID).

    After you get BLE_GATTC_EVT_READ_RSP, call sd_ble_gattc_read(m_conn_handle, handle, 20) again with some offset (e.g. 20).

    Again, wait for the BLE_GATTC_EVT_READ_RSP, and then you can concatenate the first and the second data into one (array) variable.

  • I still don't understand. I call this function from main how you adviced me, but It still doesn't work. my call:

    while(1)
        {			
    			if(m_deufol.conn_handle != BLE_CONN_HANDLE_INVALID)
    			{
    				if(m_deufol.common_handles.value_handle != 0)
    				{
    					for(uint8_t x=0;x<2;x++)
    						sd_ble_gattc_read(m_deufol.conn_handle,m_deufol.common_handles.value_handle , x*20);
    				}
    			}
            //power_manage();
        }
    

    and I have this in on_ble_evt:

    case BLE_GATTC_EVT_READ_RSP:							
    							if(p_ble_evt->evt.gattc_evt.params.read_rsp.handle == 0x29)
    							{
    								for(uint16_t x = p_ble_evt->evt.gattc_evt.params.read_rsp.offset;x < p_ble_evt->evt.gattc_evt.params.read_rsp.offset +20;x++)
    								{
    									readvalue[x] = p_ble_evt->evt.gattc_evt.params.read_rsp.data[x - p_ble_evt->evt.gattc_evt.params.read_rsp.offset];
    								}
    							}
    				
    					break;
    

    I can't set any variable as flag too, because global variable is reseting and I don't know why. It seems that event on sd_ble_gattc_read is fired only for last call in my loop.

    I have suspicion that part of my problems debugger cause. Anyway, How I recognize what packet arrives? There is no packet identification.

  • You get an error, because you are looping through sd_ble_gattc_read without a delay.

        while(1)
            {           
                    if(m_deufol.conn_handle != BLE_CONN_HANDLE_INVALID)
                    {
                        if(m_deufol.common_handles.value_handle != 0)
                        {
                            for(uint8_t x=0;x<2;x++) {
                                          sd_ble_gattc_read(m_deufol.conn_handle,m_deufol.common_handles.value_handle , x*20);
                             DELAY 
                            }
                        }
                          DELAY
                }
    

    It should work with proper delays, but thenafter you should rewrite your code without delays in such a way that you wait for events.

  • @Jackx. As gorazd says you should follow the flow in this message sequence chart.

Related