This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

why im getting NRF_ERROR_INVALID_STATE with sd_ble_gap_adv_stop

just when I send command to nrf51 to stop advertising by calling sd_ble_gap_adv_stop I got error NRF_ERROR_INVALID_STATE . what's the source of this error ?

static void advertising_init(void)
{
    uint32_t                    err_code;
    ble_advdata_t               advdata;
    ble_advdata_t               scanrsp;
    ble_advdata_manuf_data_t    manufacturer;
    uint8_t                     flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    ble_uuid_t adv_uuids[] = { { DGS_UUID_SERVICE, BLE_UUID_TYPE_BLE } };
    memset(&advdata, 0, sizeof(advdata));
    advdata.name_type = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance = true;
    advdata.flags = flags;
    manufacturer.company_identifier = 7;
manufacturer specified by the company identifier.
    memset(&manufacturerSpecificData, '\0', sizeof(manufacturerSpecificData));

        if (strlen(LOCAL_NAME) <= (sizeof(manufacturerSpecificData) - 2)) {
            strcpy(manufacturerSpecificData, LOCAL_NAME);
        }
        else {
            strncpy(manufacturerSpecificData, LOCAL_NAME, sizeof(manufacturerSpecificData) - 2);
        }
    manufacturerSpecificData[sizeof(manufacturerSpecificData) - 2] = (Vhard & 0xff);
    manufacturerSpecificData[sizeof(manufacturerSpecificData) - 1] = (Vsoft & 0xff);
    manufacturer.data.p_data = (uint8_t *)manufacturerSpecificData;
    manufacturer.data.size = (uint16_t)sizeof(manufacturerSpecificData);
    advdata.p_manuf_specific_data = &manufacturer;
    memset(&scanrsp, 0, sizeof(scanrsp));
    scanrsp.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
    scanrsp.uuids_complete.p_uuids = adv_uuids;
    err_code = ble_advdata_set(&advdata, NULL);
    APP_ERROR_CHECK(err_code);
}

    static void advertising_start(void)
    {
        uint32_t             err_code;
        ble_gap_adv_params_t adv_params;
    
        // Start advertising
        memset(&adv_params, 0, sizeof(adv_params));
    
        adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
        adv_params.p_peer_addr = NULL;
        adv_params.fp = BLE_GAP_ADV_FP_ANY;
        adv_params.interval = APP_ADV_INTERVAL; //64
adv_params.timeout = APP_ADV_TIMEOUT_IN_SECONDS; //BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED
    
        err_code = sd_ble_gap_adv_start(&adv_params);
        APP_ERROR_CHECK(err_code);
        status.system_status = BLE_ADV; 
        status.timeout_adv = 0;
    }

I call advertising_stope function on a characteristic write event

static void advertising_stop(void)
{
    uint32_t err_code;

    err_code = sd_ble_gap_adv_stop();
    APP_ERROR_CHECK(err_code);
}
Parents
  • Good, now you've supplied the actual code and the correct error ...

    What does NRF_ERROR_INVALID_STATE mean when you call sd_ble_gap_adv_stop()? It's documented right there in the manual and/or header file, did you look?

    NRF_ERROR_INVALID_STATE	Invalid state to perform operation (most probably not in advertising state).
    

    You get an error because you're not in the advertising state. And the other clue is also in your edited question

    I call advertising_stope (sic) function on a characteristic write event
    

    if you're in a characteristic write event then you're in a connection which means you've been connected to which means you are no-longer advertising (unless you specifically restarted non-connectable advertising after you went into connection, but I rather doubt that).

  • Couldn't have said it better myself.

Reply Children
No Data
Related