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

how to get value from custom char?

hi i get vlaue with this line of code image description

but i did not specify what char should get it value how can i get value from specific char? i have nrf51422 v2 sdk v6

  • FormerMember
    0 FormerMember

    The event BLE_GATTS_EVT_WRITE (SDK 6) contains the following information:

    /**@brief Event structure for BLE_GATTS_EVT_WRITE. */
    typedef struct
    {
      uint16_t                    handle;             /**< Attribute Handle. */
      uint8_t                     op;                 /**< Type of write operation, see @ref BLE_GATTS_OPS. */
      ble_gatts_attr_context_t    context;            /**< Attribute Context. */
      uint16_t                    offset;             /**< Offset for the write operation. */
      uint16_t                    len;                /**< Length of the incoming data. */
      uint8_t                     data[1];            /**< Incoming data, variable length. */
    } ble_gatts_evt_write_t;
    

    All BLE event handlers in your application can receive and retrieve data from all BLE write events. The "handle" parameter in the write event tells the intended destination of the write event. However, it is up to the application to pass it there.

    In ble_app_hrs, the device (peripheral) can notify the central with heart rate values. In order for peripheral to start notifying, "notification" has to be enabled. Notifications are being enabled by the central, when writing to the heart rate CCCD, in the example it is handled like this:

    void ble_hrs_on_ble_evt(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt)
    {
    ...
     case BLE_GATTS_EVT_WRITE:
                on_write(p_hrs, p_ble_evt);
                break;
    ...
    }
    

    The on_write(..) function checks if write event contains the correct handle/if the write event is addressed to the heart rate CCCD :

    void ble_hrs_on_ble_evt(ble_hrs_t * p_hrs, ble_evt_t * p_ble_evt) {

        if (p_bas->is_notification_supported)
        {
            ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    
            if (
                (p_evt_write->handle == p_bas->battery_level_handles.cccd_handle)
                &&
                (p_evt_write->len == 2)
               )
            {
                // CCCD written, call application event handler
               ....
    
         }
    }
    
  • If you want the characteristic value you should look for the attribute handle of it. For bas you can find it in p_bas->battery_level_handles.value_handle

Related