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

how to use sd_ble_gatts_service_changed()

After updating an application on nRF51822 chip with a new application which has new Characteristic UUID via BLE, iOS app cannot detect the new Characteristic UUID. I know iOS device uses cache、so nRF51822 has to enable service changed characteristic. I already enable that like this.

ble_enable_params_t ble_enable_params;
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
sd_ble_enable(&ble_enable_params);

and after disconnected, retrieve system attribute like this.

 err_code = sd_ble_gatts_sys_attr_get(m_conn_handle, 
                                                 m_service_changed_sys_attr, 
                                                 &sys_attr_len, 
                                                 BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);

then on next BLE_GAP_EVT_CONN_SEC_UPDATE event, set the system attribute and send service changed indication like this.

err_code = sd_ble_gatts_sys_attr_set(m_conn_handle,
                                             m_service_changed_sys_attr,
                                             sizeof(m_service_changed_sys_attr),
                                             BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
 err_code = sd_ble_gatts_sys_attr_set(m_conn_handle,
                                             NULL,
                                             0,
                                             BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS);
 err_code = sd_ble_gatts_service_changed(m_conn_handle, 0x000C, 0xFFFF);

sd_ble_gatts_service_changed returns NRF_ERROR_INVALID_STATE error.

I can't find any idea or answer for this question. I already look in dfu_transport_ble.c's service_change_indicate(), but I can't solve the problem.

Thank you for your cooperation in advance.

Related