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

Update characteristic when no device is connected and beeing notified on notification enabled.

I have several characteristics which are updated by a regular timer.

The characteristic property is notify. Due to memory issues I use as value location BLE_GATTS_VLOC_STACK.

In case I am connected I use the function sd_ble_gatts_hvx() to notify the connected device.

In case I am not connected I update the value via sd_ble_gatts_value_set(BLE_CONN_HANDLE_INVALID, pChara->handler.value_handle, &value); with value.p_value set to 0 and the buffer of the characteristic is updated via memcpy.

So my question:

If I connect to the nordic chip and set the notifiy property from the connected device, I do not get the current value of the characteristic. Only when a call to sd_ble_gatts_hvx() is done the connected device is notified. Do I need to do an explicte read on connection?

Parents
  • Hi,

    If the central need the value immediately after the service discovery, and it don't want to wait for any notification, you could read the characteristic from the central directly. This is done in e.g. the central example ble_app_hrs_c in the SDK with ble_bas_c_bl_read().

    Snippet:

    case BLE_BAS_C_EVT_DISCOVERY_COMPLETE:
            {
                err_code = ble_bas_c_handles_assign(p_bas_c,
                                                    p_bas_c_evt->conn_handle,
                                                    &p_bas_c_evt->params.bas_db);
                APP_ERROR_CHECK(err_code);
    
                // Initiate bonding.
                err_code = pm_conn_secure(p_bas_c_evt->conn_handle, false);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
    
                // Batttery service discovered. Enable notification of Battery Level.
                NRF_LOG_DEBUG("Battery Service discovered. Reading battery level.");
    
                err_code = ble_bas_c_bl_read(p_bas_c);
                APP_ERROR_CHECK(err_code);
    
                NRF_LOG_DEBUG("Enabling Battery Level Notification.");
                err_code = ble_bas_c_bl_notif_enable(p_bas_c);
                APP_ERROR_CHECK(err_code);

    Another option is to send the value with sd_ble_gatts_hvx() immediately after the central enables notification. Using the Battery Service as an example, in addition to starting a timer that sends notifications periodically, you could also send the value when you get the first BLE_BAS_EVT_NOTIFICATION_ENABLED event.

     

    case BLE_BAS_EVT_NOTIFICATION_ENABLED:
    // Start battery timer
    err_code = app_timer_start(m_battery_timer_id, BATTERY_LEVEL_MEAS_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);
    
    //Add code with sd_ble_gatts_hvx() / ble_bas_battery_level_update()
    
    break; // BLE_BAS_EVT_NOTIFICATION_ENABLED

  • Thank you for the answer.

    I think the best way is to send it directly after the BLE_BAS_EVT_NOTIFICATION_ENABLED event.

Reply Children
No Data
Related