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

ble_nus_string_send returns NRF_ERROR_INVALID_STATE

Hi, My code was working perfectly until yesterday, where the function "ble_nus_string_send" on my main loop started returning NRF_ERROR_INVALID_STATE because "p_nus->is_notification_enabled" was false.

In the same code, the same function works fine when called from "nus_data_handler" in order to loopback the received data.

Here is my code:

/**@brief Function for handling the data from the Nordic UART Service.
 *
 * @details This function will process the data received from the Nordic UART BLE Service and send
 *          it to the UART module.
 *
 * @param[in] p_nus    Nordic UART Service structure.
 * @param[in] p_data   Data to be send to UART module.
 * @param[in] length   Length of the data.
 */
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
    uint32_t       err_code;
    err_code = ble_nus_string_send(&m_nus, p_data, length);
    if (err_code != NRF_ERROR_INVALID_STATE)
    {
      APP_ERROR_CHECK(err_code);
    }
}


static void services_init(void)
{
    uint32_t       err_code;
    ble_nus_init_t nus_init;

    memset(&nus_init, 0, sizeof(nus_init));

    nus_init.data_handler = nus_data_handler;

    err_code = ble_nus_init(&m_nus, &nus_init);
    APP_ERROR_CHECK(err_code);
}


int main(void)
{
    uint32_t err_code;
    bool erase_bonds;

    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);

    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    adc_init();
    bool f_time = true;

    // Enter main loop.
    for (;;)
    {
        int i;
        if(send_flag){
          for (i = 0; i < ADC_BUFFER_SIZE; i++)
          {
              for(int j = 0; j<4;j++){
                nrf_drv_adc_channel_enable(&channel_config[j]);
                uint16_t tmp_val;
                APP_ERROR_CHECK(nrf_drv_adc_sample_convert(&channel_config[j], &tmp_val));
                adc_buffer[i][j] = tmp_val;
              }
              highpass(i, SAMPLING_PERIOD_S, Fc, &f_time);
              uint32_t       err_code;
              char snum[30];
              strcpy(snum, "{\"RX\":[");
              char tmp[5];
              itoa(filter_buffer[i][0], tmp, 10);
              strcat(snum, tmp);
              strcat(snum, ",");
              char tmp2[5];
              itoa(filter_buffer[i][1], tmp2, 10);
              strcat(snum, tmp2);
              strcat(snum, ",");
              char tmp3[5];
              itoa(filter_buffer[i][2], tmp3, 10);
              strcat(snum, tmp3);
              strcat(snum, ",");
              char tmp4[5];
              itoa(filter_buffer[i][3], tmp4, 10);
              strcat(snum, tmp4);
              strcat(snum, "]}");
              err_code = ble_nus_string_send(&m_nus, snum, 19);
              power_manage();
              nrf_delay_ms(20);
          }
      }
      else
        power_manage();
    }
}

Any help is welcomed thanks.

Related