Disconnected BLE [NRF_ERROR_INVALID_STATE]

Hi, 
I have an application that enable BLE (advertise and connectable) then disable BLE (disconnecte and stop advertising) every 10 second.
My application worked when have no any central connected to my nRF. When I checked on Nordict for Mobile to scan my nRF it show off device name of device and disapear after 10s
But when I try to connect to my nRF when it's advertising and after 10s advertising_stop was called it throwed this error


Here is my code
All above configuration I followed by ble_app_uart example

  • Hi!

    The documentation states: Error 8 Invalid state: The advertising handle is not advertising.

    You are seeing this error because you are trying to stop advertising when the device is not actually advertising.

    I assume your device will stop advertising when establishing a connection.
    If you don't want the device to start advertising again after the disconnect, you can define this when receiving the disconnected event.

    Br,
    Joakim

  • Hi Joakim, 
    There are some confusions in here plese clarify for me
    1. What is the difference between connected and non-connected state ? 
    I got this error when I try disconnect BLE while nRF connected with 1 mobile phone. 
    But It worked normally when I disconnect BLE while nRF did not connected with any other central.
    So according to this quote it's mean when nRF connected with any central (in my case nRF is only connected in 1 central at a time). It no longer advertise ?

    You are seeing this error because you are trying to stop advertising when the device is not actually advertising.
  • Hi.

    1.
     When you are connected to a device, the devices will communicate and exchange data etc. on a given interval. This is decided during connection.

    John12 said:
    So according to this quote it's mean when nRF connected with any central (in my case nRF is only connected in 1 central at a time). It no longer advertise ?

    I don't have any knowledge about your code or how you handle the connection events, so I can't be sure. But in most cases (where you only want to be connected to one device at a time), the device will stop advertising after it connects to another device.

    However, the error code that is returned from sd_ble_gap_adv_stop() indicates that you are trying to stop advertising when the device is in fact not advertising.
    Like I said, I don't know much about your code. But it seems that your device stops advertising when you connect. You make a call to advertising_stop(), which terminates the connection and try to stop the advertisement, which returnes an error cause the device isn't advertising.

    What I suggest you try;
    Make a call to terminate the connection but remove the sd_ble_gap_adv_stop() function. When you receive the disconnected event, do not make any call to start advertising/remove the advertising start function already there. This will prevent the device from restarting the advertisement and you can manually start it again.

    Br,
    Joakim

  • Hi Joakim, thanks for your respone
    I got it and handled this issuse
    For another people got this error
    In my case (based on ble_app_uart), split into 2 case:
    1. When my nRF connected with other centeral
    - in this case, nRF no longer advertising so other central can not scan it
    => using disconnect function to disconnect with current central and using stop function to stop advertising
    2. When my nRF not connected with other central

    - in this case, nRF can be scanned. It means nRF is advertising. So use only use stop function to stop advertising
    Conclusion:
    - Before using stop or disconect function, make sure device is connecting with other central, advertising

1 2