cancel
Showing results for 
Search instead for 
Did you mean: 

STM32WB - How to Stop Advertising Bluetooth Connection

TomC
Senior

To start advertising connection, I run

APPE_Init();

adv();

To stop advertising the connection, I run

aci_set_non_discoverable(connection_handle, reason);

aci_gap_terminate();

However the latter functions do not successfully cease advertising, I was wondering where I am going wrong.

1 ACCEPTED SOLUTION

Accepted Solutions
TomC
Senior

I was able to successfully use the Adv_Cancel() function in STM32_WPAN -> App -> app_ble.c by first checking the value of

BleApplicationContext.Device_Connection_Status

which is a static variable in app_ble.c. Adv_Cancel will not do anything if the value of Device_Connection_Status is APP_BLE_CONNECTED_SERVER(0x05). See Adv_Cancel()

static void Adv_Cancel( void )
{
/* USER CODE BEGIN Adv_Cancel_1 */
 
/* USER CODE END Adv_Cancel_1 */
 
  if (BleApplicationContext.Device_Connection_Status != APP_BLE_CONNECTED_SERVER)
 
  {
 
    tBleStatus result = 0x00;
 
    result = aci_gap_set_non_discoverable();
 
    BleApplicationContext.Device_Connection_Status = APP_BLE_IDLE;
    if (result == BLE_STATUS_SUCCESS)
    {
      APP_DBG_MSG("  \r\n\r");APP_DBG_MSG("** STOP ADVERTISING **  \r\n\r");
    }
    else
    {
      APP_DBG_MSG("** STOP ADVERTISING **  Failed \r\n\r");
    }
 
  }

Where I went wrong was not waiting for the value of BleApplicationContext.Device_Connection_Status to change from APP_BLE_CONNECTED_SERVER to something else after disconnection. See possible values for BleApplicationContext.Device_Connection_Status

typedef enum
    {
      APP_BLE_IDLE,
      APP_BLE_FAST_ADV,
      APP_BLE_LP_ADV,
      APP_BLE_SCAN,
      APP_BLE_LP_CONNECTING,
      APP_BLE_CONNECTED_SERVER,
      APP_BLE_CONNECTED_CLIENT
    } APP_BLE_ConnStatus_t;

View solution in original post

2 REPLIES 2
YSN
Senior

aci_gap_set_non_discoverable();

it is working for me !

TomC
Senior

I was able to successfully use the Adv_Cancel() function in STM32_WPAN -> App -> app_ble.c by first checking the value of

BleApplicationContext.Device_Connection_Status

which is a static variable in app_ble.c. Adv_Cancel will not do anything if the value of Device_Connection_Status is APP_BLE_CONNECTED_SERVER(0x05). See Adv_Cancel()

static void Adv_Cancel( void )
{
/* USER CODE BEGIN Adv_Cancel_1 */
 
/* USER CODE END Adv_Cancel_1 */
 
  if (BleApplicationContext.Device_Connection_Status != APP_BLE_CONNECTED_SERVER)
 
  {
 
    tBleStatus result = 0x00;
 
    result = aci_gap_set_non_discoverable();
 
    BleApplicationContext.Device_Connection_Status = APP_BLE_IDLE;
    if (result == BLE_STATUS_SUCCESS)
    {
      APP_DBG_MSG("  \r\n\r");APP_DBG_MSG("** STOP ADVERTISING **  \r\n\r");
    }
    else
    {
      APP_DBG_MSG("** STOP ADVERTISING **  Failed \r\n\r");
    }
 
  }

Where I went wrong was not waiting for the value of BleApplicationContext.Device_Connection_Status to change from APP_BLE_CONNECTED_SERVER to something else after disconnection. See possible values for BleApplicationContext.Device_Connection_Status

typedef enum
    {
      APP_BLE_IDLE,
      APP_BLE_FAST_ADV,
      APP_BLE_LP_ADV,
      APP_BLE_SCAN,
      APP_BLE_LP_CONNECTING,
      APP_BLE_CONNECTED_SERVER,
      APP_BLE_CONNECTED_CLIENT
    } APP_BLE_ConnStatus_t;