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

(Edit) Invoking 'sd_ble_gap_adv_stop()' and 'sd_ble_gap_disconnect()' an error.

Hello

I use nrf52832 and sdk v17.0.

I'm using a battery and I can check whether the battery is charging or not.  And I'm going to turn off the Bluetooth function while the battery is charging.  So I'm going to stop the Bluetooth advertising with 'sd_ble_gap_adv_stop()' or use 'sd_ble_gap_disconnect()' to turn off Bluetooth.

However, when I attempt to invoke these two functions, the error 'NRF_ERROR_INVALID_STATE' is output.  And all operations will be stopped.

I couldn't show all the codes, so I only put some related codes in.   (Based on the example of ble_app_uart.)

/**@brief Function for starting advertising.
 */
static void advertising_start(bool erase_bonds)
{
    if (erase_bonds == true)
    {
        delete_bonds();
        // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
    }
    else
    {
        ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);

        APP_ERROR_CHECK(err_code);
    }
}


static void advertising_stop()
{
    ret_code_t err_code;

    err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle); 
    APP_ERROR_CHECK(err_code);
}



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

    // Initialize.
    gpio_init(); 
    uart_init();
    log_init();
    timers_init();
    app_buttons_init(); 
    power_management_init();
        
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    peer_manager_init();

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);

    adc_configure(); //baterry value & LED
    pwm_init();
    mpu_init(); 
    bsp_board_init(BSP_INIT_LEDS); //use bsp

    nrf_gpio_pin_set(POWER_SW); //HIGH : stay power on

    // Start execution.
    printf("\r\nHelmet Device started.\r\n");
    NRF_LOG_INFO("Debug logging for UART over RTT started.");
    
    advertising_start(erase_bonds); //false

    pwm_change_frequency(1500); 
    do_play_buzzer(); //When start play buzzer

    // Enter main loop.
    for (;;)
    {
        uint32_t err_code;

       charge_state = nrf_gpio_pin_read(bat_state); //read CHG pin     

       if(charge_state == 0) //charging
       {  
          if(m_conn_handle == BLE_CONN_HANDLE_INVALID) //in scanning
          {
           //printf("Not yet pairing!\n");
           advertising_stop(); //not scanning bluetooth //error
          }

          else //in connect pairing
          {
            //m_conn_handle = BLE_CONN_HANDLE_INVALID; //Not working
            err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); //disconnect pairing //error
            APP_ERROR_CHECK(err_code);
          } 

          charging_state = 1;
          PairLED_timers_stop();
          nrf_gpio_pin_clear(Pairing_LED); //pairing led off
       }

       else //no charging
       {      
        //doing
       } 
    }
}

Thank you.
=============================================================================
I think it was because of a problem called several times in loop().  The above issue has been resolved, but an error output when reconnecting after calling 'sd_ble_gap_disconnect()'.
This Log is output when I reconnect bluetooth in the nrf connect app.
V	17:37:53.705	Connecting to FA:21:92:8E:86:C8...
D	17:37:53.705	gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, preferred PHY = LE 1M)
D	17:37:53.813	[Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED
D	17:37:53.840	[Callback] Connection state changed with status: 0 and new state: CONNECTED (2)
I	17:37:53.840	Connected to FA:21:92:8E:86:C8
V	17:37:53.864	Discovering services...
D	17:37:53.864	gatt.discoverServices()
I	17:37:54.504	Connection parameters updated (interval: 7.5ms, latency: 0, timeout: 5000ms)
D	17:37:59.538	[Callback] Connection state changed with status: 8 and new state: DISCONNECTED (0)
E	17:37:59.539	Error 8 (0x8): GATT CONN TIMEOUT
I	17:37:59.539	Disconnected
D	17:37:59.597	[Broadcast] Action received: android.bluetooth.device.action.ACL_DISCONNECTED
Can I know about this problem?

Thank you.
Parents
  • Hi

    Comment on your first (resolved) issue: The NRF_ERROR_INVALID_STATE error is usually returned from sd_ble_gap_adv_stop if the device is not in an advertising state when you initially tried to stop it, so you may indeed have called it from multiple locations.

    This just seems like a regular timeout. Looking at your log I see that your connection parameters are updated with the "timeout: 5000ms" and ~5 seconds (5000ms) later the connection state changes to DISCONNECTED with the reason GATT CONN TIMEOUT. This is due to the connection not being active, and the timeout will disconnect if nothing happens within 5 seconds. If you'd like the connection to last longer, try increasing the CONN_SUP_TIMEOUT in your central application.

    Best regards,

    Simon

Reply
  • Hi

    Comment on your first (resolved) issue: The NRF_ERROR_INVALID_STATE error is usually returned from sd_ble_gap_adv_stop if the device is not in an advertising state when you initially tried to stop it, so you may indeed have called it from multiple locations.

    This just seems like a regular timeout. Looking at your log I see that your connection parameters are updated with the "timeout: 5000ms" and ~5 seconds (5000ms) later the connection state changes to DISCONNECTED with the reason GATT CONN TIMEOUT. This is due to the connection not being active, and the timeout will disconnect if nothing happens within 5 seconds. If you'd like the connection to last longer, try increasing the CONN_SUP_TIMEOUT in your central application.

    Best regards,

    Simon

Children
Related