In our Android app, we want to throttle back MTU size when the error rates get too high. However, the app is crashing with NRF_ERROR_INVALID_STATE when we try to send the reply. To reproduce this, I used nRF Connect on Android, and did a "set MTU" to 200. We are using SDK 16.
static void on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
uint32_t err_code = NRF_SUCCESS;
ble_sp_on_ble_evt(&m_sp, p_ble_evt);
switch (p_ble_evt->header.evt_id)
{
...
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
NRF_LOG_INFO("BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST");
err_code = sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle,
NRF_BLE_MAX_MTU_SIZE);
APP_ERROR_CHECK(err_code);
break; // BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST
00> <info> app: Connection interval updated: 0x18, 0x18.
00> <debug> nrf_ble_gatt: Peer on connection 0x0 requested an ATT MTU of 200 bytes.
00> <debug> nrf_ble_gatt: Updating ATT MTU to 200 bytes (desired: 247) on connection 0x0.
00> <info> app: Data len is set to 0xC5(197)
00> <info> app: ATT MTU exchange completed. desired central 0xF7 peripheral 0xF7, effective both: 0xC8
00> <info> app: BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST
00> <error> app: Fatal error
00> <error> app: ERROR 8 [NRF_ERROR_INVALID_STATE] at :0
00> PC at: 0x00000000
00> <error> app: End of error report