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

how to Stop advertise when ble are connected

hello.

I want to toggle ble functions when received message from UART.

so when start message come, I start advertising and start services and stop message come, disconnect(if it connected) and stop advertising.

       if(stop)
{
    if(m_conn_handle != BLE_CONN_HANDLE_INVALID) // ble connected
    {
      sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    }
    advertising_stop();
}

but when ble is connected, the advertising_stop() function is not working. because ble going to advertising state when device is connected.

How I Can handle this situation?

I want to disable BLE directly.

  • By default, when you get connected the softdevice automatically disable advertising unless you start it again. You don't have to stop advertising manually.

    In your code, if it's connected you should not call advertising_stop() because it's not advertising. You should do something like this:

          if(stop)
    {
        if(m_conn_handle != BLE_CONN_HANDLE_INVALID) // ble connected
        {
          sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        }
       else
       {
           advertising_stop();
       }
    }
    
  • Thanks Hung. but when disconnected softdevice start advertising again. I want to diable whole ble functions. when I get a stop message if it connected , I want to disconnect and disable advertising. but softdevice automatically restart advertising when disconnected. is there any of options or function to work for this?

  • No, it's not automatically restart advertising. It's the application code does that, somewhere in the code does, you need to find that. Search for advertising_start() or ble_advertising_start or something similar. Which firmware you are testing with ? Which SDK version ?

    If you try any example in the example\ble_peripheral folder, you can find that the device doesn't advertise after you get connected.

Related