When has the Central has written to a peripheral?

My application is a Central using SD140.  

The connection to the peripheral is made successfully.  What event will I get when the Central successfully writes to the connected Peripheral's characteristic?

And will there be an event if the Write does not complete?

I am trying to use the following with mixed results:

case BLE_GATTC_EVT_WRITE_RSP:
{
//const ble_gattc_evt_write_t * p_val = &p_ble_evt->evt.gatts_evt.params.write;
const ble_gattc_evt_write_rsp_t * p_val = &p_ble_evt->evt.gattc_evt.params.write_rsp;
// handles moving to the next step when the write has completed.
cble_nus_c_handleWriteChar(p_val);

Thanks,

Dan

 

  • What event will I get when the Central successfully writes to the connected Peripheral's characteristic?

    Using a write request BLE_GATT_OP_WRITE_REQ to write to the characteristic will give you BLE_GATTC_EVT_WRITE_RSP to the client (client device that is writing to the characteristic on the server).

    And will there be an event if the Write does not complete?

    Then you should get an error in that event if some permissions or state was not valid or you will get a procedural timeout if the operation was not completed for other reasons.

  • Hi Susheel,

    Do you have an example of handling the Write errors and Timeout cases?  

    Thanks,

    Dan


    case BLE_GATTC_EVT_WRITE_RSP:
    {
    if ((BLE_GATT_STATUS_SUCCESS != p_ble_evt->evt.gattc_evt.gatt_status))
    {
    // Handle retry of the write depending upon the error

    // What element in the structure would hold the errors?
    }else{// success ...
    const ble_gattc_evt_write_rsp_t * p_val = &p_ble_evt->evt.gattc_evt.params.write_rsp;
    // handles moving to the next step when the write has completed.
    cble_nus_c_handleWriteChar(p_val);
    }
    }
    break;

    case BLE_GATTC_EVT_TIMEOUT:
    // Disconnect on GATT Client timeout event.
    NRF_LOG_DEBUG("GATT Client Timeout.");

    // Put a retry here instead?
    err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
    BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    APP_ERROR_CHECK(err_code);
    break;

  • one example could be to check for what kind of status you get. There could be many status including the rejection of write operation due to auth settings etc.

    For example you can use this code snippet to check to see if you got a response that the write is rejected due to authentication.

    static void on_write_rsp(ble_hrs_c_t * p_ble_hrs_c, const ble_evt_t * p_ble_evt)
    {
        // Check if the event if on the link for this instance
        if (p_ble_hrs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
        {
            return;
        }
    
        if ((p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION) ||
            (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION))
        {
             // write req rejected most likely due to AUTH setup delays, retry until AUTH setup is complete
             // wait until encryption is complete and retry write operation.
    
        }
    }

    and handle the write response from the peer in the ble_hrs_c_on_ble_evt as below

            case BLE_GATTC_EVT_WRITE_RSP:
                on_write_rsp(p_ble_hrs_c, p_ble_evt);
                break;

  • in the similar way you need to check the gatt_status and based on it handle it to see if you need to give up write operation or retry it after sometime.

Related