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

How to stop advertising and disconnect bluetooth properly?

Hello,

I am working on ble_app_uart example on nRF52840DK.

I am using one switch (gpio) for advertising_switch() function. It means on changing the position of the switch, the advertising will start and after that I will connect to the Bluetooth on my mobile phone.

Likewise, when the switch is put back to it's original position, the advertising will stop and if the Bluetooth is connected yet, it will disconnect.

The function looks like this:

void zip_btn_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
        switch(pin)
    {
      case ZIP:
    if(!nrf_gpio_pin_read(pin))
    {
        advertising_start();
    }
    else
      {
        sd_ble_gap_adv_stop(m_advertising.adv_handle);
        sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        m_conn_handle = BLE_CONN_HANDLE_INVALID;
      }
    
      break;

      default: 
      break;
    }
}

Now the problem is:

When I put the switch back to it's position, the Bluetooth is diconnected but still it continues to show the name of the device (advertising).

On contrary, If I disconnect Bluetooth with the application and then I change the position of the switch, the advertising also stops as I want.

The problem is when I don't disconnect the Bluetooth.

So how can I stop the advertising in this case?

Regards,

Jagruti

Parents Reply
  • Hello,

    Thank you for the reply.

    Yes I tried switching the functions. But still it did not stop the advertising. I did not understand this behaviour.

    Just to avoid advertising after the disconnect event, I removed the on_disconnected() in ble_advertising_on_ble_evt() in ble_advertising.c. But I don't want to change the sdk files.

    So is there any other way to stop advertising?

Children
No Data
Related